Rendering a document with filled form fields using

2019-09-10 01:55发布

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: Correct rendering with 1.8.12

Bad version rendered using 2.0.2: Bad version rendering with 2.0.2

1条回答
smile是对你的礼貌
2楼-- · 2019-09-10 02:51

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.

查看更多
登录 后发表回答