Java PDFBOX acroForm已填充,但在Acrobat reader中打开时,值将消失

Java PDFBOX acroForm已填充,但在Acrobat reader中打开时,值将消失,java,pdfbox,adobe-reader,acrofields,Java,Pdfbox,Adobe Reader,Acrofields,我有PDF表格,我正在尝试用PDFBOX填充它。它可以工作,表单被填充,我用其他阅读器或浏览器打开,我可以看到值,但是当我尝试在Adobe reader中打开时,值消失了,我尝试了所有可能的方法来找出为什么值不可见 我有一个模板表单,我使用它并填充数据,重命名字段,并将其合并到其他文档中,然后重复该过程,直到所有表单都被填充 我不确定这是否与我的代码或Adobe reader有关 这是我用来填充表单的代码 abstract class AbstractPDFFormFiller<T&g

我有PDF表格,我正在尝试用PDFBOX填充它。它可以工作,表单被填充,我用其他阅读器或浏览器打开,我可以看到值,但是当我尝试在Adobe reader中打开时,值消失了,我尝试了所有可能的方法来找出为什么值不可见

我有一个模板表单,我使用它并填充数据,重命名字段,并将其合并到其他文档中,然后重复该过程,直到所有表单都被填充

我不确定这是否与我的代码或Adobe reader有关

这是我用来填充表单的代码


abstract class AbstractPDFFormFiller<T> {

    private val logger = LogManager.getLogger()
    private val merge = PDFMergerUtility()

    private val PAGE_SIZE = 12

    fun fillForm(templatePath: String, data: List<T>, headerParam: Map<String, String>): PDDocument {
        val chunks = getDataChunks(data)
        val totalPages = chunks.size

        if (totalPages == 1) {
            val sourceDocument = getTemplate(templatePath)
            val form = sourceDocument.documentCatalog.acroForm
            fillHeader(form, headerParam, totalPages, totalPages, data)
            fillData(form, data, totalPages)
            return sourceDocument
        } else {
            val resultDocument = PDDocument()
            chunks.forEachIndexed { currentPage, it ->
                val sourceDocument = getTemplate(templatePath)
                val form = sourceDocument.documentCatalog.acroForm
                fillHeader(form, headerParam, currentPage, totalPages, it)
                fillData(form, it, currentPage)
//                mergePDFForm(resultDocument, sourceDocument)
                sourceDocument.save("C:\\Users\\\\Documents\\Downloads\\$currentPage.pdf")
                sourceDocument.close()
            }
            mergeFromDisk(File("C:\\Users\\Documents\\Downloads"),resultDocument)
            return resultDocument
        }
    }

    fun mergeFromDisk(folderPath:File, resultDoc:PDDocument){
        folderPath.listFiles()?.forEach {
            mergePDFForm(resultDoc, PDDocument.load(it))
        }
    }

