Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Reset only page 1 with a poppup

Explorer ,
Oct 21, 2022 Oct 21, 2022

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!

TOPICS
Create PDFs , Edit and convert PDFs , General troubleshooting , How to , JavaScript , PDF forms , Standards and accessibility
315
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Oct 21, 2022 Oct 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
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 ,
Oct 21, 2022 Oct 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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 ,
Oct 21, 2024 Oct 21, 2024
LATEST

Thank you Thom, very useful script!


Acrobate du PDF, InDesigner et Photoshopographe
Translate
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