Rendering a document with filled form fields using PDFBox works with 1.8.2, but not 2.0.2

泪湿孤枕 提交于 2019-12-12 03:18:51

问题


My goal is to open a PDF document, fill in some form fields and then render it to an image. I'm using PDFBox with Java to do it. I started using version 2.0.2 (latest) and filling the form fields works. When I save it and then open it with a PDF reader, the form fields have values. But when I render it to an image, the form fields have black borders and no text inside. I then tried the same thing with 1.8.12 and it works. However, I would really like to use the new features in 2.x.

  • The PDF only has AcroForms, no XFA (or at least I think so). When I call PDAcroForm.getXFA() it returns null.
  • Using 2.0.2, if I'm rendering something filled using setValue, then the rendering looks broken. However, rendering something filled using Adobe Reader works. Both cases work using 1.8.
  • With 2.0.2 I tried any combination of PDAcroForm.refreshAppearances() and/or PDAcroForm.setNeedAppearances(true). Those methods are absent in 1.8.

The code I'm using to render using 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();
    }
}

The code I'm using to render using 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);
        }
    }
}

Correct version rendered using 1.8.12:

Bad version rendered using 2.0.2:


回答1:


This was a bug in PDFBox 2.0.2. It is resolved in 2.0.4, so the solution is to upgrade the version to the latest.



来源:https://stackoverflow.com/questions/38724499/rendering-a-document-with-filled-form-fields-using-pdfbox-works-with-1-8-2-but

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!