    private fun mergePDFForm(destination: PDDocument, source: PDDocument) {
        try {
            source.documentCatalog.acroForm.flatten()
            merge.acroFormMergeMode = PDFMergerUtility.AcroFormMergeMode.JOIN_FORM_FIELDS_MODE
            merge.appendDocument(destination, source)
            merge.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun getTemplate(templatePath: String): PDDocument {
        val s = javaClass.classLoader?.getResource(templatePath)?.openStream()
        return PDDocument.load(s)
    }

    private fun getDataChunks(data: List<T>): List<List<T>> {
        return data.chunked(PAGE_SIZE)
    }

    fun setValue(
        form: PDAcroForm,
        fullyQualifiedName: String,
        value: String,
        rename: Boolean = false,
        reNameTo: String?
    ) {
        val field = form.getField(fullyQualifiedName)
        field.setValue(value)
        if (rename)
            renameField(
                form = form,
                fullyQualifiedName = fullyQualifiedName,
                newName = "${field.fullyQualifiedName}_$reNameTo"
            )
    }

    fun renameField(form: PDAcroForm, fullyQualifiedName: String, newName: String) {
        val field = form.getField(fullyQualifiedName)
            ?: throw IllegalArgumentException("Field with $fullyQualifiedName not found")
        if (field.actions != null && field.actions.f != null) field.actions.f = null
        try {
            field.partialName = newName
        } catch (e: Exception) {
            logger.fatal("Cannot rename to PDF form name {} to new name {}", fullyQualifiedName, newName)
        }
    }


    abstract fun fillHeader(
        form: PDAcroForm,
        map: Map<String, String>,
        currentPage: Int,
        totalPage: Int,
        data: List<T>
    )

    abstract fun fillData(form: PDAcroForm, data: List<T>, currentPage: Int)
}


class DiversionDataFormFiller : AbstractPDFFormFiller<DiversionData>() {
    private val logger = LogManager.getLogger()

    override fun fillHeader(
        form: PDAcroForm,
        map: Map<String, String>,
        currentPage: Int,
        totalPage: Int,
        data: List<DiversionData>
    ) {
        form.getField("Product Type").setValue(map["Product Type"])
        form.getField("Type of Schedule").setValue(map["Type of Schedule"])
        form.getField("LA Revenue Account Number").setValue(map["LA Revenue Account Number"])
        form.getField("Company Name").setValue(map["Company Name"])
        form.getField("Filling Period").setValue(map["Filling Period"])
        form.getField("Page").setValue((currentPage + 1).toString())
        form.getField("of").setValue(totalPage.toString())
        form.getField("Total").setValue(data.stream().mapToDouble(DiversionData::quantity).sum().toString())
        renameField(form, "Product Type", "Product Type_$currentPage")
        renameField(form, "Type of Schedule", "Type of Schedule_$currentPage")
        renameField(form, "LA Revenue Account Number", "LA Revenue Account Number_$currentPage")
        renameField(form, "Company Name", "Company Name_$currentPage")
        renameField(form, "Filling Period", "Filling Period_$currentPage")
        renameField(form, "Total", "Total_$currentPage")
        renameField(form, "Page", "Page_$currentPage")
        renameField(form, "of", "of_$currentPage")
    }

    override fun fillData(form: PDAcroForm, data: List<DiversionData>, currentPage: Int) {
        val fieldTree = form.fieldTree
        data.forEachIndexed { i, element ->
            fieldTree.forEach {
                if (it.fieldType == "Tx") {
                    try {
                        if (it.fullyQualifiedName.startsWith("Date") && it.partialName == i.toString()) {
                            logger.info(
                                "renaming {} to {}, {}",
                                it.fullyQualifiedName,
                                "${it.fullyQualifiedName}_$currentPage",
                                it.partialName
                            )
/*                            it.setValue(Util.dateToStr(element.date, "MM/dd/yy"))
                            renameField(form, it.fullyQualifiedName, "${it.partialName}_$currentPage")*/
                            setValue(
                                form,
                                it.fullyQualifiedName,
                                Util.dateToStr(element.date, "MM/dd/yy"),
                                true,
                                "$currentPage"
                            )
                        } else if (it.fullyQualifiedName.startsWith("Name2") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.shipperTaxPayerNumber)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("Name") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.supplierTaxPayerNumber)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("Diversion Number") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.importNumber)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("FEIN2") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.shipperTaxPayerNumber)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("FEIN") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.supplierTaxPayerNumber)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("Mode") && it.partialName == i.toString()
                        ) {
                            it.setValue("J")
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("Manifest") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.billOfLading)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("Doc. Number") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.billOfLading)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("Net gallons") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.quantity.toString())
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        } else if (it.fullyQualifiedName.startsWith("New") && it.partialName == i.toString()
                        ) {
                            it.setValue(element.revisedDestination)
                            renameField(form, it.fullyQualifiedName, "${it.fullyQualifiedName}_$currentPage")
                        }
                    } catch (ex: IOException) {
                        ex.printStackTrace()
                    }
                }
            }
        }
    }
}

