使用iTextSharp填写其他字段时如何强制PDF格式化和计算?

时间:2020-12-10 21:10:47

I have a PDF form with a number of text fields. The values entered in these fields are used to calculate values in other fields (the calculated fields are read-only).

我有一个包含许多文本字段的PDF表单。在这些字段中输入的值用于计算其他字段中的值(计算字段是只读的)。

When I open the form in Adobe Reader and fill in a field, the calculated fields automatically re-calculate.

当我在Adobe Reader中打开表单并填写字段时,计算字段会自动重新计算。

However, I am using iTextSharp to fill in the fields, flatten the resulting form, then stream the flattened form back to the user over the web.

但是,我使用iTextSharp填充字段,展平生成的表单,然后通过Web将展平的表单流回用户。

That part works just fine except the calculated fields never calculate. I'm assuming that since no user triggered events (like keydowns or focus or blur) are firing, the calculations don't occur.

该部分工作正常,但计算字段永远不会计算。我假设因为没有用户触发事件(如keydowns或焦点或模糊)正在触发,所以不会发生计算。

Obviously, I could remove the calculations from the fillable form and do them all on the server as I am filling the fields, but I'd like the fillable form to be usable by humans as well as the server.

显然,我可以从可填写的表单中删除计算,并在填充字段时在服务器上完成所有计算,但我希望可填写的表单可供人类和服务器使用。

Does anyone know how to force the calculations?

有谁知道如何强制计算?

EDIT: I ain't feeling too much iText/iTextSharp love here...

编辑:我感觉不太喜欢iText / iTextSharp在这里爱...

Here are a few more details. Setting stamper.AcroFields.GenerateAppearances to true doesn't help.

这里有一些细节。将stamper.AcroFields.GenerateAppearances设置为true无济于事。

I think the answer lies somewhere in the page actions, but I don't know how to trigger it...

我认为答案在于页面操作中的某个地方,但我不知道如何触发它...

5 个解决方案

#1


Paulo Soares (one of the main devs of iText and current maintainer of iTextSharp) says:

Paulo Soares(iText的主要开发者之一和iTextSharp的当前维护者)说:

iText doesn't do any effort to fix calculated fields because most of the times that's impossible. PdfCopyFields has some support for it that sometimes works and sometimes don't.

iText不会为修复计算字段做任何努力,因为大部分时间是不可能的。 PdfCopyFields有一些支持,有时可以工作,有时不工作。

#2


I have figured out how to do this. Please see my answer for the * question:

我已经想出了如何做到这一点。请参阅我对*问题的回答:

How to refresh Formatting on Non-Calculated field and refresh Calculated fields in Fillable PDF form

如何刷新非计算字段上的格式并刷新可填写PDF表单中的计算字段

#3


I have updated all the calculated fields of my pdfs by calling the javascript method calculateNow on the Doc object.

我通过调用Doc对象上的javascript方法calculateNow更新了我的pdf的所有计算字段。

According to the adobe javascript documentation this.calculateNow();

根据adobe javascript文档this.calculateNow();

Forces computation of all calculation fields in the current document.

强制计算当前文档中的所有计算字段。

When a form contains many calculations, there can be a significant delay after the user inputs data into a field, even if it is not a calculation field. One strategy is to turn off calculations at some point and turn them back on later (see example).

当表单包含许多计算时,在用户将数据输入字段后,即使它不是计算字段,也会有明显的延迟。一种策略是在某个时刻关闭计算并稍后将其重新打开(参见示例)。

To include the javascript call with iTextSharp :

要使用iTextSharp包含javascript调用:

using (PdfReader pdfReader = new PdfReader(pdfTemplate))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create)))
{
    // fill f1 field and more...
    AcroFields pdfFormFields = pdfStamper.AcroFields;
    pdfFormFields.SetField("f1", "100");
    //...

    // add javascript on load event of the pdf
    pdfStamper.JavaScript = "this.calculateNow();";
    pdfStamper.Close();
}

#4


On the server side, see if there is an answer in the calculated fields. If not, calculate them.

在服务器端,查看计算字段中是否有答案。如果没有,请计算它们。

#5


As Greg Hurlman says, you should do the calculations yourself on the server. This is for more than just convenience, there's actually a good reason for it.

正如Greg Hurlman所说,你应该自己在服务器上进行计算。这不仅仅是为了方便,实际上还有很好的理由。

Any file that the customer has, they have the potential to screw with. I don't know what the PDF forms are for, but chances are it's connected to money somehow, so the potential exists for people to cheat by making the calculations show the wrong result. If you trust the calculations done on the client side, you have no way of detecting it.

