PDFsharp & MigraDoc Foundation http://forum.pdfsharp.com/ |
|
Creating custom RubberStamp Annotation Appearance-Stream http://forum.pdfsharp.com/viewtopic.php?f=2&t=4543 |
Page 1 of 1 |
Author: | JZsolt [ Wed Jan 31, 2024 4:18 pm ] |
Post subject: | Creating custom RubberStamp Annotation Appearance-Stream |
I have been using PdfSharp library in a C# project for saving PDF files with Annotations. I would like to create a RubberStamp Annotation with a custom image. PfSharp has a nice RubberStamp implementation, but it does not let you specify custom images, only the predefined icons like TopSecret. I have tried to analyze other PDF files with custom Stamps and tried to mimic the same structure as they have, by manually creating the XObjects, adding the image to the Resources, and creating an Appearance Stream. Long story short, it's not working. I have a feeling that simply writing the object to the Appearance-Stream is not a good idea, even though the image gets to the file, I can see the annotation rectangle in the PDF Viewer, but it's a blank white rect, showing nothing. Ideally, I would like to trigger PDFSharp to create the Appearance Stream, but after several hours of online research, I haven't found anything useful. Can anyone point me out how can I add a custom image to the RubberStamp Appearance-Stream? Or trigger the PdfSharp to recreate the Appearance-Stream? Code: public void SavePdf()
{ System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); pdfDocument.Version = 15; // Create an empty page PdfPage pdfPage; pdfPage = pdfDocument.AddPage(); pdfPage.Width = XUnit.FromInch(8.5); pdfPage.Height = XUnit.FromInch(11.00); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(pdfPage); // Creating the Rubber Stamp with some default settings PdfRubberStampAnnotation cstamp = new PdfRubberStampAnnotation() { Icon = PdfRubberStampAnnotationIcon.TopSecret }; XRect rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(10, 10), new XSize(200, 50))); cstamp.Rectangle = new PdfRectangle(rect); cstamp.Color = XColor.FromArgb(255, 0, 0); // Load the custom image var image = XImage.FromFile(@"c:\temp\test.bmp"); var pdfImage = new PdfImage(pdfDocument, image); string r = pdfPage.Resources.AddImage(pdfImage); // adding the image to the Page References // Now let's create the XForm object, and reference the image var matrix = new PdfArray(); matrix.Elements.Add(new PdfReal(1)); matrix.Elements.Add(new PdfReal(0)); matrix.Elements.Add(new PdfReal(0)); matrix.Elements.Add(new PdfReal(1)); matrix.Elements.Add(new PdfReal(0)); matrix.Elements.Add(new PdfReal(0)); var xform = new PdfDictionary(pdfDocument); xform.Elements.Add("/Type", new PdfName("/XObject")); xform.Elements.Add("/Subtype", new PdfName("/Form")); xform.Elements.Add("/FormType", new PdfInteger(1)); xform.Elements.Add("/BBox", new PdfRectangle(new XRect(10, 10, image.PointWidth, image.PointHeight))); xform.Elements.Add("/Matrix", matrix); xform.Elements.Add("/Resources", pdfImage.Reference); xform.Elements.Add("/Length", new PdfInteger(0)); // Now let's create the appearance stream, and add the XForm object var appearanceStream = new PdfDictionary(pdfDocument); appearanceStream.Elements.Add("/N", xform); cstamp.Elements.Add("/AP", appearanceStream); // Add to the page gfx.PdfPage.Annotations.Add(cstamp); pdfDocument.Save(@"c:\temp\1.pdf"); } |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |