Skip to main content
Inspiring
February 16, 2024
Question

Script help required to get the pages that are actually spawned to delete.

  • February 16, 2024
  • 2 replies
  • 423 views

Hi there,

 

I have a script that needs to run each time to delete the spawned pages in the pdf document. 

 

Unfortunately, it stops if any of the pages aren't spawned. Is there anyway to get the script to run and ignore the pages that aren't spawned. Basic I want to reset the spawned pages before more are spawned through the javascipt. 

 

var f = this.getField("Page text 5.1").page; this.deletePages(f);
var g = this.getField("Page text 6.1").page; this.deletePages(g);
var h = this.getField("Page text 7.1").page; this.deletePages(h);
var j = this.getField("Section4-5.1").page; this.deletePages(j);
var k = this.getField("Section4-6.1").page; this.deletePages(k);
var l = this.getField("Section4-7.1").page; this.deletePages(l);
var m = this.getField("Section4-8.1").page; this.deletePages(m);

 

Any help would be greatly appreciated. 

 

Steve

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
February 16, 2024

So what you need to do is to test for the existance of a template before trying to delete it.

 

The page number for a field on a template that is not spawned is -1.

However, it's not that easy because the "page" property is only a single number if there is only one instance of it. As soon as the page is spawned there are potentially two instances on the form. One on the hidden template and one on the spawned template.   So the "page" property is an an array of page numbers. You need to write a function to search the page array for a positive number. 

Of course this only works if the template is spawned without renaming fields. If the fields are renamed, then you'll need to search all field names for the one with the matching postfix. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
BarlaeDC
Community Expert
Community Expert
February 16, 2024

Hi,

 

you could wrap them in try{} catch(){} blocks and this way you can return an error, or at least handle when one has gone wrong, if you want them all to continue the I would recommend something like

 

```

try {

    var f = this.getField("Page text 5.1").page;

   this.deletePages(f);

} catch (e) {

console.println ( "ERROR occoured at f " + e);

}

```

if you create a try...catch for each it will tell you which one failed, but it should also allow the others to succeed.