在C#中使用iTextSharp在PDF中添加多个数字签名 [英] Add multiple Digital Signature in PDF using iTextSharp in C#

查看:728
本文介绍了在C#中使用iTextSharp在PDF中添加多个数字签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用iTextSharp Dll实现了数字签名,以使用单个签名对PDF文件进行签名.现在,我想在以前或已经过数字签名的PDF中添加另一个数字签名,并且在验证一个签名时遇到错误.

I have implemented Digital Signature using iTextSharp Dll to sign PDF files with a single signature. Now, I want to add another digital signature in previously or already digitally signed PDF and I’m getting an error when verifying one signature.

如何在一个PDF中添加多个数字签名并验证所有签名.

How can I add multiple Digital Signature in one PDF and verify all signatures.

我正在使用以下代码:

PdfReader reader = new PdfReader(fileName);
using (FileStream fout = new FileStream(SignedFileName, FileMode.Create, FileAccess.ReadWrite))
{
    // appearance
    PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0');
    PdfSignatureAppearance appearance = stamper.SignatureAppearance;
    //appearance.Reason = SignReason;
    //appearance.Location = SignLocation;
    appearance.SignDate = DateTime.Now.Date;
    appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(xPos, yPos, xPos + 200, yPos + 100), PageNo, null);//.IsInvisible

    // Custom text and background image
    appearance.Image = iTextSharp.text.Image.GetInstance(SignatureImg);
    appearance.ImageScale = 0.6f;
    appearance.Image.Alignment = 300;
    appearance.Acro6Layers = true;

    StringBuilder buf = new StringBuilder();
    buf.Append("Digitally Signed by ");
    String name = SignerName;

    buf.Append(name).Append('\n');
    buf.Append("Date: ").Append(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss zzz"));

    string text = buf.ToString();

    appearance.Layer2Text = text;

    //digital signature
    IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
    MakeSignature.SignDetached(appearance, es, new Org.BouncyCastle.X509.X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);

    stamper.Close();

}

推荐答案

此行中的错误:

PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0');

将其更改为:

PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0', true);

说明:您不是在附加模式下签署文档.

The explanation: you are not signing the document in append mode.

在进一步检查您的代码时,我发现您还在添加图像.那可能很棘手.以追加模式添加新签名解决了一个问题.添加额外的内容可能会导致额外的问题,具体取决于您所使用的iText版本.

On further inspection of your code, I see that you're also adding an image. That can be tricky. Adding the new signature in append mode solves one problem. Adding that extra content could cause an extra problem depending on the version of iText you are using.

这篇关于在C#中使用iTextSharp在PDF中添加多个数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