Quantcast
Channel: PDF tips & tricks
Viewing all articles
Browse latest Browse all 125

How to sign the already signed PDF document

$
0
0

Introduction


Digital signatures provide a reliable way to sign documents and verify the identity of the signing person(s). They are used nowadays in many areas of our everyday workflow, and, when it comes to PDF, we luckily have the support for them built-in into the standard.

We have already covered the PDF signing and validating topic in one of our earlier posts (you can read it here), but it was related to signing the document with one or several signatures at once.


In this article we’ll cover another interesting scenario where signing will be performed for previously signed document.  “What is the difference?” - you may ask. Sequential signing requires different handling and implementation because as the document changes, previous signatures may become invalid. But the Apitron PDF Kitfor .NET component can handle any signing task and we’ll demonstrate it in code section.

Code sample


The code below adds two digital signatures to PDF document sequentially. This way you can add as many signatures as you want.

classProgram
{
    privatestaticvoid Sign(string pathToDocument, string pathToCertificate,
       string password, stringpathToSignatureImage, Boundary signatureViewLocation)
    {           
        // open existing document andd sign once
        using(Stream inputStream= newFileStream(pathToDocument, FileMode.Open,
            FileAccess.ReadWrite))
        {
            using (FixedDocument doc = newFixedDocument(inputStream))
            {
                string imageResourceId = Guid.NewGuid().ToString("N");
                string signatureFieldId = Guid.NewGuid().ToString("N");

                // register signature image resource
                doc.ResourceManager.RegisterResource(newImage(imageResourceId,
                    pathToSignatureImage));

                // create first signature field and initialize it using a stored certificate
                SignatureField signatureField = newSignatureField(signatureFieldId);
                using (Stream signatureDataStream = File.OpenRead(pathToCertificate))
                {
                    signatureField.Signature = Signature.Create(
                       newPkcs12Store(signatureDataStream,password));
                }

                // add signature fields to the document
                doc.AcroForm.Fields.Add(signatureField);

                // create first signature view using the image resource
                SignatureFieldView signatureView =
newSignatureFieldView(signatureField, signatureViewLocation);
                signatureView.ViewSettings.Graphic = Graphic.Image;
                signatureView.ViewSettings.GraphicResourceID = imageResourceId;
                signatureView.ViewSettings.Description = Description.None;

                // add views to page annotations collection
                doc.Pages[0].Annotations.Add(signatureView);

                // save as incremental update
                doc.Save();
            }
        }
    }

    staticvoid Main(string[] args)
    {
        string fileName = "signedTwice.pdf";
        CreatePDFDocument(fileName);

        // save once and save
        Sign(fileName, "../../data/certs/JohnDoe.pfx", "password",
 "../../data/images/signatureImage.png"newBoundary(10,750,110,800));

        // add second signature to the already signed doc and save
        Sign(fileName,
            "../../data/certs/JaneDoe.pfx", "password",
             "../../data/images/signatureImageJane.png"
             newBoundary(120, 750, 220, 800));                      
    }

    privatestaticvoid CreatePDFDocument(string fileName)
    {
        using (Stream stream = File.Create(fileName))
        {
            FlowDocument doc = newFlowDocument(){Margin = newThickness(10)};
            doc.Add(newTextBlock("Signed using Apitron PDF Kit for .NET"));
            doc.Write(stream,newResourceManager());
        }
    }               
}


The resulting file looks as follows:

Pic. 1 Signed PDF document

Pic. 1 Signed PDF document

Conclusion


Apitron PDF Kit handles multiple signing scenarios and can be used to create digitally signed documents in corporate, government and end user environments. You can download it from our websiteand try for yourself. Whenever you have a question you can read our free book, browse online API documentation and articles (see documentation page). Contact us if you need any help and we’ll be happy to assist you.

Viewing all articles
Browse latest Browse all 125

Trending Articles