PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 3:02 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Jan 31, 2024 4:18 pm 
Offline

Joined: Wed Jan 31, 2024 4:10 pm
Posts: 1
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");
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 383 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group