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

Ven*_*enu 3 itext

签署pdf时我收到以下错误.错误是"签名已定义.必须在PdfSignatureAppearance中关闭."

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

我在下面的行中收到错误

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

请在下面找到代码

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);
}
Run Code Online (Sandbox Code Playgroud)

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

mkl*_*mkl 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");
Run Code Online (Sandbox Code Playgroud)

不幸的是,由于该指令,在该行中发生的实际错误被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;
}
Run Code Online (Sandbox Code Playgroud)

明显的原因是

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

当你描述你的情况时

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

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

追加模式

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

PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, fout, '\0', true);
Run Code Online (Sandbox Code Playgroud)

参看 该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)
Run Code Online (Sandbox Code Playgroud)