简体   繁体   English

使用Java中的pdfbox为PDF中的方程插入双值

[英]Insert double Value for equation in PDF with pdfbox in Java

I'm struggling with a little Java Project: I made a Program which autofills a PDF Formular.我正在为一个小 Java 项目而苦苦挣扎:我制作了一个自动填充 PDF 公式的程序。 Mostly everything works fine for me, but there is a Problem: In this PDF Formular (which is given from my company, so I have to deal with this document) is a equation Field, which is used for calculation the Costs from Number of Items and the single Price.大多数情况下,一切对我来说都很好,但有一个问题:在这个 PDF 公式中(这是我公司提供的,所以我必须处理这个文档)是一个方程字段,用于计算项目数量的成本和单价。 When I insert the Price of a single Item as a String to my PDF当我将单个项目的价格作为字符串插入到我的 PDF 中时

    public void setEinzelpreis(String Einzelpreis)
{
    try {
        fieldList.get(30).setValue(Einzelpreis);
...

在此处输入图片说明

There should be the single Price on the empty field in the first row.第一行的空白字段上应该有单个价格。 The last Cell of the row is auto-calculated by the pdf.该行的最后一个单元格由 pdf 自动计算。 When I Click in the PDF in the "empty" Field, the Value appears:当我在“空”字段中单击 PDF 时,值出现: 在此处输入图片说明 When I click to another Field, the Value disappears.当我单击另一个字段时,该值消失了。 This is my Problem.这是我的问题。 I'm getting the FieldList via pdfbox and the Code for getting my fieldList of the PFD is:我通过 pdfbox 获取 FieldList,获取 PFD 的 fieldList 的代码是:

try {
    pdfTemplate = PDDocument.load(template);
    PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();
    if (acroForm != null)
    {
    // Get field names
    fieldList = acroForm.getFields();
    
    }
...

So, can anybody tell what I'm doing wrong?那么,有人能告诉我我做错了什么吗? Maybe the PDF wants a double Value for the equation and I am giving a String?也许 PDF 需要等式的双值,而我给出的是字符串? But I don't know how to write a double in the FieldList.但是我不知道如何在 FieldList 中写一个双精度值。 Thanks a lot for every hint!非常感谢每一个提示!

Edit:编辑:
The PDF File which I'm using: https://1drv.ms/b/s!Av6exjPNXlgOioouAuXL6QV4eUGkqg?e=ocfhvC我正在使用的 PDF 文件: https : //1drv.ms/b/s!Av6exjPNXlgOioouAuXL6QV4eUGkqg?e=ocfhvC

And this is the file I generated: https://1drv.ms/b/s!Av6exjPNXlgOioovK-HuRuXW2aRy_w?e=D1ZCA8这是我生成的文件: https : //1drv.ms/b/s!Av6exjPNXlgOioovK-HuRuXW2aRy_w?e=D1ZCA8

The strange thing is: when I change the value in the document by hand, everything acts normal, even with a different Document Viewer.奇怪的是:当我手动更改文档中的值时,一切都正常,即使使用不同的文档查看器。

First of all, the AcroForm form structure in your PDF is weird.首先,PDF 中的 AcroForm 表单结构很奇怪。 It looks like someone used a graphical form generation tool he did not understand and clicked, dragged, dropped, copied, ... until the form in a viewer did what he wanted, not caring about it having become difficult to maintain.看起来有人使用了一个他不理解的图形表单生成工具,然后单击、拖放、复制……直到查看器中的表单按照他的意愿行事,而不关心它变得难以维护。

In particular the Einzelpreis fields have a completely unnecessary structure of intermediate and final fields, eg特别是 Einzelpreis 字段具有完全不必要的中间和最终字段结构,例如

截屏

Thus, the field Einzelpreis in € exkl USt1 (the '€' is missing in the tree above) is not the one to fill in, it's merely an intermediary field.因此, Einzelpreis in € exkl USt1的字段Einzelpreis in € exkl USt1 (上面的树中缺少“€”)不是要填写的字段,它只是一个中间字段。 The actual form field to fill in is Einzelpreis in € exkl USt1.0.0.0.0 .要填写的实际表单字段是Einzelpreis in € exkl USt1.0.0.0.0

Unfortunately you in your code simply grab the 30th field in the field list returned by PDAcroForm , and this field happens to be the intermediary field Einzelpreis in € exkl USt1 ;不幸的是,您在代码中只是获取了PDAcroForm返回的字段列表中的第 30 个字段,而该字段恰好是Einzelpreis in € exkl USt1的中间字段Einzelpreis in € exkl USt1 as an intermediary field it has no visible widgets of its own, so your setValue call doesn't change the visible Einzelpreis.作为中间字段,它没有自己的可见小部件,因此您的setValue调用不会更改可见的 Einzelpreis。

The JavaScript instruction calculating the Gesamtpreis uses the value from the final field, too:计算 Gesamtpreis 的 JavaScript 指令也使用来自最终字段的值:

AFSimple_Calculate("PRD", new Array ("Anzahl1", "Einzelpreis in € exkl USt1.0.0.0.0"));

But as the field value is inheritable and none of the .0 fields has an own value, the calculation sees the 100 once form calculation has been triggered and uses it.但是由于字段值是可继承的,并且 .0 字段都没有自己的值,因此一旦触发表单计算并使用它,计算就会看到 100。

Thus, you should fill the Einzelpreis in € exkl USt1.0.0.0.0 field instead.因此,您应该Einzelpreis in € exkl USt1.0.0.0.0字段中填写Einzelpreis in € exkl USt1.0.0.0.0 And the more secure way to retrieve it is not by index in a field list but by name:更安全的检索方式不是通过字段列表中的索引,而是通过名称:

PDField fieldByName = acroForm.getField("Einzelpreis in € exkl USt1.0.0.0.0");

(excerpt from FillInForm test testFill2020_04BeschaffungsantragEinzelpreis ) (摘自FillInForm测试testFill2020_04BeschaffungsantragEinzelpreis

After filling that field, the "100" should be visible in your form.填写该字段后,“100”应该在您的表单中可见。

The remaining problem that the Gesamtpreis value is not calculated is due to the fact already mentioned by @Tilman in a comment to the question: PDFBox doesn't use javascript .未计算 Gesamtpreis 值的剩余问题是由于@Tilman 在对问题的评论中已经提到的事实: PDFBox 不使用 javascript Thus, you have to calculate those values yourself and update the fields in question accordingly.因此,您必须自己计算这些值并相应地更新相关字段。

If you need to know the correct name of a form field, you can do as Tilman proposed and use the PDFBox PDFDebugger.如果您需要知道表单域的正确名称,您可以按照 Tilman 的建议进行操作并使用 PDFBox PDFDebugger。 If you hover over the field there, it will display the name in the status bar at the bottom.如果您将鼠标悬停在该字段上,它将在底部的状态栏中显示名称。

By the way, the AcroForm method getFields won't return the field required here anyways.顺便说一下, AcroForm方法getFields无论如何都不会返回此处所需的字段。 As documented in its JavaDocs, this method will return all of the documents root fields , no fields further down in the hierarchy, at least not immediately.正如其 JavaDocs 中所述,此方法将返回所有文档根字段,在层次结构中没有进一步向下的字段,至少不会立即返回。 (From the user perspective the method name getFields is a misnomer. It is accurate, though, from the PDF specification perspective as the corresponding entry in the AcroForms object has the key Fields .) (从用户的角度来看,方法名称getFields是用词不当。不过,从 PDF 规范的角度来看,它是准确的,因为AcroForms对象中的相应条目具有键Fields 。)

Beware, though, you probably will have to update your PDFBox version.但是请注意,您可能需要更新您的 PDFBox 版本。 In earlier versions PDFBox did not update appearances of fields with JavaScript actions (believing some JavaScript would fill it in anyways).在早期版本中,PDFBox 没有使用 JavaScript 操作更新字段的外观(相信一些 JavaScript 无论如何都会填充它)。 I used the current 3.0.0-SNAPSHOT in which that behavior has been changed.我使用了当前的 3.0.0-SNAPSHOT,其中该行为已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM