We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to sign an existing PDF that does not have a form

The documentation for Working with Digital Signatures describes how to sign an existing PDF with a form. 

PdfLoadedSignatureField field = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;

I am writing code to sign existing PDFs, but they do not have forms. (We were doing this previously with iTextSharp.) How can I do this with Syncfusion's PDF library? Here's what I have tried so far, but it does not seem to be producing signed PDFs:

        public static Stream DigitallyCertifyPdfStream(Stream uncertifiedFileStream, CertificationBundle certificationBundle)
        {
            var loadedDocument = new PdfLoadedDocument(uncertifiedFileStream);

            var page = loadedDocument.Pages[0] as PdfLoadedPage;

            var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid()}.pfx");
            File.WriteAllBytes(filePath, certificationBundle.ToBytes());
            var certificate = new PdfCertificate(filePath, certificationBundle.Password);

            // Signature
            var signature = new PdfSignature(loadedDocument, page, certificate, "Signature");

            // Encrypted, password protected PDF
            loadedDocument.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
            loadedDocument.Security.Algorithm = PdfEncryptionAlgorithm.AES;
            loadedDocument.Security.OwnerPassword = certificationBundle.Password;
            loadedDocument.Security.Permissions = PdfPermissionsFlags.EditAnnotations | PdfPermissionsFlags.Print;

            var digitallySignedPdf = new MemoryStream();
            loadedDocument.Save(digitallySignedPdf);

            loadedDocument.Close(true);

            return digitallySignedPdf;
        }

Thanks in advance!

13 Replies

SL Sowmiya Loganathan Syncfusion Team July 17, 2017 12:43 PM UTC

Hi Scott, 

We have written the code to add a digital signature to an existing document. Input document used in this code snippet is a blank document and it  does not have forms. Please find the code snippet below to sign existing PDFs. 

//Loads the PDF document with signature field 
 
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf"); 
 
            //Gets the page 
 
            PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; 
 
            PdfCertificate certificate = new PdfCertificate(@"PDF.pfx", "syncfusion"); 
 
 
            //Creates a signature field 
 
            PdfSignatureField signatureField = new PdfSignatureField(page, "Signaturefield"); 
 
            signatureField.Bounds = new RectangleF(100, 100, 100, 100); 
 
            var signature = new PdfSignature(loadedDocument, page, certificate, "Signature"); 
 
            signatureField.Signature = signature; 
 
            //Adds certificate to the signature field 
            signatureField.Signature.Certificate = certificate;            
 
            signatureField.BackColor = Color.Black; 
 
            signatureField.BorderWidth = 5; 
 
            //Adds the field 
 
            loadedDocument.Form.Fields.Add(signatureField); 


Please try the code snippet in your end and let us know if you need further assistance on this. 

Regards, 
Sowmiya L 



SC Scott July 17, 2017 06:36 PM UTC

Thanks Sowmiya. I have integrated your code. It does not throw errors, but Adobe does not indicate there is a signature, though Foxit does. Furthermore, Foxit shows there are two identical signatures. Here is my modified code:

public static Stream DigitallyCertifyPdfStream(Stream uncertifiedFileStream, CertificationBundle certificationBundle)
{
        var loadedDocument = new PdfLoadedDocument(uncertifiedFileStream);

        var page = loadedDocument.Pages[0] as PdfLoadedPage;

        var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid()}.pfx");
        File.WriteAllBytes(filePath, certificationBundle.ToBytes());
        var certificate = new PdfCertificate(filePath, certificationBundle.Password);

        // Signature
        var signature = new PdfSignature(loadedDocument, page, certificate, "Signature");
        var signatureField = new PdfSignatureField(page, "Signaturefield");
        signatureField.Bounds = new RectangleF(100, 100, 100, 100);
        signatureField.Signature = signature;
        signatureField.Signature.Certificate = certificate;
        signatureField.BackColor = Color.Black;
        signatureField.Visible = false;
        loadedDocument.Form.Fields.Add(signatureField);

        // Encrypted, password protected PDF
        loadedDocument.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
        loadedDocument.Security.Algorithm = PdfEncryptionAlgorithm.AES;
        loadedDocument.Security.OwnerPassword = certificationBundle.Password;
        loadedDocument.Security.Permissions = PdfPermissionsFlags.EditAnnotations | PdfPermissionsFlags.Print;

        var digitallySignedPdf = new MemoryStream();
        loadedDocument.Save(digitallySignedPdf);

        loadedDocument.Close(true);

        return digitallySignedPdf;
}

I have also examined the generated PDF file (attached) and found /Type /Sig entries. Why are these not being picked up on in Adobe Reader?


Attachment: BlankSigned_f68f3238.zip


PH Praveenkumar H Syncfusion Team July 18, 2017 03:41 PM UTC

Hi Scott, 

We have analyzed the given document and found that there is two signature fields added in the document, internally when we initialize the signature we are creating the signature field also, so we are suggesting to use only the below lines to create the signature in the existing PDF document. 

var signature = new PdfSignature(loadedDocument, page, certificate, "Signature"); 
//seeting bounds to the signature field 
signature.Bounds = new RectangleF(100, 100, 200, 200); 
//ceating the csutom apperence to the field. 
signature.Appearence.Normal.Graphics.DrawRectangle(PdfBrushes.Red,new RectangleF(0,0,200,200)); 

