Skip to main content
Participating Frequently
August 23, 2018
Question

Using Javascript to add pages to a PDF?

  • August 23, 2018
  • 2 replies
  • 9255 views

Hello!

I'm trying to figure out if it's possible to use the Javascript capabilities of some form fields to auto-insert pages into a PDF.

For example, if I import data into a field and the field value is "x", is there a way to execute a script that will automatically insert a separate PDF page (Blue.pdf) at the end of the document?  If the value is "y", insert another, separate page (Red.pdf)?  If the value is "z", insert even another page (Green.pdf)?

If not based off of an imported value, what about if I use checkboxes to execute the script?  If the box is checked, then it inserts the page.

I'm not sure if I'm even asking this correctly, as I'm relatively new to the deeper capabilities of Acrobat.  I appreciate any insight you fine folks can offer.

Thank you!

This topic has been closed for replies.

2 replies

Inspiring
July 21, 2020

If the pages are predetermined you can use the spawning feature of Acrobat that will work with reader. First,  you will need to add all the pages to the document and go to the 'Organize Pages' Tab. Second, Highlight the first page you want to auto-insert then Go to the 'More' drop down box and select  Page Templates. A box will appear - Name the page and select 'Add'. Uncheck the box next to the newly added page to hide it from view. Do this for every page you want to insert based on field values. to spawn the pages based on inputs create a button to execute the script to spawn the pages based on your criteria.

// Assign Templates to be spawned a variable so they may be recalled;
var a = this.getTemplate("PageA");
var b = this.getTemplate("PageB");
var c = this.getTemplate("PageC");

var resp1 = this.getField("Input").valueAsString;

 

if (resp1 == "x") {
a.spawn({nPage:this.numPages-1,bRename:false,bOverlay:false});
}
if (resp1 =="y") {
b.spawn({nPage:this.numPages-1,bRename:false,bOverlay:false});
}
if (resp1 == "z" ) {
c.spawn({nPage:this.numPages-1,bRename:false,bOverlay:false});

try67
Community Expert
Community Expert
August 23, 2018

Yes, this is possible, but it's tricky. Importing data to the file won't trigger it, most likely. You'll probably need to press a button or something to initiate it. Also, it requires installing a script on the local machine, as well as knowing the exact (or relative) path of the files to import.
And of course it can't work in Reader, only in Acrobat.

The basic code to do it would be:

if (this.getField("Text1").valueAsString == "x") this.insertPages(this.numPages-1, "Blue.pdf");

else if (this.getField("Text1").valueAsString == "y") this.insertPages(this.numPages-1, "Red.pdf");

Participating Frequently
August 23, 2018

If importing isn't a great way to go... maybe a set of "invisible", non-printing buttons that the user could click?

In the properties of the button, would I do Actions -> On Focus -> Run a JavaScript?

If I went that route, would I just use this part:

this.insertPages(this.numPages-1, "Blue.pdf")

I tried it on my test PDF and it doesn't seem to work.

Thank you!

try67
Community Expert
Community Expert
August 23, 2018

MouseUp is a better option, but as I said, it's not that simple, because the insertPages command can only be used from a privileged context, ie a folder-level script.