客户拥有的任何文件,都有可能与之相关。我不知道PDF格式的用途是什么,但它有可能以某种方式与金钱相关联,因此人们通过使计算显示错误的结果而作弊的可能性。如果您信任在客户端进行的计算,则无法检测到它。

When you recieve the PDF form from the client, you should redo all calculations so that you know they're correct. Then if you also have the client's versions to compare to, you should check whether they've been screwed with.

当您从客户端收到PDF表单时,您应该重做所有计算,以便您知道它们是正确的。然后,如果您还要比较客户端的版本,您应该检查它们是否已经被搞砸了。

Don't think your clients are that untrustworthy? Good for you, but the evidence disagrees. One of my earliest introductions to programming was opening up the savegames for SimCity to give myself more money. If the opportunity exists to cheat in some way, then at some point people will try it.

不要以为你的客户不值得信任吗?对你有好处,但证据不同意。我最早的编程介绍之一是为SimCity打开存档以给自己更多的钱。如果有机会以某种方式作弊,那么在某些时候人们会尝试。

#1


Paulo Soares (one of the main devs of iText and current maintainer of iTextSharp) says:

Paulo Soares(iText的主要开发者之一和iTextSharp的当前维护者)说:

iText doesn't do any effort to fix calculated fields because most of the times that's impossible. PdfCopyFields has some support for it that sometimes works and sometimes don't.

iText不会为修复计算字段做任何努力,因为大部分时间是不可能的。 PdfCopyFields有一些支持,有时可以工作,有时不工作。

#2


I have figured out how to do this. Please see my answer for the * question:

我已经想出了如何做到这一点。请参阅我对*问题的回答:

How to refresh Formatting on Non-Calculated field and refresh Calculated fields in Fillable PDF form

如何刷新非计算字段上的格式并刷新可填写PDF表单中的计算字段

#3


I have updated all the calculated fields of my pdfs by calling the javascript method calculateNow on the Doc object.

我通过调用Doc对象上的javascript方法calculateNow更新了我的pdf的所有计算字段。

According to the adobe javascript documentation this.calculateNow();

根据adobe javascript文档this.calculateNow();

Forces computation of all calculation fields in the current document.

强制计算当前文档中的所有计算字段。

When a form contains many calculations, there can be a significant delay after the user inputs data into a field, even if it is not a calculation field. One strategy is to turn off calculations at some point and turn them back on later (see example).

当表单包含许多计算时,在用户将数据输入字段后,即使它不是计算字段,也会有明显的延迟。一种策略是在某个时刻关闭计算并稍后将其重新打开(参见示例)。

To include the javascript call with iTextSharp :

要使用iTextSharp包含javascript调用:

using (PdfReader pdfReader = new PdfReader(pdfTemplate))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create)))
{
    // fill f1 field and more...
    AcroFields pdfFormFields = pdfStamper.AcroFields;
    pdfFormFields.SetField("f1", "100");
    //...

    // add javascript on load event of the pdf
    pdfStamper.JavaScript = "this.calculateNow();";
    pdfStamper.Close();
}

#4


On the server side, see if there is an answer in the calculated fields. If not, calculate them.

在服务器端,查看计算字段中是否有答案。如果没有,请计算它们。

#5


As Greg Hurlman says, you should do the calculations yourself on the server. This is for more than just convenience, there's actually a good reason for it.

正如Greg Hurlman所说,你应该自己在服务器上进行计算。这不仅仅是为了方便,实际上还有很好的理由。

Any file that the customer has, they have the potential to screw with. I don't know what the PDF forms are for, but chances are it's connected to money somehow, so the potential exists for people to cheat by making the calculations show the wrong result. If you trust the calculations done on the client side, you have no way of detecting it.

客户拥有的任何文件,都有可能与之相关。我不知道PDF格式的用途是什么,但它有可能以某种方式与金钱相关联,因此人们通过使计算显示错误的结果而作弊的可能性。如果您信任在客户端进行的计算,则无法检测到它。

When you recieve the PDF form from the client, you should redo all calculations so that you know they're correct. Then if you also have the client's versions to compare to, you should check whether they've been screwed with.

当您从客户端收到PDF表单时,您应该重做所有计算,以便您知道它们是正确的。然后,如果您还要比较客户端的版本,您应该检查它们是否已经被搞砸了。

Don't think your clients are that untrustworthy? Good for you, but the evidence disagrees. One of my earliest introductions to programming was opening up the savegames for SimCity to give myself more money. If the opportunity exists to cheat in some way, then at some point people will try it.

不要以为你的客户不值得信任吗?对你有好处,但证据不同意。我最早的编程介绍之一是为SimCity打开存档以给自己更多的钱。如果有机会以某种方式作弊,那么在某些时候人们会尝试。