Add Signature in PDF File
Add Digital Signature in a PDF File
PdfFileSignature class allows you to add signature in a PDF file. You need to create an object of PdfFileSignature class using input and output PDF files. You also need to create a Rectangle object at which you want to add the signature and in order to set appearance you can specify an image using SignatureAppearance property of the PdfFileSignature object. Aspose.Pdf.Facades also provides different kinds of signatures like PKCS#1, PKCS#7, and PKCS#7Detached. In order to create a signature of a specific type, you need to create an object of the particular class like PKCS1 , PKCS7 or PKCS7Detached using the certificate file and the password.
Once the object of a particular signature type is created, you can use the Sign method of the PdfFileSignature class to sign the PDF and pass the particular signature object to this class. You can also specify other attributes for this method. Finally, you need to save the signed PDF using Save method of the PdfFileSignature class. The following code snippet shows you how to add signature in a PDF file.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddPdfFileSignature()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
// Bind PDF document
pdFileSignature.BindPdf(dataDir + "input.pdf");
// Create a rectangle for signature location
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10, 10, 300, 50);
// Set signature appearance
pdFileSignature.SignatureAppearance = dataDir + "aspose-logo.png";
// Create any of the three signature types
var signature = new PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
pdFileSignature.Sign(1, "I'm document author", "test01@aspose-pdf-demo.local", "Aspose Pdf Demo, Australia", true, rect, signature);
// Save PDF document
pdFileSignature.Save(dataDir + "DigitallySign_out.pdf");
}
}
The following code example shows us the ability to sign a document with two signatures. In our example, we put the first signature on the first page, and the second on the second page. You can specify the pages that you need.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTwoSignature()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
// Bind PDF document
pdFileSignature.BindPdf(dataDir + "input.pdf");
// Create a rectangle for 1st signature location
System.Drawing.Rectangle rect1 = new System.Drawing.Rectangle(10, 10, 300, 50);
// Create 1st signature object
var signature1 = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
pdFileSignature.Sign(1, "I'm document author", "test@aspose-pdf-demo.local", "Aspose Pdf Demo, Australia", true, rect1, signature1);
pdFileSignature.Save(dataDir + "DigitallySign_out.pdf");
// Sign with 2nd signature
// Bind PDF document
pdFileSignature.BindPdf(dataDir + "DigitallySign_out.pdf");
// Create a rectangle for 2nd signature location
System.Drawing.Rectangle rect2 = new System.Drawing.Rectangle(10, 10, 300, 50);
// Create 2nd signature object
var signature2 = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
pdFileSignature.Sign(2, "I'm document reviewer", "test02@aspose-pdf-demo.local", "Aspose Pdf Demo, Australia", true, rect2, signature2);
// Save PDF document
pdFileSignature.Save(dataDir + "DigitallySign2_out.pdf");
}
}
For a document with forms or acroforms that needs to be signed, see the following example. You need to create an object of PdfFileSignature class using input and output PDF files. Use BindPdf for binding. Create a signature with the ability to add the required properties. In our example they are ‘Reason’ and ‘CustomAppearance’.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddPdfFileSignatureField()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
// Bind PDF document
pdFileSignature.BindPdf(dataDir + "input.pdf");
// Create any of the three signature types
var signature = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345")
{
Reason = "Sign as Author",
CustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
{
FontSize = 6,
FontFamilyName = "Calibri"
}
}; // PKCS#1
pdFileSignature.Sign("Signature1", signature);
// Save PDF document
pdFileSignature.Save(dataDir + "DigitallySign_out.pdf");
}
}
If our document has two fields, the algorithm for signing it is similar to the first example.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddPdfFileSignatureField2()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
// Bind PDF document
pdfFileSignature.BindPdf(dataDir + "input.pdf");
// Create any of the three signature types
var signature1 = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345")
{
Reason = "Sign as Author",
CustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
{
FontSize = 6
}
}; // PKCS#1
pdFileSignature.Sign("Signature1", signature1);
// Save PDF document
pdFileSignature.Save(dataDir + "DigitallySign_out.pdf");
// Bind PDF document
pdFileSignature.BindPdf(dataDir + "DigitallySign_out.pdf");
// Create any of the three signature types
var signature2 = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345")
{
Reason = "Sign as Reviwer",
CustomAppearance = new SignatureCustomAppearance
{
FontSize = 6
}
}; // PKCS#1
pdFileSignature.Sign("Signature2", signature2);
// Save PDF document
pdFileSignature.Save(dataDir + "DigitallySign2_out.pdf");
}
}