Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jan 30, 2019 10:11 pm

Is it possible to replace the "Custom calculation script" applied to a Text Field property from within spire?

Thanks

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Thu Jan 31, 2019 2:32 am

Hi,

Thank you for your inquiry.
Yes, Spire.PDF supports that function. Please directly set the calculation action for the text field like below. The new script would cover the old one by default.
Code: Select all
//new script
String newScript="the new script";
PdfJavaScriptAction action = new PdfJavaScriptAction(newScript);

PdfDocument pdf = new PdfDocument(@"input.pdf");
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
//get the calculation result textbox field
PdfTextBoxFieldWidget textbox = formWidget.FieldsWidget.List[index] as PdfTextBoxFieldWidget;
textbox.Actions.Calculate = action;

pdf.SaveToFile("output.pdf");


If there's still any problem, please share your sample input PDF file for investigation.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Jan 31, 2019 5:25 pm

I followed the sample but I want to overwrite an existing calc script with revisions. When I debug the project, I see the changes made to the script and it appears to be applied but when the document is updated, it shows the result of the original script without my revisions. Is there some additional action that needs to be taken to force the script to re-run?

My original script is located in the "Custom calculation script" found on the "Calculate" tab found in the Text Field Properties.

Thank you

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Thu Jan 31, 2019 8:52 pm

Wanted to post an update. I was able to get the script to activate if once I revised the script, I set it to run after the document open, effectively reassigning the script revisions to another place.

Spire.Pdf.Widget.PdfTextBoxFieldWidget tb = field as Spire.Pdf.Widget.PdfTextBoxFieldWidget;
JscriptVal = tb.Actions.Calculate.Script;
//modifications to JscriptVal (the original script stored in the document)
.......
// save the revisions
PdfJavaScriptAction action = new PdfJavaScriptAction(JscriptVal);
tb.Actions.Calculate = action;

// after everything else is done and BEFORE i save the document, i do the following.
//move the script to run after the document opens
PdfJavaScriptAction blah = new PdfJavaScriptAction(JscriptVal);
doc.AfterOpenAction = blah;

So when the script is updated as original recommended, the script is not reactivated so it remains the same. The way i've done it will work but has a much more limited and specific use where the original way would be more useful generically if it can be accomplished.

Thanks

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Fri Feb 01, 2019 2:38 am

Hi,

Thank you for your response.
I have forward the requirement of automatically updating the field value to our dev team. They would investigate if it would be achieved.
Once there's any good news, I will let you know.
As a workaround, you could use "doc.AfterOpenAction=action" to force updating.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Feb 01, 2019 11:04 am

Hi,

After our dev team's further investigation, sorry to tell there's no way to achieve it at present.
Adobe would automatically run the script after altering the script code, while in Spire, it would be triggered only after other field value has been changed.
You have to force it to run after opening a file like the workaround.
Sorry for the inconvenience caused!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Feb 15, 2019 8:03 pm

I'm see some other undesirable behavior.

After I've updated the script then open the document, the revisions are as expected; however, I need 2 copies of the pdf merged into a larger document. The first copy looks as expected but the second looks like the "ORIGINAL" script rather than the updated script.

I updated the AfterOpenScript with the revised script. After seeing this, i tried these things with no success.
setting the original script on the textbox to empty
setting the original script on the textbox to the revised version of the script.

Somehow the ORIGINAL script somehow gets executed even after it has been replaced and moved to the AfterOpen.

I have no idea what's happening. Can you offer any assistance?

Thank you
:(

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Fri Feb 15, 2019 10:16 pm

I found where the data was coming from. It was stored in the .Text of the element where the script was originally applied. I applied the same updates to the .Text element and now the old script text is eliminated.

So, the issue appears comes when files are merged. The first occurrence of the document loads as expected, the second occurence ends up with whatever result is store in the .Text element. The AfterOpen action is not executed on the second occurence.

Even if i use a different PDF file name, the second occurrence is not processed as the first. The behavior is using the AppendPage method. If i use the MergeFiles method, none of the scripts appear to perform as expected.

Defintely some issues here that are preventing me from using the product. :(

Would appreciate any suggestions you have to offer.

Thank you

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Mon Feb 18, 2019 9:31 am

Hello,

Thanks for your feedback and sorry for late reply as weekend.
I have noticed that after merging two copied PDF files, the script values were not correct. The issue has been forwarded to our Dev team for further investigating. If there is any update, we will let you know. Sorry for the inconvenience caused. BTW, could you please provide your two copied PDF as well as your merged PDF, so that we can look into your case accurately. You could send then to us via email ([email protected]). Thanks in advance.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Tue Feb 19, 2019 3:10 pm

I was only able to upload a single file due to upload restrictions. I sent the PDF created after processing the template. This sample is a single element where the form field contains a calulate script. The script is as follows

var ObjectName = "LEGAL_BODY";
var JSArray = new Array();
JSArray[0] = new Object();
JSArray[0].text = " WHEREAS";
JSArray[0].fontWeight = 999;
JSArray[1] = new Object();
JSArray[1].text = ", test body language for a person named <<DB_NAME>>.";
JSArray[2] = new Object();
JSArray[2].text = "\n\n"
this.getField(ObjectName).richValue = JSArray;


In the above case, we set the first word to bold, then we have a dynamic element where we replace the value with another value. As noted in the earlier post, we move the script from the form element to the AfterOpen action so that when the document is opened, the script is applied. As you can see in the attached file, this works correctly.

If we take this file and attempt to create a second PDF with 2 copies of this file, the first is as expected but the second loses the bold formatting. The same result occurs if we use the "appendpage" approach file the "file merge" approach.

We also set the form field to rich text, then copy/pasted the same text into the DefaultValue WITHOUT the script; however, when we updated the DefaultValue text, we also lose the formatting for the element.

We have spent much time try to find a work around for this issue and would be grateful if this could be resolved.

I will send another reply with the merge sample if the system will allow it.

Thank you,

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Tue Feb 19, 2019 3:16 pm

we were not able to upload the merge sample due to size restriction. sorry

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Tue Feb 19, 2019 5:59 pm

*********UPDATE*********
I have found the issue with the merge. When a new document is appended and is the same document, the form elements are renamed which causes the script from in the original document to become ineffective. For example, if i have an element called "mydata" in the first document, the second, duplicate document element name will become "mydata0".

The work around is to cause the script to update both form element names which i have done and it does solve the problem.

The file merge appears to just ignore the scripts altogether.

Thanks

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Wed Feb 20, 2019 8:02 am

Hello,

Thanks for your sharing.
Your understanding is correct. While merging the two same PDF, our Spire.PDF will automatically change the same field name that has already appeared to avoid the name conflict in a PDF. The filed name has changed, hence the script didn't work for the second field. That led to your issue. One solution for you is to use the tb.SetName("LEGAL_BODY_New") to change the field name of copied PDF to make the field name different, and also replace the var ObjectName = "LEGAL_BODY" with the new name "LEGAL_BODY_New" and apply the new script to the copied PDF. After that, there will be no problem. Please have a try. If there is any question, please feel free to write back.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF

cron