【问题标题】:Rendering a document with filled form fields using PDFBox works with 1.8.2, but not 2.0.2使用 PDFBox 呈现具有填充表单域的文档适用于 1.8.2,但不适用于 2.0.2
【发布时间】:2016-08-02 15:21:25
【问题描述】:

我的目标是打开一个 PDF 文档,填写一些表单域,然后将其呈现为图像。我正在使用带有 Java 的 PDFBox 来做到这一点。我开始使用版本 2.0.2(最新)并填写表单字段。当我保存它然后用 PDF 阅读器打开它时,表单字段具有值。但是当我将它渲染为图像时,表单域有黑色边框,里面没有文本。然后我用 1.8.12 尝试了同样的事情,它可以工作。但是,我真的很想使用 2.x 中的新功能。

  • PDF 只有 AcroForms,没有 XFA(或者至少我认为是这样)。当我调用 PDAcroForm.getXFA() 时,它返回 null。
  • 使用 2.0.2,如果我正在渲染使用 setValue 填充的内容,那么渲染看起来会损坏。但是,使用 Adob​​e Reader 渲染填充的内容是可行的。这两种情况都使用 1.8。
  • 在 2.0.2 中,我尝试了 PDAcroForm.refreshAppearances() 和/或 PDAcroForm.setNeedAppearances(true) 的任意组合。 1.8 中没有这些方法。

我使用 1.8.12 渲染的代码:

public class Main {
    public static void main(String[] args) throws Exception {
        PDDocument doc = PDDocument.loadNonSeq(new File("test.pdf"), null);
        doc.setAllSecurityToBeRemoved(true);
        PDDocumentCatalog cat = doc.getDocumentCatalog();
        PDAcroForm form = cat.getAcroForm();
        for (Object _field : form.getFields()) {
            PDField field = (PDField) _field;
            System.out.println(field.getFullyQualifiedName());
            field.setValue(field.getFullyQualifiedName());
        }

        List<PDPage> pdPages = doc.getDocumentCatalog().getAllPages();
        int page = 0;
        for (PDPage pdPage : pdPages) {
            ++page;
            BufferedImage bim = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, 96);
            ImageIOUtil.writeImage(bim, "rendered" + "-" + page + ".png", 96);
        }
        doc.close();
    }
}

我使用 2.0.2 渲染的代码:

public class Main {
    public static void main(String[] args) throws Exception {
        PDDocument doc = PDDocument.load(new File("test.pdf"));
        doc.setAllSecurityToBeRemoved(true);
        PDDocumentCatalog cat = doc.getDocumentCatalog();
        PDAcroForm form = cat.getAcroForm();
        for (PDField field : form.getFields()) {
            System.out.println(field.getFullyQualifiedName());
            if (field instanceof PDTextField) {
                field.setValue(field.getFullyQualifiedName());
            }
        }
        // Doesn't work with or without these
        form.setNeedAppearances(true);
        form.refreshAppearances();

        PDDocument renderDoc = doc;
        PDFRenderer pdfRenderer = new PDFRenderer(renderDoc);
        for (int page = 0; page < renderDoc.getNumberOfPages(); ++page) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 96,     ImageType.RGB);
            ImageIOUtil.writeImage(bim, "rendered" + "-" + (page + 1) +     ".png", 96);
        }
    }
}

使用 1.8.12 呈现的正确版本:

使用 2.0.2 渲染的错误版本:

【问题讨论】:

  • 我已经在 Dropbox 上上传了我正在测试的文档:dl.dropboxusercontent.com/u/87048723/…
  • 这是 PDFBox 2.0.x 中的一个错误。我在issues.apache.org/jira/browse/PDFBOX-3454为此创建了一个问题
  • @MaruanSahyoun 非常感谢。如果这确实得到证实,这是否更适合作为答案?
  • @IvailoKaramanolev 你能用 2.0.4 重新测试吗?我得到的结果与你不同(见问题)。
  • @TilmanHausherr 为了完整起见,我也粘贴了我在 Jira 中发布的内容:我已经在之前使用相同的 PDF 的同一台机器和 JVM 上使用 2.0.4 对此进行了测试。它似乎以我期望的方式呈现并正确显示字段内容。我认为这已在 2.0.4 中解决。谢谢。

标签: java forms pdf pdfbox


【解决方案1】:

这是 PDFBox 2.0.2 中的一个错误。 2.0.4解决了,所以解决方法是升级到最新版本。

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2014-10-24
    相关资源
    最近更新 更多