Skip to main content
Inspiring
October 21, 2022
Answered

Reset only page 1 with a poppup

  • October 21, 2022
  • 2 replies
  • 394 views

Hello!

 

I would like to have a button on each page to only reset THAT page but first have a popup that asked are you sure you want to reset?

I found a JavaScript that has the poppup and works, but it resets the ENTIRE document.

 

if( app.alert({
cMsg: "Are you sure you want to clear this form?",
cTitle: "Reset Form Warning",
nIcon: 2,
nType: 2
}) == 4) {
this.resetForm();
}

 

Can someone help? I acually don't know how to write Javascript (just learning), so if you could explain what you write, I would LOVE that!

Correct answer Thom Parker

Identifying fields by page number is not the greatest technique. It's better to group fields based on the required functionality or data relationship.  Then the fields can be manipulated in a way that is consistent with the forms intent, or the required data structure. 

But if you must go by page you can use this function to identify all fields that are on a page.

 

// if bExclusive is true, then it only returns fields that are exclusive to this page
function GetFieldsOnPage(oDoc, nPage, bExclusive)
{
   var aFldList = [], oFld,aPgs;
   for(var i=0;i<oDoc.numFields;i++){
      oFld = oDoc.getField(oDoc.getNthFieldName(i)));
      aPgs = (typeof(oFld.page) == "number")?[oFld.page]:oFld.page;
      if(bExclusive?aPgs.every(nPg=> nPg == nPage):aPgs.some(nPg=> nPg == nPage))
         aFldList.push(oFld);
   }
   return aFldList;
}

 

Assuming that the name of the reset button is unique for the entire form, this code will return the page number the button is on.

var nPg = event.target.page;

 

Resetting the fields can be done in a couple of different ways, here's one just sets each field to it's default value.

 

GetFieldsOnPage(this,nPg,true).forEach(a=>a.value = a.defaultValue);

 

Here's code that uses the resetForm Function

 

this.resetForm.apply(this, GetFieldsOnPage(this,nPg,true).map(a=>a.name) );

 

 

2 replies

JR Boulay
Community Expert
October 21, 2024

Thank you Thom, very useful script!

Acrobate du PDF, InDesigner et Photoshopographe
Thom Parker
Thom ParkerCorrect answer
Community Expert
October 21, 2022

Identifying fields by page number is not the greatest technique. It's better to group fields based on the required functionality or data relationship.  Then the fields can be manipulated in a way that is consistent with the forms intent, or the required data structure. 

But if you must go by page you can use this function to identify all fields that are on a page.

 

// if bExclusive is true, then it only returns fields that are exclusive to this page
function GetFieldsOnPage(oDoc, nPage, bExclusive)
{
   var aFldList = [], oFld,aPgs;
   for(var i=0;i<oDoc.numFields;i++){
      oFld = oDoc.getField(oDoc.getNthFieldName(i)));
      aPgs = (typeof(oFld.page) == "number")?[oFld.page]:oFld.page;
      if(bExclusive?aPgs.every(nPg=> nPg == nPage):aPgs.some(nPg=> nPg == nPage))
         aFldList.push(oFld);
   }
   return aFldList;
}

 

Assuming that the name of the reset button is unique for the entire form, this code will return the page number the button is on.

var nPg = event.target.page;

 

Resetting the fields can be done in a couple of different ways, here's one just sets each field to it's default value.

 

GetFieldsOnPage(this,nPg,true).forEach(a=>a.value = a.defaultValue);

 

Here's code that uses the resetForm Function

 

this.resetForm.apply(this, GetFieldsOnPage(this,nPg,true).map(a=>a.name) );

 

 

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