填写表格的测试

   @Test
    public void fillFormUsingNewKotlinClass() throws IOException {
        List<DiversionData> diversionData = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            DiversionData d = new DiversionData(
                "terminal_Code" + i,
                "Regular" + i,
                "Supplier tax" + i,
                "shipper tax" + i,
                1000 + i,
                "TX",
                "LA",
                "0000" + i,
                LocalDate.now().plusDays(i),
                "123456" + i
            );
            diversionData.add(d);
        }
        //E:/repo/gasjobber-docker/gasjobber-api/src/main/resources/
        String path = "templates/taxFormsPDF/LA/5402(7_06)F.pdf";
        DiversionDataFormFiller filler = new DiversionDataFormFiller();
        Map<String, String> param = new HashMap<>();
        param.put("Product Type","065");
        param.put("Type of Schedule","22");
        param.put("LA Revenue Account Number","3264660001");
        param.put("Company Name","Test CO.");
        param.put("Filling Period","2020-12");
        PDDocument document =  filler.fillForm(path,diversionData,param);
        document.save(new File("C:\\Users\\Documents\\Downloads\\testpdf.pdf"));
        document.close();
    }
@测试
public void fillFormUsingNewKotlinClass()引发IOException{
List diversionData=new ArrayList();
对于(int i=0;i<20;i++){
DiversionData d=新的DiversionData(
“终端代码”+i,
“常规”+i,
“供应商税”+i,
“托运人税”+i,
1000+i,
“TX”,
“洛杉矶”,
“0000”+i,
LocalDate.now().plusDays(i),
“123456”+i
);
添加(d);
}
//E:/repo/gasjobber docker/gasjobber api/src/main/resources/
String path=“templates/taxFormsPDF/LA/5402(7_06)F.pdf”;
DiversionDataFormFiller=新的DiversionDataFormFiller();
Map param=new HashMap();
参数put(“产品类型”、“065”);
参数put(“附表类型”、“22”);
参数put(“LA收入账号”,“326460001”);
参数put(“公司名称”、“测试公司”);
参数put(“填充期”、“2020-12”);
PDDocument document=filler.fillForm(路径、diversionData、参数);
document.save(新文件(“C:\\Users\\Documents\\Downloads\\testpdf.pdf”);
document.close();
}

页面
/AA/O
条目(“打开页面时应执行的操作”)具有以下内容:

if (!bReset)
{
    this.resetForm();
    bReset = true;
}
因此表单被重置

即使手动填写表单并保存、关闭和重新打开,也会发生这种情况。也许这是一个演示版的“formupack”这样做的目的

您可以通过如下方式删除page/AA条目来防止这种情况

document.getPage(0).setActions(null);
或者只删除/O条目

document.getPage(0).getActions().setO(null);

页面
/AA/O
条目(“打开页面时应执行的操作”)具有以下内容:

if (!bReset)
{
    this.resetForm();
    bReset = true;
}
因此表单被重置

即使手动填写表单并保存、关闭和重新打开,也会发生这种情况。也许这是一个演示版的“formupack”这样做的目的

您可以通过如下方式删除page/AA条目来防止这种情况

document.getPage(0).setActions(null);
或者只删除/O条目

document.getPage(0).getActions().setO(null);

您使用的PDFBox版本是什么?你能创建一个自我工作的短代码吗?你能生成外观吗?对我来说似乎缺少外观…@Tilmahauser我正在使用版本2.0.21,这是代码,它的完整集包括测试和它的工作PDFBox在2.0.22,请重试。为了找出原因,理想的测试应该是设置一个字段(但可能“param.put”行应该可以指示它发生的位置),我可以使用SetFields.java示例和“Product Type”和605重现效果。该字段在PDFBox中可见,但在Adobe Reader中不可见,我怀疑Javascript有什么作用。您使用的是哪个PDFBox版本?你能创建一个自我工作的短代码吗?你能生成外观吗?对我来说似乎缺少外观…@Tilmahauser我正在使用版本2.0.21,这是代码,它的完整集包括测试和它的工作PDFBox在2.0.22,请重试。为了找出原因,理想的测试应该是设置一个字段(但可能“param.put”行应该可以指示它发生的位置),我可以使用SetFields.java示例和“Product Type”和605重现效果。该字段在PDFBox中可见,但在AdobeReader中不可见,我怀疑Javascript做了些什么。伟大的发现——这是我的第一次尝试!伟大的发现-这是我的第一次!