We have to set the bounds to the signature in order to make visible in the page, we can also create a customer appearance to the signature field, please find the detailed sample in the below link. 

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

Regards, 
Praveen 



SC Scott July 19, 2017 10:14 PM UTC

Thanks, I have changed my code so that there is just one signature, but it is still not being recognized/listed by Adobe Reader. I do see the signature in Foxit Reader. 

Here is a screenshot of Foxit showing the signature:


And here is a screenshot of the same PDF in Adobe Reader where it does not recognize the signature


I am not concerned about a visible signature on the page itself, just that the PDF readers recognize that it has been digitally signed.

Thanks for your help.



SL Sowmiya Loganathan Syncfusion Team July 21, 2017 12:10 PM UTC

Hi Scott, 

We have run the sample with latest version and there is no issue with that .We have able to see the signature field on the output document and PDF reader recognized that it has been digitally signed. 
Please try the sample in your side with latest version. 

Please find the output document from the below location : 

Please find the download link for latest version from the below location : 

Let us know if you need further assistance on this. 

Regards, 
Sowmiya L 



SC Scott August 2, 2017 02:50 PM UTC

Hey, thanks for looking into this. I see that the document you have provided does work, but I am still experiencing problems.


I posted a question on StackOverflow. I wonder if the answer may shed any light to you on what is happening.


https://stackoverflow.com/questions/45451470/pdf-shows-signature-in-foxit-reader-but-not-adobe-reader


Thanks for continuing to look into this.Scott H



SL Sowmiya Loganathan Syncfusion Team August 3, 2017 10:21 AM UTC

Hi Scott, 

We have tried with the sample to reproduce the issue which you have mentioned. We have already faced this kind of issue in our side with the older version. Now we have fixed this issue in our latest version.  

Please find the download link for latest version from the below location :  

Please find the sample from the below location :  

Please find the output document from the below location : 

Please try the sample in your side with the latest version and let us know if you need further assistance on this. 

Regards, 
Sowmiya L 



SC Scott August 3, 2017 05:06 PM UTC

Sowmiya, thank you for making a sample project. I ran your project and it did indeed work. However when I switched out Data/Input.pdf with this PDF (the one that is referenced in my StackOverflow question) it did not work. Any idea why?

Thanks for all your work in investigating.



SL Sowmiya Loganathan Syncfusion Team August 4, 2017 09:23 AM UTC

Hi Scott, 

We have tried to reproduce the issue which you have mentioned, with the input document provided by you. We regret to let you know that we could not reproduce the issue. The output PDF document shows signature in Adobe Reader, we are using the ES version 15.3.0.46. 

Please find the output document from the below location : 

Please try the sample in latest version and let us know the result. 

Regards, 
Sowmiya L 



SH Scott Hoelsema August 7, 2017 02:46 AM UTC

Hi, Sowmiya, thanks for continuing to look into it. I am not using Essential Studio, but am using the Nuget package, whose version is 14.1.0.46. It was last published over a year ago (see Nuget). Does its version number correspond with the Essential Studio version number? Should a publish be done of the Nuget package?

We prefer to use Visual Studio with the Nuget packages. If I downloaded Essential Studio, would I be able to copy the DLLs over and use those?



SL Sowmiya Loganathan Syncfusion Team August 7, 2017 12:43 PM UTC

Hi Scott, 

Thank you for the update. Nuget version number is correspond with the Essential Studio version number and the publish has been done with the Nuget package. 

Please find the below link to download the latest Nuget package :  

If you downloaded Essential Studio, it will be able to copy the DLLs. Please try the sample in your side with latest version and let us know if you need further assistance. 

Regards, 
Sowmiya L 



SH Scott Hoelsema August 8, 2017 07:17 PM UTC

Thanks! It works for me now that I am pointed to Syncfusion Nuget servers. You really should update your packages on nuget.org.

Scott Hoelsema



GR Ganesan Rengasamy Syncfusion Team August 9, 2017 12:05 PM UTC

Hi Scott,   
  
Thanks for the update.   
  
Syncfusion NuGet packages which are available in the common NuGet (nuget.org) server is not an official Syncfusion NuGet packages since we are not yet published the Syncfusion NuGet packages in public NuGet (nuget.org) server. Also, we can’t publish the Syncfusion NuGet packages based on platform wise in NuGet.org since its single global NuGet repository. Due to huge count of Syncfusion NuGet packages (Syncfusion supported platforms), it will be more complex to update the packages to nuget.org and pick the required NuGet package from nuget.org.    
  
Due to the above-mentioned reasons, we are maintaining the Syncfusion public NuGet server(nuget.syncfusion.com) with platform wise and anyone can utilize the Syncfusion NuGet packages with just configure the required platform NuGet feed links in their Visual Studio.    
  
We have documented the details to configure and utilize the Syncfusion NuGet packages in their application for each Syncfusion supported platforms user guide. Find the ASP.NET MVC NuGet configuration user guide below for your reference.    
  
  
Regards,   
Ganesan R. 


Loader.
Up arrow icon