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

JavaScript for Acrobat get filed value from another file pdf

Guest
Jan 11, 2024 Jan 11, 2024

I have struggled to read the value from the form filed in doc1 and write in another field in doc2

 

I have two pdf document, for example:

 

doc1:

Text filed with name Text1

Text filed with name Text2

Button  filed with name Button1

doc2:

Text filed with name Text1

Text filed with name Text2

 

doc1, mouse enter event on Button1, action Run a java script 

var doc = app.openDoc("doc2.pdf", bHidden) ;
getField("Text1").value = doc.getField("Text1").value;

 

this code not working

 

If I try to copy data from one text field to another in the same pdf document that works

getField("Text1").value = doc.getField("Text1").value;

 

My question is how I can read text field value from doc2 and copy it into text field in doc1?

TOPICS
JavaScript , PDF forms
2.8K
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 11, 2024 Jan 11, 2024
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 11, 2024 Jan 11, 2024

in this code

var doc = app.openDoc("doc2.pdf", bHidden) ;
getField("Text1").value = doc.getField("Text1").value;

 

the bHidden value is not specified.  

Did you check the console for errors?

 

 

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
Guest
Jan 12, 2024 Jan 12, 2024

Screenshot_20240112_225810.jpg

 How is not specified? 🙂 Look at documentation. Lines are OK! They works without any errors. I can read value of any field and write it to another in the same doc. That not working with two different documents. openDoc opet pdf in show/hidden state but it seems that getField not working on ref document

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 12, 2024 Jan 12, 2024

You misunderstand.  In the script you posted, the value of the bHidden variable is not set.   

 

Also you haven't said if any errors were reported in the JavaScript console (Press Ctrl-j).  

I strongly suspect the "app.openDoc()" function is throwing an exception because the 2nd argument for this function is a document object that is used to resolve a relative path.  The "bHidden" argument is the 4th input. So Acrobat is likely choking on the 2nd input, which is the bHidden variable. Please refer to the reference entry you've posted above. 

 

I think what you meant to write was this:

 

var doc = app.openDoc({cPath:"doc2.pdf", bHidden:false}) ;

 

The Acrobat model allows some function arguments to be passed by object, using named parameters. Some functions in the model have huge numbers of entries, and this notation provides a method for entering only the needed arguments. 

 

 

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
Guest
Jan 12, 2024 Jan 12, 2024

Ohh. Sorry. Yes, it was misunderstand. I forgot to write that I tried also that. In documentation there is example for that.

If I try this...

var doc = app.openDoc("1.pdf");
app.alert(doc.getField("Text1").value);

First line will open the 1.pdf document but the message will not be executed. Why?

but...

var doc = app.openDoc("1.pdf");
app.alert("blabla");

Both lines will be executed.

 

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
Guest
Jan 12, 2024 Jan 12, 2024

Pls take a look at the example from the documentation

Untitled.png

This is my original code 
oDoc = app.openDoc({
cpath:"1.pdf",
bHidden:true
});
var v = oDoc.getField("Text1").value;
this.getField("Text1").value = v;
oDoc.closeDoc();

 

but not working... Do you know why?

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 12, 2024 Jan 12, 2024

Here is a tutorial on using the console window:

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.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
Community Expert ,
Jan 12, 2024 Jan 12, 2024
quote

 

var doc = app.openDoc("1.pdf");
app.alert(doc.getField("Text1").value);

First line will open the 1.pdf document but the message will not be executed. Why?

 

By @Deleted User

 

The probable reason the second line isn't run is that Acrobat throws an exception on the first line. This is the importance of looking in the console window. It will show any uncaught exceptions. 

 

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
Community Expert ,
Jan 12, 2024 Jan 12, 2024

In your original code "cpath" is spell incorrectly. JavaScript is a case sensitive language, so it's "cPath"

Here is the correct code:

 

oDoc = app.openDoc({
cPath:"1.pdf",
bHidden:true
});

 

However. your paths are not complete. You would be better served to use fully qualified file paths. 

This is also the type of thing that will cause Acrobat to throw an exception. 

 

 

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
Guest
Jan 12, 2024 Jan 12, 2024

Oh god... I didn't notice that. Yes, I know that some languages are case sesitive becase I work with C and C++. I don't have any experience with Java Script. This is my first project - I need to do some automation for a few PDF documents that share the same data.

 

var oDoc = app.openDoc({
cPath:"C:/Users/Administrator/Desktop/1.pdf",
bHidden:true
});
var v = oDoc.getField("Text1").value;
this.getField("Text1").value = v;
oDoc.closeDoc();

I looked at the Debuger and getting this error.

TypeError: oDoc is undefined
5:AcroForm:Button1:Annot1:MouseEnter:Action1

 

I really do not understand why it report that oDoc is undefined. This code open the reqired file but don't read value of required field.

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
Guest
Jan 12, 2024 Jan 12, 2024

I found this post on the forum related to this error
https://community.adobe.com/t5/acrobat-discussions/app-opendoc-return-typeerror-otherdoc-is-undefine...

In documentation

Untitled2.png

 

Untitled.png

I added that line as first line in the script. I have the same error.

 

this.addScript("Disclosed", "this.disclosed = true;");
var oDoc = app.openDoc({
cPath:"1.pdf",
bHidden:true
});
var v = oDoc.getField("Text1").value;
this.getField("Text1").value = v;
oDoc.closeDoc();

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 12, 2024 Jan 12, 2024

Open the document.

In the Javascript console execute:

this.disclosed = true;

Save the document.

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
Guest
Jan 12, 2024 Jan 12, 2024

I did that. 

 

NotAllowedError: Security settings prevent access to this property or method.
Doc.disclosed:1:Field Button1:Mouse Enter

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 13, 2024 Jan 13, 2024

Use the Javascript console:

Bild9.jpg

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 13, 2024 Jan 13, 2024

You need to place that code as a doc-level script, not run it from a button.

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 13, 2024 Jan 13, 2024
LATEST

So you've already seen that the "app.openDoc" only returns the document object when run from a privileged context, or if the document is disclosed, i.e., the document being opened is already disclosed. 

So, as pointed out by Try67, the document being opened must have a document level script that sets the disclosed property to true. The document has to have been setup with this script before it is opened from the script in the button.   This is I believe the final issue with your code. 

 

 

 

 

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