简体   繁体   中英

How to show digital signature VALUE in signature appearance of PDF file with iTextSharp

Is it possible to append Digital signature VALUE in signature appearance while signing document, because I am using this part of code

PdfFilename = txtFile.Text + "\\" + (string)chkLista.Items[i];
DestPdfFilename = txtFile.Text + "\\" + (string)chkLista.Items[i] + "-signed.pdf";

Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-256");
PdfReader pdfReader = new PdfReader(PdfFilename);
FileStream signedPdf = new FileStream(DestPdfFilename, FileMode.Create);  //the output pdf file
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;

signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(436, 700, 564, 750), 1, "sig");

MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);

but in PDF it shows only the rectangle filled only with data like: Digitally signed by:... and Date:...

Except this I want to be showed digital signature Value in Base64 too!

If I correctly understand the OP, the task is to display a base64 representation of the actual signature value bytes as the signature field appearance.

This is not possible.

The reason is that the appearance of the signature field is part of the signed byte range:

示例签名PDF的结构

(The appearance is defined somewhere in the first or third section in the example.)

Therefore, the appearance must be known before the signature value is created. Thus, the value cannot completely be represented in the field appearance.

For more backgrounds read this answer .

That been said, you might try to cheat a bit by adding JavaScript to the PDF which at display time changes the appearance of the signature field. This approach has two drawbacks, though:

  • Not all PDF viewers support JavaScript.
  • Adobe Reader and Acrobat, the main PDF viewers also supporting JavaScript, would recognize this change of the appearance and indicate that the signature is broken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM