Skip to main content
New Participant
December 20, 2019
Question

How to insert templates into a specific place within PDF document

  • December 20, 2019
  • 1 reply
  • 2060 views

Hi,

 

I am trying to create an interactive PDF which add/deletes pages in a specific order based on a number of check boxes. Currently I can add and delete pages using a code similar to the following - which I have pieced together from a number of threads:

 

if(event.target.value!="Off")
{this.getTemplate("Template1" ).spawn();}
else
for (var p=this.numPages-1; p>=0; p--) {
if (this.numPages==1) break;
for (var n=0; n<this.getPageNumWords(p)-1; n++) {
if (this.getPageNthWord(p, n) == "FirstWord" && this.getPageNthWord(p, n+1) == "SecondWord" && this.getPageNthWord(p, n+2) == "ThirdWord") {
this.deletePages(p);
break;
}
}
}

 

However, the order in which these templates are placed into the document is important and if a check box is checked and then unchecked, the order is lost and templates are just added to the end of the document. I have attempted to specify the page that templates are inserted using: .spawn({nPage:1}) but this has led to pages being inserted on top of one another becoming illegible. Does anyone have any ideas on how to fix this?

 

Any help would be appreciated!

This topic has been closed for replies.

1 reply

Thom Parker
Inspiring
December 20, 2019

Please read the JavaScript reference entry for the "template.spawn()" function.

 

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2FTemplate_methods.htm%23TOC_spawnbc-1&rhtocid=_6_1_8_74_1_0

 

So to develop a method for inserting pages in the correct locations, you need a way to identify those locations. Using words on the page is one way, but a better method is field names. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ZF94Author
New Participant
December 23, 2019

Hi Thom,

 

I've got it thanks for replying! I do have one further query though. The script for deleting pages based off of a string of words takes a long time to process, do you have any recommendations of faster (less process heavy) methods? Would this be better based off field names too?

 

Thanks again

Thom Parker
Inspiring
December 23, 2019

Yes exactly, if each page contains a uniquely idenitifiable field, then is a simple and fast operation to get the field object, and then the page the field is on. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often