Problem with pdf signature placement

Hello,

I am following example on how to digitally sign the document. Everything works as expected except for the signature placement. The code below properly creates a signature but places it in upper left corner of the document (0,0) instead of coordinates specified:

            // Load the PDF document.
            FileStream docStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
            
            // Get the loaded form
            PdfLoadedForm loadedForm = loadedDocument.Form;


            // Load the form field collections from the form.
            PdfLoadedFormFieldCollection fieldCollection = loadedForm.Fields as PdfLoadedFormFieldCollection;
            PdfLoadedField loadedField = null;


            // Create PDF graphics for the page
            PdfGraphics graphics = loadedDocument.Pages[0].Graphics;


            // Creates a certificate instance from PFX file with private key
            FileStream certificateStream = new FileStream(ConfigManager.Instance().GetValue(DefaultEnum.PdfCertificate), FileMode.Open, FileAccess.Read);
            PdfCertificate pdfCert = new PdfCertificate(certificateStream, ConfigManager.Instance().GetValue(DefaultEnum.PdfCertificatePassword));


            // Creates a digital signature
            PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], pdfCert, "Signature");


            // Sets an image for signature field.
            FileStream imageStream = new FileStream(ConfigManager.Instance().GetValue(DefaultEnum.PdfCertificateSignature), FileMode.Open, FileAccess.Read);
            
            // Sets an image for signature field.
            PdfBitmap signatureImage = new PdfBitmap(imageStream);


            // Sets signature information
            signature.Bounds = new RectangleF(new PointF(0, 0), signatureImage.PhysicalDimension);
            signature.ContactInfo = "xxx@lakecouxxxntyin.org";
            signature.LocationInfo = "abc, CA";
            signature.Reason = "I am authorized to sign this document.";


            // Draws the signature image
            graphics.DrawImage(signatureImage, 1.29f, 8.66f);

I appreciate any help in resolving this issue. Many thanks in advance!


3 Replies

JT Jeyalakshmi Thangamarippandian Syncfusion Team May 17, 2024 09:46 AM UTC

Hi Mark,

We can specify the location of the signature field using the bounds property available in the PdfSignature instance. We have updated the code for your reference. Please try this on your end and let us know if you need any further assistance.

 

// Load the PDF document.

FileStream docStream = new FileStream(Server.MapPath("~/App_Data/Input.pdf"), FileMode.Open, FileAccess.Read);

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

 

// Get the loaded form

PdfLoadedForm loadedForm = loadedDocument.Form;

 

 

// Load the form field collections from the form.

PdfLoadedFormFieldCollection fieldCollection = loadedForm.Fields as PdfLoadedFormFieldCollection;

PdfLoadedField loadedField = null;

 

 

// Create PDF graphics for the page

PdfGraphics graphics = loadedDocument.Pages[0].Graphics;

 

 

// Creates a certificate instance from PFX file with private key

FileStream certificateStream = new FileStream(Server.MapPath("~/App_Data/pdf.pfx"), FileMode.Open, FileAccess.Read);

PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123");

 

 

// Creates a digital signature

PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], pdfCert, "Signature");

 

 

// Sets an image for signature field.

FileStream imageStream = new FileStream(Server.MapPath("~/App_Data/syncfusion.jpg"), FileMode.Open, FileAccess.Read);

 

// Sets an image for signature field.

PdfBitmap signatureImage = new PdfBitmap(imageStream);

 

 

// Sets signature information

signature.Bounds = new RectangleF(new PointF(100, 100), signatureImage.PhysicalDimension);

signature.ContactInfo = "xxx@lakecouxxxntyin.org";

signature.LocationInfo = "abc, CA";

signature.Reason = "I am authorized to sign this document.";

 

 

// Draws the signature image

signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, signature.Bounds.Width, signature.Bounds.Height);

 

In this above code we placed the signature at the position of x=100 and y=100 and drawn the image to the signature appearance instead of the PDF page graphics.


Regards,

Jeyalakshmi T



MA Mark May 19, 2024 04:36 PM UTC

Hello Jeyalakshmi,

This was exactly it! Many thanks again for prompt reply and solution.



JT Jeyalakshmi Thangamarippandian Syncfusion Team May 20, 2024 09:02 AM UTC

We are glad to hear, please get back to us if you need any further assistance on this. We always happy to assist you.


Loader.
Up arrow icon