Sara
Sara

Reputation: 99

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!

Upvotes: 1

Views: 1751

Answers (2)

Ram
Ram

Reputation: 162

I know This is a very old question and it was answered by @mki, and the answer is accepted.

I am going to post the answer for anyone who requires it nowadays.

Recently I customized the signature displaying appearance, and works for me.

sap.Layer2Text = "Digitally signed by " + PdfPKCS7.GetSubjectFields(chain(0)).GetField("CN") + Constants.vbCrLf + "Designation: President & Head – Cards & Payments" + Constants.vbCrLf + "Date: " + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss zzz", CultureInfo.InvariantCulture) + Constants.vbCrLf + "Entity: Pulkitsoft";

Note: sap is a PdfSignatureAppearance

Upvotes: 0

mkl
mkl

Reputation: 95888

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:

The structure of an example signed 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.

Upvotes: 1

Related Questions