iText签名表单字段内容未打印在纸上

时间:2018-02-21 09:01:06

标签: java pdf printing itext digital-signature

我有一种方法,使用iText 5.5.9将签名字段添加到PDF单元格。

private static void addSignatureFieldToCell(String fieldname, PdfPCell pdfPCell, PdfWriter pdfWriter) {
    PdfFormField digitalSignatureField = PdfFormField.createSignature(pdfWriter);
    // If the PDF is not signed we don't show anything
    digitalSignatureField.setWidget(new Rectangle(0, 0, 0, 0), PdfAnnotation.HIGHLIGHT_NONE);
    digitalSignatureField.setFieldName(fieldname);
    // With this flag the field is shown on Acrobat
    digitalSignatureField.setFieldFlags(PdfAnnotation.FLAGS_PRINT);
    // This allows to show the signature on the signatures panel at the left
    digitalSignatureField.setPage();

    pdfWriter.addAnnotation(digitalSignatureField);
    // This is the event attached to the cell that tells the writer to fill the field with the signature data
    FieldPositioningEvents fieldPositioningEvents = new FieldPositioningEvents(pdfWriter, digitalSignatureField);
    pdfPCell.setCellEvent(fieldPositioningEvents);
}

这很好用。经过一些步骤后,数字签名的信息在PDF上。我打开Acrobat Reader,我可以看到PDF数字签名等等。

问题在于我要从Acrobat打印PDF。签名的信息没有显示在预览上,当然,当我打印文件时,它也没有显示,因此该框只是空白。

我是否错过了PdfFormField上的一些配置?我需要什么才能打印签名?

Link of sample PDF

1 个答案:

答案 0 :(得分:2)

PRINT标志是注释标志,而不是字段标志。

您使用setFieldFlags

digitalSignatureField.setFieldFlags(PdfAnnotation.FLAGS_PRINT);

设置了一些你可能根本不感兴趣的字段标志。相反,您应该使用setFlags

digitalSignatureField.setFlags(PdfAnnotation.FLAGS_PRINT);

实际设置使注释可打印的标志。

此外,您将签名字段添加到注释

pdfWriter.addAnnotation(digitalSignatureField);

FieldPositioningEvents也会在布局期间这样做。因此,签名字段在注释列表中重复,这是您不想要的。