Extract Image and Signature Information
The following code snippet also work with Aspose.PDF.Drawing library.
Extracting Image from Signature Field
Aspose.PDF for .NET supports the feature to digitally sign the PDF files using the SignatureField 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 SignatureField class. Please take a look at the following code snippet which demonstrates the steps to extract an image from the SignatureField object:
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
string input = dataDir + @"ExtractingImage.pdf";
using (Document document = new Document(input))
{
foreach (Field field in document.Form)
{
SignatureField sf = field as SignatureField;
if (sf != null)
{
string outFile = dataDir + @"output_out.jpg";
using (Stream imageStream = sf.ExtractImage())
{
if (imageStream != null)
{
using (System.Drawing.Image image = Bitmap.FromStream(imageStream))
{
image.Save(outFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
}
Replace signature Image
Sometimes you may have a requirement to only replace the image of an already present signature field inside PDF file. In order to accomplish this requirement, first, we need to search form fields inside the PDF file, identify Signature fields, get the dimensions (Rectangular dimensions) of the signature field and then stamp an image over the same dimensions.
Extract Signature Information
Aspose.PDF for .NET supports the feature to digitally sign the PDF files using the SignatureField class. Currently, we can also determine the validity of the certificate but we cannot extract the whole certificate. The information which can be extracted is a public key, thumbprint, issuer, etc.
To extract signature information, we have introduced the ExtractCertificate method to the SignatureField class. Please take a look at the following code snippet which demonstrates the steps to extract the certificate from SignatureField object:
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
string input = dataDir + "ExtractSignatureInfo.pdf";
using (Document document = new Document(input))
{
foreach (Field field in document.Form)
{
SignatureField sf = field as SignatureField;
if (sf != null)
{
Stream cerStream = sf.ExtractCertificate();
if (cerStream != null)
{
using (cerStream)
{
byte[] bytes = new byte[cerStream.Length];
using (FileStream fs = new FileStream(dataDir + @"input.cer", FileMode.CreateNew))
{
cerStream.Read(bytes, 0, bytes.Length);
fs.Write(bytes, 0, bytes.Length);
}
}
}
}
}
}
You can get information about document signature algorithms.
using (Document document = new Document(inputFilePath))
{
using (PdfFileSignature signature = new PdfFileSignature(document))
{
var sigNames = signature.GetSignNames();
List<SignatureAlgorithmInfo> signaturesInfoList = signature.GetSignaturesInfo();
foreach (var sigInfo in signaturesInfoList)
{
Console.WriteLine(sigInfo.DigestHashAlgorithm);
Console.WriteLine(sigInfo.AlgorithmType);
Console.WriteLine(sigInfo.CryptographicStandard);
Console.WriteLine(sigInfo.SignatureName);
}
}
}
Sample output for the example above:
Sha256
Rsa
Pkcs7
Signature1