I am trying to work on a solution where I operate on a PDF in-memory for adding a digital signature. When I save the signed pdf to disk, it all looks good. However, if I save to a stream and then persist the stream to disk, the certificate is invalid. Following is the slightly altered code from your documentation:
Here, inputPath is the pdf file. OutputPath is the fully qualified desired pdf file path. CertPath is the certificate which is trusted and has no password. Logo is the image.
void SignPDF(string inputPath, string outputPath, string certPath, string logo)
{
var outputStream = new MemoryStream();
var inputStream = new MemoryStream(File.ReadAllBytes(inputPath));
using (PdfFileSignature signature = new PdfFileSignature())
{
signature.BindPdf(inputStream);
PKCS7 pkcs = new PKCS7(certPath, ""); // Use PKCS7/PKCS7Detached objects
DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.NoChanges);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
// Set signature appearance
signature.SignatureAppearance = logo;
// Create any of the three signature types
signature.Certify(1, null,null,null, true, rect, docMdpSignature);
// Save output PDF file
//signature.Save(outputPath) //works fine;
signature.Save(outputStream);
var doc = new Document(outputStream);
doc.Save(outputPath);
}