Sign a PDF using an already signed template

Hello, Is it possible to sign a list of PDFs using an already signed PDF template?

@CQcesar

Yes, it is possible to sign a list of PDFs using an already signed PDF template with Aspose.PDF for .NET. You can achieve this by utilizing the PdfFileSignature class to bind the template and then apply the signature to each PDF document.

Here’s a general approach you can follow:

  1. Load the Signed Template: First, load your already signed PDF template using the Document class.
  2. Create a PdfFileSignature Object: This object will allow you to manage the signing process.
  3. Define Signature Appearance: You can set the appearance of the signature, such as an image or text.
  4. Sign Each PDF: Loop through your list of PDFs, binding each one to the PdfFileSignature object and applying the signature.

Here’s a sample code snippet to illustrate this process:

using System;
using Aspose.Pdf;
using Aspose.Pdf.Facades;

namespace PDFSigningExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Path to the signed template
            string templatePath = "path/to/signedTemplate.pdf";
            // List of PDFs to sign
            string[] pdfFiles = { "path/to/pdf1.pdf", "path/to/pdf2.pdf" };

            // Load the signed template
            Document templateDoc = new Document(templatePath);
            PdfFileSignature pdfSign = new PdfFileSignature(templateDoc);

            // Set signature appearance
            pdfSign.SignatureAppearance = "path/to/signatureImage.jpg";

            foreach (var pdfFile in pdfFiles)
            {
                // Load the PDF to be signed
                Document docToSign = new Document(pdfFile);
                
                // Define the signature rectangle
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
                
                // Sign the document
                pdfSign.Sign(1, "Signature Reason", "Signer Name", "Location", true, rect, new PKCS1("path/to/certificate.pfx", "password"));
                
                // Save the signed document
                pdfSign.Save("path/to/signed_" + System.IO.Path.GetFileName(pdfFile));
            }

            // Clean up
            pdfSign.Dispose();
        }
    }
}

In this example, replace the paths with your actual file locations. The PKCS1 class is used to specify the certificate for signing, and you can adjust the signature rectangle and appearance as needed.

For more detailed information on signing PDFs, you can refer to the official documentation on digitally signing PDFs with Aspose.PDF for .NET here.

If you have any further questions or need additional assistance, feel free to ask!

Sources:
[1]: Add digital signature or digitally sign PDF in C#

It seems you are signing the template and not the list of PDFs. And the code is not reusing the certificate of the template.

@CQcesar

Do you want to extract signatures/certificate file and re-use it to sign other PDF documents?

Yes, is that possible?

@CQcesar

Here is the code to extract signatures from an existing PDF:

var extractedSignInfos = new List<ExtractedSignInfo>();

// Get Sign Info
using (var pdfSignature = new PdfFileSignature())
{
    var doc = new Document(inputFile);
    pdfSignature.BindPdf(doc);
    foreach (var signName in pdfSignature.GetSignNames())
    {
        var field = doc.Form[signName] as SignatureField;
        var exi = new ExtractedSignInfo();

        exi.Page = field.PageIndex;
        exi.Reason = pdfSignature.GetReason(signName);
        exi.Contact = pdfSignature.GetContactInfo(signName);
        exi.Location = pdfSignature.GetLocation(signName);
        exi.Rect = exi.Rect;

        extractedSignInfos.Add(exi);
    }
}

Then you can add extracted signatures like below:

// Add Signatures
foreach (var exi in extractedSignInfos)
{
    using (var pdfSignature = new PdfFileSignature())
    {
        pdfSignature.BindPdf(resultFile);
        var pkcs = new PKCS7Detached(yourPdf, "111111");
        pdfSignature.Sign(exi.Page, exi.Reason, exi.Contact, exi.Location, true, exi.Rect, pkcs);
        pdfSignature.Save(resultFile);
    }
}

Thank you for the code, Asad. I cannot find the assembly for ExtractedSignInfo. Can you share the assembly used for it, please?

@CQcesar

Please allow us to check our logs and get back to you with the requested information.

@CQcesar

Below is the definition of the Class in question:

private class ExtractedSignInfo
{
  public int Page { get; set; }
  public string Reason { get; set; }
  public string Contact { get; set; }
  public string Location { get; set; }
  public Aspose.Pdf.Rectangle Rect { get; set; }
}