签名字段在签名后保持未签名状态

Signature field remain unsigned after the signing

我正在使用 iText7 库执行签名。我尝试在文档上可用的签名字段之一上签名,但在签名后,它仍然显示该字段未签名。以下是我用于签名的代码片段。

String fieldName = "signature1";
PdfAcroForm pdfAcroForm = PdfAcroForm.getAcroForm(pdfDocument, false);
PdfArray pdfArray = pdfAcroForm.getField(fieldName).getWidgets().get(0).getRectangle();
Rectangle rect = pdfArray.toRectangle();
PdfSignatureAppearance appearance = pdfSigner.getSignatureAppearance().setReason("Test")
    .setLocation("Test").setReuseAppearance(false);
appearance.setPageRect(rect).setPageNumber(1);
IExternalSignature pks =
     new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName());
pdfSigner.signDetached(new BouncyCastleDigest(), pks, chain, null, null, null, 0, CryptoStandard.CMS);

original pdf &signed pdf

已更新

对于以下 pdf,尽管我按照响应中所述对字段进行了签名,但我没有在文档中看到签名;但是,为上述早期共享的 pdf 工作。

pdfSigner.setFieldName(fieldName);

original pdf &signed pdf

原刊

您忘记告诉 PdfSigner 签署现有表单字段之一,因此它会创建一个新字段并签署该字段。

要告诉 iText 使用特定字段,请使用 PdfSigner.setFieldName:

pdfSigner.setFieldName(fieldName);

根据其 JavaDocs 这个方法

Sets the name indicating the field to be signed. The field can already be presented in the document but shall not be signed. If the field is not presented in the document, it will be created.

(我猜“presented”应该是“present”……)

一旦你这样做,就没有必要

appearance.setPageRect(rect).setPageNumber(1);

不再使用现有表单域的矩形和页码。

FORM FDA 1571 的问题

在更新中,您共享了一个文件,尽管进行了上述更正,但仍未显示签名外观,即 FDA 1571 表格。

原因很简单:该文档中的签名字段在签名前后都是不可见的!

before signing after signing

因此,您没有看到签名外观是完全正确的。

如果您想要一个可见的签名外观,您必须确保签名字段是可见的。

在很多情况下签名外观是不可见的,特别是:

  • 签名小部件未被任何页面引用。
  • 签名小部件的 Rect 条目在页面外(在页面的裁剪框之外)。
  • 签名小部件的 Rect 条目的宽度或高度为 0。
  • 签名小部件属于已关闭的可选内容组(又名图层)。
  • 其中一个签名小部件标志 InvisibleHiddenNoView 已设置。

如果您的表格 FDA 1571 设置了 隐藏 标志。

您可以像这样取消设置该标志:

PdfAcroForm.getAcroForm(pdfSigner.getDocument(), false)
    .getField(fieldName)
    .setVisibility(PdfFormField.VISIBLE);

(来自SignWithAdaptions测试testFda1571)

如果您想知道为什么在手动签署文档后签名是可见的:表单上的签名按钮执行了一点 JavaScript,其中还清除了 隐藏标志。