I am attempting to digitally sign a pdf that has been loaded as an Aspose.pdf.Document and been filled by our program. We are using Aspose.PDF for .NET for this process.
The entire process is loading a certificate from the Local Machine store, creating the PdfFileSignature object passing the filled document in the constructor, creating a pfx stream from the loaded X509 certificate, passing creating the PKCS7 Signature object, creating the DocMDPSignature with NoChanges then using the PdfFileSignature.Certify method to sign and the PdfFileSignature.Save method to save the resulting signed file to a MemoryStream.
When I reset that MemoryStream’s position and save it to a file, I receive an output file that is the correct form and it is signed however the data that was previously filled is not present in the form. If I use the PdfFileSignature.Certify method and save the Aspose.Pdf.Document object separately I get the data filled but no signature.
public void SignAndLockPDF(Stream outputStream)
{
X509Certificate2 iisCert = GetCertificateFromStore(_certName, StoreLocation.LocalMachine);
using (PdfFileSignature signature = new PdfFileSignature(_document))
{
using (MemoryStream certStream = new MemoryStream(iisCert.Export(X509ContentType.Pfx, "tempPass")))
{
var pkcs7Sig = new PKCS7(certStream, "tempPass");
DocMDPSignature pdfSignature = new DocMDPSignature(pkcs7Sig, DocMDPAccessPermissions.NoChanges);
signature.Certify(1, "Application Completed", "UA Membership", "UA", false, new System.Drawing.Rectangle(1, 1, 1, 1), pdfSignature);
signature.Save(outputStream);
}
}
}