How to format and calculate formatting in PDF when filling in other fields using iTextSharp?

I have a PDF form with several text fields. The values โ€‹โ€‹entered in these fields are used to calculate values โ€‹โ€‹in other fields (calculated fields are read-only).

When I open the form in Adobe Reader and fill out the field, the calculated fields are automatically recalculated.

However, I use iTextSharp to fill in the fields, smooth out the resulting form, and then stream the flattened form back to the user over the Internet.

This part works just fine, except that computed fields are never computed. I assume that since no user events (such as keystrokes or focus or blur) are triggered, calculations do not occur.

Obviously, I could remove the calculations from the fillable form and do it all on the server, as I fill in the fields, but I would like the fillable form to be used by people as well as the server.

Does anyone know how to force the calculations?

EDIT: I don't feel too much iText / iTextSharp love here ...

Here are some more details. Setting stamper.AcroFields.GenerateAppearances to true does not help.

I think the answer lies somewhere in the actions of the page, but I donโ€™t know how to call it ...

+6
c # pdf pdf-generation itext itextsharp
source share
5 answers

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

iText makes no effort to fix calculated fields, since most of the times this is not possible. PdfCopyFields has some support for this, which sometimes works and sometimes not.

+5
source share

I figured out how to do it. Please see my answer to stackoverflow question:

How to update formatting by a non-calculated field and update the Calculated fields in the form of a fillable PDF

+2
source share

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

According to adobe javascript documentation this.calculateNow();

Forces to calculate all fields of calculations in the current document.

When the form contains a lot of calculations, there may be a significant delay after the user enters data in the field, even if it is not a calculation field. One strategy is to turn off calculations at some point and turn them back on (see Example).

To enable javascript call with iTextSharp:

 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(); } 
+2
source share

On the server side, see if there is an answer in the computed fields. If not, figure them out.

+1
source share

As Greg Hurlman says, you must do the calculations on the server yourself. This is more than just convenience, and there is a good reason for this.

Any file that the client has has potential for screws. I donโ€™t know why PDF forms are needed, but most likely it is connected with money, so there is a possibility that people will cheat by making the calculations the wrong result. If you trust client-side calculations, you cannot detect them.

When you receive a PDF form from a client, you must redo all the calculations so that you know that they are correct. Then, if you also have client versions that you are comparing with, you should check to see if they have been linked to.

Do not think that your customers are unreliable? Good for you, but the evidence disagrees. One of my first programming introductions was opening games for SimCity to give me more money. If it is possible to cheat in some way, then at some point people will try to do it.

+1
source share

All Articles