Working with Signature in PDF File
How to Extract Signature Information
Aspose.PDF for .NET supports the feature to digitally sign PDF files using the PdfFileSignature class. Currently, it is also possible to determine the validity of a certificate but we cannot extract the whole certificate. The information that can be extracted is the public key, thumbprint, and issuer, etc.
To extract signature information, we have introduced the ExtractCertificate(..) method to the PdfFileSignature class. Please take a look at the following code snippet which demonstrates the steps to extract certificate from the PdfFileSignature object:
// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractSignatureInfo()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
string input = dataDir + "signed_rsa.pdf";
string certificateFileName = "extracted_cert.pfx";
using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
pdfFileSignature.BindPdf(input);
// Get list of signature names
var sigNames = pdfFileSignature.GetSignatureNames();
if (sigNames.Count > 0)
{
SignatureName sigName = sigNames[0];
// Extract signature certificate
Stream cerStream = pdfFileSignature.ExtractCertificate(sigName);
if (cerStream != null)
{
using (cerStream)
{
using (FileStream fs = new FileStream(dataDir + certificateFileName, FileMode.CreateNew))
{
cerStream.CopyTo(fs);
}
}
}
}
}
}
Extracting Image from Signature Field (PdfFileSignature)
Aspose.PDF for .NET supports the feature to digitally sign the PDF files using the PdfFileSignature class and while signing the document, you can also set an image for SignatureAppearance. Now this API also provides the capability to extract signature information as well as the image associated with the signature field.
In order to extract signature information, we have introduced the ExtractImage(..) method to the PdfFileSignature class. Please take a look at the following code snippet which demonstrates the steps to extract image from the PdfFileSignature object:
// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractSignatureImage()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var signature = new Aspose.Pdf.Facades.PdfFileSignature())
{
signature.BindPdf(dataDir + "ExtractingImage.pdf");
if (signature.ContainsSignature())
{
// Get list of signature names
foreach (string sigName in signature.GetSignatureNames())
{
// Extract signature image
using (Stream imageStream = signature.ExtractImage(sigName))
{
if (imageStream != null)
{
imageStream.Position = 0;
using (FileStream fs = new FileStream(dataDir + "ExtractImages_out.jpg", FileMode.OpenOrCreate))
{
imageStream.CopyTo(fs);
}
}
}
}
}
}
}
Suppress Location and Reason
Aspose.PDF functionality allows flexible configuration for digital sign instance. PdfFileSignatureclass provides ability sign PDF file. Sign method implementation allows to sign the PDF and pass the particular signature object to this class. Sign method contains set of attributes for the customization of output digital sing. In case if you need to suppress some text attributes from result sing you can leave them empty. The following code snippet demonstrate how to suppress Location and Reason two rows from signature block:
// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SupressLocationReason()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
pdfFileSignature.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
pdfFileSignature.SignatureAppearance = dataDir + "aspose-logo.png";
// Create any of the three signature types
var signature = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
pdfFileSignature.Sign(1, string.Empty, "test01@aspose-pdf-demo.local", string.Empty, true, rect, signature);
// Save the document
pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
}
}
Customization Features for Digital Sign
Aspose.PDF for .NET allows customization features for a digital sign. The Sign method of class SignatureCustomAppearanceimplements with 6 overloads for your comfortable usage. For example, you can configure result sign only by SignatureCustomAppearance class instance and its properties values. The following code snippet demonstrates how to hide “Digitally signed by” caption from output digital sign of your PDF.
// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomizationFeaturesForDigitalSign()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
pdfFileSignature.BindPdf(dataDir + "input.pdf");
// Create a rectangle for signature location
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10, 10, 300, 50);
// Create any of the three signature types
var signature = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
// Create signature appearance
var signatureCustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
{
FontSize = 6,
FontFamilyName = "Times New Roman",
DigitalSignedLabel = "Signed by:"
};
// Set signature appearance
signature.CustomAppearance = signatureCustomAppearance;
pdfFileSignature.Sign(1, true, rect, signature);
// Save the document
pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
}
}
Change Language In Digital Sign Text
Using Aspose.PDF for .NET API, you can sign a PDF file using any of the following three types of signatures:
- PKCS#1.
- PKCS#7.
- PKCS#12.
Each of provided signatures contains a set of configuration properties implemented for your convenience(localization, date time format, font family etc). Class SignatureCustomAppearance provides corresponding functionality. The following code snippet demonstrates how to change language in digital sign text:
// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ChangeLanguageInDigitalSignText()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
{
pdfFileSignature.BindPdf(dataDir + "input.pdf");
// Create a rectangle for signature location
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(310, 45, 200, 50);
// Create any of the three signature types
var pkcs = new Aspose.Pdf.Forms.PKCS7(dataDir + "rsa_cert.pfx", "12345")
{
Reason = "Pruebas Firma",
ContactInfo = "Contacto Pruebas",
Location = "Población (Provincia)",
Date = DateTime.Now
};
var signatureCustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
{
DateSignedAtLabel = "Fecha",
DigitalSignedLabel = "Digitalmente firmado por",
ReasonLabel = "Razón",
LocationLabel = "Localización",
FontFamilyName = "Arial",
FontSize = 10d,
Culture = System.Globalization.CultureInfo.InvariantCulture,
DateTimeFormat = "yyyy.MM.dd HH:mm:ss"
};
// Set signature appearance
pkcs.CustomAppearance = signatureCustomAppearance;
// Sign the PDF file
pdfFileSignature.Sign(1, true, rect, pkcs);
// Save the document
pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
}
}