Skip to main content
martynd54407871
Participant
April 12, 2021
Answered

Deleting multiple spawned pages - Adobe Pro DC

  • April 12, 2021
  • 3 replies
  • 1128 views

Hi everyone. 

I have created an application form (pages 1-3) which the applicant fills out. I have a hidden button which spawns a permit (pages 4-7)  - all four pages appear together. Re-clicking the button deletes pages 4-7, but it deletes one page at a time i.e. requires four clicks. Is there a way to delete pages 4-7 with one click? Using Adobe Acrobat DC, Windows 10.

Here is the script I'm using:

 

// This script is activated by "Mouse-Up" on a hidden button

// Get a reference to the template


var t1 = getTemplate("Pg4");
var t2 = getTemplate ("Pg5");
var t3 = getTemplate ("Pg6");
var t4 = getTemplate ("Pg7");
var spwndel = this.numPages;
// Create a new page, but only if there's currently only three pages in this document;

if (numPages == 3)
{


t1.spawn({nPage: numPages, bRename: true, bOverlay: false});
t2.spawn({nPage: numPages, bRename: true, bOverlay: false});
t3.spawn({nPage: numPages, bRename: true, bOverlay: false});
t4.spawn({nPage: numPages, bRename: true, bOverlay: false});
// this piece works - it adds the correct pages
} else
{

// this piece deletes pagespages 4, 5, 6 and 7 , but have to click 4 times

if(numPages>3)
{
deletePages(3);
deletePages(4);
deletePages(5);
deletePages(6);

}

}

 

Any help gratefully received!

 

This topic has been closed for replies.
Correct answer Thom Parker

The action you are seeing is a bit odd. If you follow the logic, this code is expected to fail, just not in the way you have described.  For example, when page 3 is deleted the document is shortened by one page, so there is no page 6. However, there is a page 4, so I would expect two pages to be deleted before the failure. 

Have you looked in the console window to see what errors are reported, because there are errors. 

 

Here is a "dressed" (i.e. neater) version of your code.

if (this.numPages == 3)
{
    this.getTemplate("Pg4").spawn(this.numPages,false, false);
    this.getTemplate("Pg5").spawn(this.numPages,false, false);
    this.getTemplate("Pg6").spawn(this.numPages,false, false);
    this.getTemplate("Pg7").spawn(this.numPages,false, false);
} else if(numPages>3)
{
    this.deletePages(3,6);
}

 

Note that the renaming argument is false. There is no need to rename fields if the template will only ever be spawned once. 

3 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 12, 2021

The action you are seeing is a bit odd. If you follow the logic, this code is expected to fail, just not in the way you have described.  For example, when page 3 is deleted the document is shortened by one page, so there is no page 6. However, there is a page 4, so I would expect two pages to be deleted before the failure. 

Have you looked in the console window to see what errors are reported, because there are errors. 

 

Here is a "dressed" (i.e. neater) version of your code.

if (this.numPages == 3)
{
    this.getTemplate("Pg4").spawn(this.numPages,false, false);
    this.getTemplate("Pg5").spawn(this.numPages,false, false);
    this.getTemplate("Pg6").spawn(this.numPages,false, false);
    this.getTemplate("Pg7").spawn(this.numPages,false, false);
} else if(numPages>3)
{
    this.deletePages(3,6);
}

 

Note that the renaming argument is false. There is no need to rename fields if the template will only ever be spawned once. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
martynd54407871
Participant
April 12, 2021

Thom - thank you so much! Worked like a charm. Here is the full working script:

// Get a reference to the template

var t1 = getTemplate("Pg4");
var t2 = getTemplate ("Pg5");
var t3 = getTemplate ("Pg6");
var t4 = getTemplate ("Pg7");
var spwndel = this.numPages;
// Create a new page, but only if there's currently only three pages in this document;

if (numPages == 3)
{


t1.spawn({nPage: numPages, bRename: true, bOverlay: false});
t2.spawn({nPage: numPages, bRename: true, bOverlay: false});
t3.spawn({nPage: numPages, bRename: true, bOverlay: false});
t4.spawn({nPage: numPages, bRename: true, bOverlay: false});
// this piece works - it adds the correct pages
} else

{
if(numPages>3)
this.deletePages(3, 6);
}

 

Once again, many thanks!

Thom Parker
Community Expert
Community Expert
April 12, 2021

Try this:

 

this.deletePages(3,6);

 

You are also missing an explicit document object reference from the document function calls. It is a good practice to include "this".   🙂  pun intended.

 

Here's the reference entry for the deletePages function 

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2FDoc_methods.htm%23TOC_deletePagesbc-20&rhtocid=_6_1_8_23_1_19

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
LinSims
Community Expert
Community Expert
April 12, 2021

Let me move this to the Acrobat forum for you, which is the appropriate forum for your question.

The Using the Community forum is for help in using the Adobe Support Community forums, not for help with specific programs. Product questions should be posted in the associated product community.