Skip to main content
Participant
June 13, 2020
Question

Formatting and calculation after programmatically merging forms and setting field values

  • June 13, 2020
  • 3 replies
  • 1942 views

I am using C# and Spire PDF to merge PDF files and set field values. The form is not intended to allow manual entry of any field data. The user enters their values using an HTML form - their entries are saved to a database and then programmatically entered into the PDF form.

 

So far everything above is working fine, but the form requires calculation and field formatting. Some fields are percentages, some are currency, some are integers, etc.

 

My problem is that field formatting and Javascripts do not get triggered when the field values are entered in C#. I know the scripts work and I know the field formatting is correct since it does all trigger once I change a field manually.

 

How can I trigger field formatting and calculations after programmatically setting field values?

This topic has been closed for replies.

3 replies

Joel Geraci
Community Expert
Community Expert
June 15, 2020

You probably can't. What you can do... maybe... I'm not familiar enough with Spire PDF to know for sure... is to set the needsAppearances value to true which will force Acrobat to run the formatting scripts when the document gets opened. However, that will only work with Acrobat and Reader. Most other PDF viewers won't. I strongly recommend that you change development tools to one that can run the calculations for you. The Datalogics PDF Java Toolkit will do that.

Karl Heinz  Kremer
Community Expert
Community Expert
June 15, 2020

Just to elaborate on what Joel said: Your process would require two steps: You process your files in the Spire PDF tool, and then you would have to open the file in Acrobat or Reader and save it. Unless you do the last step, if you open the file in e.g. Chrome or Firefox, the built-in PDF renderer will process the PDF file and will very likely not honor the needsAppearances flag, and the same is true for probably all PDF viewers in the mobile world (I don't know how the Acrobat viewer would fare in this scenario, but based on the comlexity of your JavaScript, it may not even have the JavaScript support to run your scripts). We now live in a world where you can no longer assume that a PDF file will be displayed or processed with Adobe tools, so you have to plan for that. 

Legend
June 14, 2020

Spire PDF is not an Adobe product, you have to contact Spire PDF support. However what you see is likely to be by design  

try67
Community Expert
Community Expert
June 13, 2020

You can trigger calculations by adding the following line of code to a doc-level script:

this.calculateNow();

You can't trigger the formatting of the values, though. You have to sort that out yourself in your code.

rwk.iiiAuthor
Participant
June 15, 2020

Try67, I think Spire PDF confused the question here. The PDF is viewed via filestream in an HTML5 iFrame where the user can download it. But, the 7-page document does not perform any calculations.

 

I've tried it flattened and not flattened. Thing is, when the document is not flattened and the fields can be entered data manually - one change to the form by the user triggers all of the document calculations and everything looks correct. But, the end user should never see this form in its unflattened state and so that will not work.

 

Where do I add this.calculateNow(); 

 

Joel Geraci
Community Expert
Community Expert
June 16, 2020

Most browsers don't process JavaScript in PDF. You're going to need to rethink your process.