Skip to main content
Participant
October 6, 2016
Answered

Compare PDF fields with imported xml in a loop

  • October 6, 2016
  • 1 reply
  • 748 views

Hello,

At the beginning I am a newbie in Acrobat and Javascript.

I have create a PDF-form with Acrobat Pro, my next idea is to fill the PDF with related data via an XML-Import.

Actual I can import the xml and fill the PDF-Fields hardcoded, this works already via Javascript.

My problem actual is, I want to fill the PDF with the XML-data via an for-loop, because we want to use this script in several PDF-forms and not each form has the same amount of fields.

Here my working script:

var xmldoc = util.readFileIntoStream();

var xmlstring = util.stringFromStream();

var myXML = XMLData.parse(xmlstring,false);

var oNode = XMLData.applyXPath(myXML,"//Customer");

this.getField("CustomerName").value=oNode.Customer.CustomerName.value;

this.getField("CustomerStreet").value=oNode.Customer.CustomerStreet.value;

this.getField("CustomerPlace").value=oNode.Customer.CustomerPlace.value;

this.getField("CustomerSiteName").value=oNode.Customer.CustomerSiteName.value;

this.getField("CustomerSiteStreet").value=oNode.Customer.CustomerSiteStreet.value;

this.getField("CustomerSitePlace").value=oNode.Customer.CustomerSitePlace.value;

Here my problem

for(var i=0;i<oNode.length;i++){

if(this.getField(????)==oNode.Order.???){

this.getField(???).value=oNode.Order.???.value; 

}

}

I hope anyone understand what I mean and can help me.

thx in advance

Regards

Dirk

This topic has been closed for replies.
Correct answer colt-ger

Hello try67,

Thank you.

In the first step I see, with getNthFieldName and numFields, all fields of the PDF now, but I have no idea how to access to the fields of the XML.

Thank you in advance.

1 reply

try67
Community Expert
Community Expert
October 6, 2016

Have a look at the getNthFieldName method and the numFields property of the Document object.
Using these two allows you to get the full list of names of the fields in the file dynamically, so you don't need to hard-code the names into your script.

colt-gerAuthorCorrect answer
Participant
October 6, 2016

Hello try67,

Thank you.

In the first step I see, with getNthFieldName and numFields, all fields of the PDF now, but I have no idea how to access to the fields of the XML.

Thank you in advance.

try67
Community Expert
Community Expert
October 6, 2016

If the values are all under the same node then you can do it like this (let's say the variable fieldName contains the name of the field you want to access):

oNode.Customer[fieldName].value