Pdf signature not visible except if in the top left corner

Hello,


Trying to sign a document with a visible signature that uses only text (no bitmaps), the signature appears in the pdf only in the top left corner. Any other position does not show the text in the signature fields.

This is the code i am using:


private void StampSignature(SignatureSettings signature, PdfLoadedDocument document, PdfLoadedPage page, PdfCertificate pdfCert, string field)

        {

            var pdfSignature = new PdfSignature(document, page, pdfCert, field);

            if (signature.Appearance != null)

            {

                pdfSignature.Bounds = GetSignatureBounds(signature.Appearance, page);

                pdfSignature.LocationInfo = signature.SignatureLocation;

                pdfSignature.SignedName = pdfCert.SubjectName;

                pdfSignature.Reason = signature.SignatureText;

                if (signature.IsFinal)

                {

                    pdfSignature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;

                    pdfSignature.Certificated = true;

                }

                var visibleText = new StringBuilder();

                visibleText.AppendLine(pdfCert.SubjectName);

                visibleText.AppendLine(BitConverter.ToString(pdfCert.SerialNumber));

                visibleText.AppendLine(string.IsNullOrEmpty(signature.SignatureText) ? field : signature.SignatureText);


                var font = new PdfStandardFont(PdfFontFamily.Helvetica, 4f);


                PdfStringLayouter layouter = new PdfStringLayouter();

                PdfStringLayoutResult layout = layouter.Layout(

                    visibleText.ToString(),

                    font,

                    new PdfStringFormat(PdfTextAlignment.Left),

                    pdfSignature.Bounds.Size);

                if (layout.Remainder != null && layout.Remainder.Length != 0)

                {

                    PdfFont resizedFont = GetResizedFont(visibleText.ToString(), pdfSignature.Bounds, font);

                    pdfSignature.Appearance.Normal.Graphics.DrawString(visibleText.ToString(), resizedFont, PdfBrushes.Black, pdfSignature.Bounds);

                }

                else

                {

                    pdfSignature.Appearance.Normal.Graphics.DrawString(visibleText.ToString(), font, PdfBrushes.Black, pdfSignature.Bounds);

                }


                pdfSignature.EnableValidationAppearance = false;

                pdfSignature.EnableLtv = true;

            }

        }


Tried using page.Graphics.DrawString instead of pdfSignature.Appearence.Normal.Graphics.DrawString, but using page.Graphics.DrawString does not work when applying multiple signatures because each sequential one invalidates the previous, because the document changes.

Below is an example.

In the left corner i have a visible signature which works, in the right corner i should have something similar, but doesn't show up.


3 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team May 17, 2022 05:41 PM UTC

Hi Cristian,

We can add multiple digital signatures in a PDF document by appending additional signatures to an already signed PDF file. We have attached the modified sample to add multiple signatures in the PDF document without breaking the existing signature for your reference, Please try the sample with your input signature on your end and let us know the result.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-1871013479

Please refer to the below link for more information,
UG: https://help.syncfusion.com/file-formats/pdf/working-with-digitalsignature    

Blogs: https://www.syncfusion.com/blogs/post/create-validate-pdf-digital-signatures-csharp.aspx#add-multiple-digital-signatures-in-a-single-PDF-document

KB: https://www.syncfusion..com/kb/9801/how-to-apply-one-or-more-digital-signatures-to-a-pdf-using-c-and-vb-net


Please let us know if you need any further assistance with this.


Regards,

Gowthamraj K


Marked as answer

CR Cristian May 19, 2022 05:34 AM UTC

Hello,


Finally realized what was going on, but still i wonder why.


In your example you draw the string in the bounds of the pdf signature (the same thing that i did) and it drew the string using the bounds as page coordinates.


In my example, when i did the same the signature was not visible, but if i set the bounds on the draw string using the coordinates 0, 0 it draws it correctly.


For example, if i have my PdfSignature Bounds at 

{{X=15,Y=10,Width=100,Height=20}}

when i do 

signature.Appearance.Normal.Graphics.DrawString(visibleText.ToString(), font, PdfBrushes.Black, signature.Bounds);


it will draw the string at X=30, Y=20, but the signature field is at X=15, X=10.


It seems like in my code, PdfSignature draws the string using the coordinates specified, relative to the field, as in your example the DrawString method uses the Bounds specified to draw the string at the coordinates relative to the page.


Is there any way to control this?




GK Gowthamraj Kumar Syncfusion Team May 19, 2022 01:09 PM UTC

 Hi Cristian,

Thank you for your update. We are glad to know that your problem is resolved.

Yes. You can also draw the string on signature appearance with below bounds , it will display the text. In the appearance, its starts with the 0, 0 bound for x and y coordinates location. Please refer the below code,


//Adds certificate to the signature field

FileStream certificateStream = new FileStream(@"../../../PDF.pfx", FileMode.Open, FileAccess.Read);

 

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

 

//Creates a digital signature

 

PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature");

 

signature.Bounds = new RectangleF(0, 0, 100, 100);

 

signature.SignedName = signature.Certificate.SubjectName;

//Set the standard font.

 

PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

//Create appearance for the digital siganture

signature.Appearance.Normal.Graphics.DrawString(signature.Certificate.SubjectName, font, PdfBrushes.Black, new RectangleF(0, 0, signature.Bounds.Width, signature.Bounds.Height));

 


Please let us know if you need any further assistance in this.


Regards,

Gowthamraj K



Loader.
Up arrow icon