签名定义。必须在PdfSignatureAppearance中关闭

时间:2015-03-18 06:44:11

标签: itext

签署pdf时出现以下错误。错误是 “签名定义。必须在PdfSignatureAppearance中关闭。“

我第一次能够签署pdf。它在输出文件夹中创建一个pdf文件,并在第一页中显示签名。到目前为止代码工作正常。 现在,当我将最近生成的文件作为输入来登录第二页时,我收到错误“Signature defined。必须在PdfSignatureAppearance中关闭。“

我在下面的行中收到错误

appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");

请找到以下代码

if (File.Exists(fName))
{
    PdfReader.unethicalreading = true;
    using (PdfReader pdfReader = new PdfReader(fName))
    {
        //file name
       fName = fName.Substring(fName.LastIndexOf("\\") + 1);
        outputFile = outputFolder + fName + ".pdf";
        if (!File.Exists(outputFile))
        {
            using (FileStream fout = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite))
            {
                using (PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, fout, '\0'))
                {
                    PdfSignatureAppearance appearance = stamper.SignatureAppearance;
                    string imagePath = txtImage.Text;
                    iTextSharp.text.Image signatureFieldImage = iTextSharp.text.Image.GetInstance(imagePath);
                    appearance.SignatureGraphic = signatureFieldImage;

                    signatureFieldImage.SetAbsolutePosition(250, 50);
                    stamper.GetOverContent(pageNo).AddImage(signatureFieldImage);

                    appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");
                    appearance.Reason = txtReason.Text;

                    IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
                    MakeSignature.SignDetached(appearance, es, new X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);
                    stamper.Close();
                }
            }
        }
    }
    this.Invoke(new BarDelegate(UpdateBar), fName);
}

有人可以帮助我,如果需要更多细节,请告诉我。

1 个答案:

答案 0 :(得分:7)

OP代码中存在多个问题:

正确的关闭电话

应用签名时,不得关闭压模对象本身,而应关闭签名外观对象。如果使用诸如MakeSignature.SignDetached之类的辅助方法,则甚至不必对该结算进行编码,因为SignDetached隐式已经在最后一行中关闭了appearance

因此,请

  • 删除stamper.Close()
  • 不要将PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, fout, '\0')放入using指令中,因为这会导致调用压模的Dispose方法,而后者又调用Close

通常你不受这些行的伤害,因为在MakeSignature.SignDetached中隐式出现关闭后,将忽略更多的近距离调用。

但是,如果你没有那么远,例如,由于之前的某些错误情况,这种紧密调用会导致您观察到的错误,在您的情况下由using指令引起的紧密调用。

SetVisibleSignature

中的问题

您收到

中的错误
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");

不幸的是,由于Close指令导致Dispose调用期间using调用导致的错误导致此行中发生的实际错误被替换。

考虑消息代码:

/**
 * Sets the signature to be visible. It creates a new visible signature field.
 * @param pageRect the position and dimension of the field in the page
 * @param page the page to place the field. The fist page is 1
 * @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name
 */
virtual public void SetVisibleSignature(Rectangle pageRect, int page, String fieldName) {
    if (fieldName != null) {
        if (fieldName.IndexOf('.') >= 0)
            throw new ArgumentException(MessageLocalization.GetComposedMessage("field.names.cannot.contain.a.dot"));
        AcroFields af = writer.GetAcroFields();
        AcroFields.Item item = af.GetFieldItem(fieldName);
        if (item != null)
            throw new ArgumentException(MessageLocalization.GetComposedMessage("the.field.1.already.exists", fieldName));
        this.fieldName = fieldName;
    }
    if (page < 1 || page > writer.reader.NumberOfPages)
        throw new ArgumentException(MessageLocalization.GetComposedMessage("invalid.page.number.1", page));
    this.pageRect = new Rectangle(pageRect);
    this.pageRect.Normalize();
    rect = new Rectangle(this.pageRect.Width, this.pageRect.Height);
    this.page = page;
}

明显的原因是

  • 包含点的字段名称
  • 文档中已存在的命名字段,或
  • 无效的页码。

正如您描述的情况

  

我第一次能够签署pdf。它在输出文件夹中创建一个pdf文件,并在第一页中显示签名。到目前为止代码工作正常。现在,当我将最近生成的文件作为输入来登录第二页时,我收到错误

我假设第二项是最可能的原因:如果要将多个签名添加到同一文档,其字段名称必须不同。

追加模式

当您指示将多个签名应用于同一文件时,必须使用追加模式。如果您不这样做,您将使之前的签名无效:

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

比照。 CreateSignature方法超载评论

/**
 * Applies a digital signature to a document, possibly as a new revision, making
 * possible multiple signatures. The returned PdfStamper
 * can be used normally as the signature is only applied when closing.
 * <p>
... (outdated Java example code) ...
 * @param reader the original document
 * @param os the output stream or <CODE>null</CODE> to keep the document in the temporary file
 * @param pdfVersion the new pdf version or '\0' to keep the same version as the original
 * document
 * @param tempFile location of the temporary file. If it's a directory a temporary file will be created there.
 *     If it's a file it will be used directly. The file will be deleted on exit unless <CODE>os</CODE> is null.
 *     In that case the document can be retrieved directly from the temporary file. If it's <CODE>null</CODE>
 *     no temporary file will be created and memory will be used
 * @param append if <CODE>true</CODE> the signature and all the other content will be added as a
 * new revision thus not invalidating existing signatures
 * @return a <CODE>PdfStamper</CODE>
 * @throws DocumentException on error
 * @throws IOException on error
 */
public static PdfStamper CreateSignature(PdfReader reader, Stream os, char pdfVersion, string tempFile, bool append)