使用PDFBox渲染具有填充表单字段的文档适用于1.8.2,但不适用于2.0.2 [英] Rendering a document with filled form fields using PDFBox works with 1.8.2, but not 2.0.2

查看:426
本文介绍了使用PDFBox渲染具有填充表单字段的文档适用于1.8.2,但不适用于2.0.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  • PDF只有AcroForms,没有XFA(或者至少我想是这样)。当我打电话给PDAcroForm.getXFA()时,它返回null。
  • 使用2.0.2,如果我使用setValue来渲染某些东西,那么渲染看起来很糟糕。但是,使用Adobe 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());
}

列表< 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());
}
}
//不带或不带
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呈现的错误版本:

解决方案

这是PDFBox 2.0.2中的一个错误。它已在2.0.4中解决,所以解决方案是将版本升级到最新版本。


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:

解决方案

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.

这篇关于使用PDFBox渲染具有填充表单字段的文档适用于1.8.2,但不适用于2.0.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