Javascript to add form to page

New Here ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

Can I use javascript to add a form to each page of a PDF, same form, same location?

If so can someone point me to what I would use to do this?

Thanks

TOPICS
Acrobat SDK and JavaScript

Views

119

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

Same form, same field names, and same filled in values?

In Reader or Acrobat?

Likes

Translate

Translate

Report

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
New Here ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

Acrobat,

this.addField("1", "text", 0, [550,390,650,410]);

This is what I'm using now. I have 10 text form fields.  The others are just below the first.  I want to add a text annotation to the left of each field.  Can I use oCoords with this.addAnnot to put the description of each field to the left or is there a better way?

Also how can I loop adding form and annotations through each page of the document?

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

Form fields don't have "labels". You can add a separate, read-only text field next to each editable field with the label, though.

You can loop through the pages like this:

for (var p=0; p<this.numPages; p++) {

     this.addField("text"+p, "text", p, [550,390,650,410]);

}

This will add a unique text field on each page in the same location, called "text0", "text1", "text2", etc.

Likes

Translate

Translate

Report

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
New Here ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

I'm having a bit of trouble populating the fields on subsequent pages from a text delimited file, when I run this code below for each row of the text file, it's filling the form on the first page only then saving, and saving files as separate documents.

I'm trying to get the script to fill each pages form from the text file, page 1 = row 1 and save each page as separate docs

for (var p = 0; p < this.numPages; p+=1)

{

this.addField("1", "text", p, [550,390,650,410]),

this.addField("1label", "text", p, [450, 390, 545, 410]),

this.addField("2", "text", p, [550,368,650,388]),

this.addField("2label", "text", p, [450, 368, 545, 388]),

break;

}

var a = this.getField("1label");

a.value = "123";

a.readonly = true;

var b = this.getField("2label");

b.value = "123";

b.readonly = true;

// specify the filename of the data file

var fileName = "/c/users/form.txt"; // the tab delimited text file containing the data

var outputDir = "/c/users/"; // make sure this ends with a '/'

var err = 0;

var idx = 0;

while (err == 0) {

err = this.importTextData(fileName, idx); // imports the next record

if (err == -1)

app.alert("Error: Cannot Open File");

else if (err == -2)

app.alert("Error: Cannot Load Data");

// else if (err == -3)

// We are not reporting this error because it does

// indicate the end of our data table: We've exhausted

// all rows in the data file and therefore are done with

// processing the file. Time to exit this loop.

// app.alert("Error: Invalid Row");

else if (err == 1)

app.alert("Warning: User Cancelled File Select");

else if (err == 2)

app.alert("Warning: User Cancelled Row Select");

else if (err == 3)

app.alert("Warning: Missing Data");

else if (err == 0) {

this.saveAs(outputDir + this.getField("1").value + "_" + this.getField("2").value + ".pdf"); // saves the file

idx++;

}

}

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

LATEST

Each field must have a unique name if you want them to have unique values.

That probably means you can't import the data directly, though, unless you flatten each page after importing the data to it.

Likes

Translate

Translate

Report

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