当前位置: 首页 > 知识库问答 >
问题:

PDFBOX acroForm填充,但当在Acrobat阅读器中打开时,值消失

薛枫
2023-03-14

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

我有模板表单,我使用和填充数据,重命名字段,并将其合并到其他文档中,然后重做该过程,直到所有表单都填满。

我不确定这是否与我的代码或Adobe阅读器有关。

链接到我需要填写的PDF表单

这是我填充表单的代码。


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()
                    }
                }
            }
        }
    }
}

数据类定义

data class DiversionData(
    val terminalIRSCode: String,
    val fuelType: String,
    val supplierTaxPayerNumber: String,
    val shipperTaxPayerNumber: String,
    val quantity: Double,
    val originalDestination: String,
    val revisedDestination: String,
    val importNumber: String?,
    val date: LocalDate,
    val billOfLading: String,
)

测试以填写表格

   @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();
    }

共有1个答案

漆雕宏浚
2023-03-14

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

if (!bReset)
{
    this.resetForm();
    bReset = true;
}

所以表单被重置。

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

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

document.getPage(0).setActions(null);

或者删除/O条目

document.getPage(0).getActions().setO(null);
 类似资料:
  • 在我们的应用程序中,我们有一个场景,我们点击一个链接,点击后打开一个新的选项卡,其中有一个动态生成的PDF。 生成的将在new选项卡中打开,并以作为URL。 我没有选择下载生成的PDF以及。 我们有什么办法能解决这个问题吗??提前谢谢你。

  • 关于 FreeBSD 下搭建开发环境的信息的修改,可以参看 development(7)。 Brian Harvey and Matthew Wright Simply Scheme MIT 1994. ISBN 0-262-08226-8 Randall Schwartz Learning Perl O'Reilly 1993 ISBN 1-56592-042-2 Patrick Henry W

  • 我使用Adobe Acrobat Pro创建了一个表单。我希望日期字段(表单上的第一个文本字段)预填有“输入日期”这样的说明性文本。点击后,这将消失,而且,如果打印了表单,预填充的文本将不显示。 我在acrobat网站上找到了一个我在日期字段中使用的脚本,但它不起作用。脚本如下--

  • 我正在使用此作为参考向pdf添加视觉签名-https://stackoverflow.com/a/27940667/7103795 我能正确地打印细节。但pdf在Acrobat中打开时不会显示绿色勾号,尽管它写着“已签名且所有签名均有效”在签名面板中。 这是我需要的一个例子: 我怎样才能确保有效期标志出现? 我使用的是pdfBox 2.0.1版

  • 我有一个熊猫数据框,它有超过4列。col1中的某些值缺失,我希望根据以下方法设置这些缺失的值: 尝试根据具有相同col2, col3, col4值的记录的col1值的平均值来设置它 如果没有这样的记录,则根据具有相同col2, col3值的记录的col1值的平均值来设置它 如果仍然没有这样的记录,则根据具有相同col2值的记录的col1值的平均值来设置它 如果以上都找不到,将其设置为col1中所有