Formatting and calculation after programmatically merging forms and setting field values

Community Beginner ,
Jun 13, 2020

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript

Views

222

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Jun 13, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
rwk.iii AUTHOR
Community Beginner ,
Jun 15, 2020

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Jun 13, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
rwk.iii AUTHOR
Community Beginner ,
Jun 15, 2020

Copy link to clipboard

Copied

I am using Acrobat to create the forms and set their options.

 

I am using Reader on the frontend to for the user to view the PDF (I think). Here is the HTML:

 

<div id="DisplayPDF" class="embed-responsive embed-responsive-1by1">
    <iframe id="pdf" class="embed-responsive-item" src="" allowfullscreen></iframe>
</div>

 

and the Ajax success() that files the iFrame:

 

...
success: function (data) {
   var iframe = document.getElementById('pdf');
   var url = window.URL || window.webkitURL;
   iframe.src=url.createObjectURL(data);
   abp.ui.clearBusy($('#ReportFormArea'));
},
...

 

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

unless you are using Internet Explorer exclusively, an iframe will not display a PDF using Adobe Reader. All modern browsers have their own built-in PDF viewers. I'm not aware of any that run calculations or format fields.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
rwk.iii AUTHOR
Community Beginner ,
Jun 15, 2020

Copy link to clipboard

Copied

Hi Joel, can you point me to any relevant reference on needsAppearances? Actually, I think I have the formatting issue resolved by setting the field's .text property already formatted in the C# backend. But, I still need to trigger calculations.

 

I am using Acrobat to edit the 7 individual PDF document templates. I am using Spire PDF in the C# controller to merge those documents and fill all of the fields with values from a SQL Database.

 

What I really wish I could do is trigger this.calculateNow() before the PDF is sent to the client's browser.

 

I have clarified with my client - mobile use of these forms is impractical and absolutely not needed.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

You can't useing the technology you have.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
rwk.iii AUTHOR
Community Beginner ,
Jun 16, 2020

Copy link to clipboard

Copied

Joel, are there any other toolkits available that you can think of other than Datalogics? I requested them to call me with a prce, but when pricing isn't published I already know it's going to be quite pricy... My client wouldn't mind purchasing a license, but I felt he was maxxed out on a Spire PDF license for $595.

 

Thank you!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 16, 2020

Copy link to clipboard

Copied

That's the only toolkit that I am aware of that will perform calculations and properly format a PDF without the application knowing anything about what calculations are in the PDF. It just basically does what Acrobat does when a user enters data. It'll even run custom JavaScripts at the document level.

 

The only alternative is to create a separate application that performs the calculations that would be calculated by the PDF and then just set the values.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more