我正在尝试使用对 PDF 文件进行数字签名 以项目为例。

当它执行 st.Close(); 时我收到臭名昭著的“对象引用未设置为对象的实例”。

我不知道为什么会发生这种情况, st 是一个对象的实例,上面使用了它。这个异常发生在 .Close() 内部,由于我没有源代码,我只能尝试猜测为什么......

我只能说:

  1. 正在成功读取原始 PDF。
  2. 正在生成一个空的 pdf(我猜 .Close() 会写入该文件)。
  3. 我的证书正在加载,但我不确定这是正确的方法。

这是我加载证书的方式:

private void processCert(X509Certificate2 card)
{
    X509CertificateParser cp = new org.bouncycastle.x509.X509CertificateParser(card.RawData);
    chain = new org.bouncycastle.x509.X509Certificate[] { cp.ReadCertificate() };
}

这就是我尝试签署 PDF 的方式。

public string Sign(string SigReason, string SigContact, string SigLocation, bool visible)
{
    string bugLog ="";
    try
    {
        PdfReader reader = new PdfReader(this.inputPDF);

        //Activate MultiSignatures
        PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);

        FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0'); 

        st.MoreInfo = this.metadata.getMetaData();

        st.XmpMetadata = this.metadata.getStreamedMetaData();

        PdfSignatureAppearance sap = st.SignatureAppearance;

        sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.WINCER_SIGNED);

        sap.Reason = SigReason;
        sap.Contact = SigContact;
        sap.Location = SigLocation;

        if (visible)
           sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);

        st.Close();
    }
    catch (Exception e)
    {
        bugLog += " ERROR Sign: " + e.Message;
    }

    return buglog;
  }

有谁知道为什么我会遇到这个异常,或者我应该做什么来克服这个问题?

iTextSharp 文档没有太大帮助......

有帮助吗?

解决方案

我现在已经成功地完成了这项工作:)对此示例进行一些细微的更改就达到了目的;)

http://itextpdf.sourceforge.net/howtosign.html#signextitextsharp2

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top