Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
17

Assign/copy a text field value from one PDF to another containing a text field with the same name

Engaged ,
Jan 02, 2024 Jan 02, 2024

Given an interactive PDF form containing a text field used to store a JSON (string) object containing data to populate form fields, I am looking for an alternative method (as opposed to requiring the end user to resort to cut and paste) to copy the JSON (string) data to a text field of the same name into an updated (renamed) version of the PDF form. To this end, I would prefer adding a menu item to my existing popup menu to activate a custom script to perform this task in one click of the mouse. All considered, what would be the best way to accomplish this task, i.e., using a global variable, import/export methods, from inside a custom script? Thank you ahead of time. Oh, lest I forget, any javascript method used needs to be supported using the Free Adobe Reader version 20 or greater. Thank you ahead of time.   

TOPICS
How to , JavaScript , PDF , PDF forms
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 02, 2024 Jan 02, 2024

A script on one PDF can access a form field in another PDF, if the target PDF is "disclosed", i.e., the "doc.disclosed" property is set to true.

A global variable won't work when used from a document script, unless the user has JS security for globals turned off, because only the document that sets the global can see it. 

Alternatively you can use a trusted folder level script to do this.

I have a paid for tool that does just this. Although it copies all data in matching fields, you could modify it for just the one field.

https://www.pdfscripting.com/public/Copy-Form-Data-Between-PDFs-Description.cfm

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 02, 2024 Jan 02, 2024

Thank you Thom for the quick response. In reality, I was hoping you would be the one to respond to my post. Also, thank you for providing the paid tool. I will be sure to check it out since I am not looking to reinvent the wheel. Also, am I correct in assuming that your paid tool doesn't require a folder level script to be installed on the end user's computer in order to use the script added to the PDF form? Last but not least, Happy New Year to you and your family. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 04, 2024 Jan 04, 2024

In electing to review the script one last time, it appears that I may have been overthinking the script required to copy a field value from one PDF to another with identical named fields. Given the script required to accomplish this task provides the same end result as having two PDF forms open inside Acrobat using basic cut and paste procedures, and moving from one PDF to the other, a task I would prefer to accomplish using a script activated through a popup menu item,  I believe the script shouldn't be much different than copying a form field value from one form field to another inside one and the same PDF form other than in this particular instance, the script is intended to copy a form field value to a form field of the same name residing in another PDF form. Alas, I suspect the script isn't at fault but that security measures added by Adobe to date appear to be interfering with the exceutiuon of a certain Acrobat method, app.openDoc, used in my script that in turn prevent the script from performing as intended. This being the case, I believe I would be better off instructing my end user to have both PDF forms (older and updated PDF forms to which I have already set the disclosed property to true) open in Acrobat, as oppposed to using the app.openDoc method to open the updated PDF to which the form field value from the older version needs to be copied, and using a script to loop through and retrieve each active doc by title to copy a form field value from one PDF to the other PDF. Question is, might I anticipate security issues using this alternative method? Also, as the form fields copied to/from are of the same name inside each PDF, what method, if any, may be required to change an already open PDF doc to the active doc for copying a like named field value to/from another.  Thank you ahead of time.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2024 Jan 05, 2024

First, folder level scripts need to use trusted functions to execute privileged operations. It's explained here:

https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm

 

 

But, it sounds like you have a plan.  I think disclosing the PDF form and putting a button on the form that copies data from another form is the best way to go.  

If only two forms are open, then it's an easy script. 

This code identifies the other form.

 

var oSourceDoc = null;
for(var i=0;i<app.activeDocs.length;i++)
{
    if(app.activeDocs[i] != this){
       oSourceDoc = app.activeDocs[i];
       break;
    }
}

if(oSourceDoc)
{
... perform copy ...
}

 

if you want to handle more than one open document, then the script needs to create a list of the other docs and present the user with a menu or some other list choice option for selecting the source doc.  

Is the operation restriced to copying from filled forms of the same type?  If so then the list also needs to be filtered. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 05, 2024 Jan 05, 2024

In response to 'First, folder level scripts need to use trusted functions to execute privileged operations. It's explained here:', understood. Also, correct me if I am wrong but given the 'activeDocs' method (displayed inside the script you provided and also included in my own script) in the Acrobat API is marked by an 'S' in the 3rd column of the quick bar, doesn't this indicate security restrictions, in which event both my script and the one you provided can only be executed in a privileged context meaning that both need to be folder level scripts installed on my computer as well as my end users? Thanks again for the response.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2024 Jan 05, 2024
LATEST

All disclosed documents are listed in app.activeDocs, when executed from a non-privileged context

Try it out. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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