3

I have searched and found out code snippet for getting a value for particular field in a PDF using javascript in Acrobat.

But I need to get all fields as key-value pair in javascript. I tried some of the code but it won't work for me.

if(this.hostContainer) {    
        var names = new Array();    
        names[0] = this.getField("personal.name").value.toString(); 
        names[1] = this.getField("personal.loginname").value.toString();    
        try{    
            this.hostContainer.postMessage(names);  
        }   
        catch(e){   
            app.alert(e.message);   
        }   
}

PDF file looks like below,

Name : x (editable)

Age : 36 (editable)

Sex : male (editable)

But the form fields in PDF may vary.

Boaz
  • 19,892
  • 8
  • 62
  • 70
sankar
  • 161
  • 13
  • Please elaborate on `it won't work for me`. – Boaz Feb 19 '15 at 08:22
  • Unable to get all fields key value pairs.I didn't see any java script methods to get all key value pairs – sankar Feb 19 '15 at 08:26
  • If you have additional information, please add it to your question. It will make it clearer and increase the likelihood of people answering it. – Boaz Feb 19 '15 at 08:27
  • Unable to get key value pair in java script. The sample pdf looks like above.but the pdf may vary (both key and value). How can i get this? – sankar Feb 19 '15 at 08:34

1 Answers1

0

A very simple way is as follows; you run this code from the Console:

for (var i = 0 ; i < this.numFields ; i++) {
var fn = this.getNthFieldName(i) ;
if (fn.type != "button") {
console.println("Name: " + fn + "   Value: " + this.getField(fn).valueAsString) 
}
}

And that should do it.

If you want to create a report, you could use the Report object, or you could create a Data Object (in either case, you would write to the target, and not to the console).

Max Wyss
  • 3,549
  • 2
  • 20
  • 26
  • Already i have gone through the above code snippet. But my problem is "How can i get the pdf document object " in java script function. – sankar Feb 20 '15 at 05:36
  • If it is the document itself, you would use "this"; if it is another document, you get its doc object with app.openDoc() ; you might have to set the disclosed flag of the other document to true, in order to have access to it. – Max Wyss Feb 20 '15 at 14:20