Spawning multiple templates and removing again

New Here ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

Hello all,

 

I'm working on a document in which I have checkboxes that when selected, spawn templates at a specified point. Then when clicked remove them again 

 

I need to be able to spawn multiple templates now though as some templates will be more than one page. So when the checkbox is clicked, it spawns say 2-3 templates, then when clicked deletes them again. 

 

The code below is what I'm using at the minute for a single page and works great, Im just not too sure how to adapt it for more than one template

 

if (event.target.value!="Off")

this.getTemplate("Section1a").spawn(this.getField("Marker1").page+1, true, false);

else this.deletePages(this.getField("Marker1").page+1, this.getField("Marker1").page+1);

 

Thanks for any help

TOPICS
Edit and convert PDFs , JavaScript , PDF forms

Views

78

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
Community Expert ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

A variation on your existing code will work. 

 

 

var oPage, oPgTmpl = this.getTemplate("Section1a");
var nTgtPg = this.getField("Marker1").page;
if (event.target.value!="Off"){
   oPage = oPgTmpl .spawn(nTgtPg+1, true, false);
   oPgTmpl.spawn(nTgtPg+2, true, false,oPage );
   oPgTmpl.spawn(nTgtPg+3, true, false,oPage );
}else 
   this.deletePages(nTgtPg+1, nTgtPg+3);

 

 

 

 

 

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

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Thanks for your help, it doesn't seem to work my end though. I've used the below but it's not producing any results which is strange as it all looks like it should work

 

var oPage; if (event.target.value!="Off"){ oPage = this.getTemplate("Section1a").spawn(this.getField("Marker1")+1, true, false); this.getTemplate("Section1b").spawn(this.getField("Marker1")+2, true, false,oPage ); this.getTemplate("Section1c").spawn(this.getField("Marker1")+3, true, false,oPage ); }else this.deletePages(this.getField("Marker1").page+1, this.getField("Marker1").page+3);

 

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
Community Expert ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

This will not work:

this.getField("Marker1")+1

Want you use the page or the value of the field?

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
Community Expert ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Bernd is correct, the ".page" property is missing. You need do a little debug on your script. Always start by looking in the console window.

 

 

 

 

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

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
Community Expert ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Beside the issues with your code, what you're trying to do is not going to work properly.

I'll try to explain. Unless you always spawn the pages to the end of the file, if you spawn two pages, then delete one of them, then spawn them again, you'll end up with pages that have duplicate field names (and therefore the fields will be copies of one another). This is because of the way the fields are renamed, which uses the original file name, the template name and the page number to which it is spawned. Since the first two are always the same, it's the latter that has to be unique for the field names to be renamed correctly.

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 ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

Hi, Thank for explaining

 

I don't fully understand though. The field name/marker are on pages in the document that won't be removed/changed, I just need templates to be spawned after these pages. Surely there's way of doing this with more than one page at a time?

 

This is what in my head would produce what I'm after, but I'm just stuck on the code. So Marker 1 is on a page within the document that won't change. I then need Template 1 and 2 to spawn after this

 

if (event.target.value!="Off")

this.getTemplate("Template1").spawn(this.getField("Marker1").page+1, true, false);
this.getTemplate("Template2").spawn(this.getField("Marker1").page+2, true, false);

else this.deletePages(this.getField("Marker1").page+1, this.getField("Marker1").page+1);

 

Thanks again for any help. I really need to try and get this to work

 

 

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 ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

Sorry a little update. So the below works to generate what I need. I just can't figure out how to remove them again as the code produces a SyntaxError when I add the deletePages line

 

if (event.target.value!="Off")

this.getTemplate("Template1").spawn(this.getField("Marker1").page+1, true, false);
this.getTemplate("Template2").spawn(this.getField("Marker1").page+2, true, false);

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
Community Expert ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

LATEST

You should have used the code I provided as written. 

The block markers are missing from your code, so the "if" only extends to the following line. So the second "spawn" line is not inside the if.  Go back and look at the code I wrote and you'll see.  

 

 

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

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

Make content for your business needs with Adobe Express.

Get started easily with free templates: