Skip to main content
Inspiring
January 20, 2024
Answered

Checking Blank Fields in Specific Pages of a Multi-Page Document

  • January 20, 2024
  • 2 replies
  • 1618 views

Hi everyone,
Please, I would like your help to figure out how this can be done. Following one of the discussions here, I found the following code to check blank fields, which works amazingly.

var emptyFields = [];

for (var i = 0; i < this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));

if (f.type !== "button" && f.required) {
if (f.valueAsString === f.defaultValue) {
emptyFields.push(f.name);
}
}
}

if (emptyFields.length > 0) {
app.alert("Please complete the following fields:\n" + emptyFields.join("\n"));
} else {
app.execMenuItem("SaveAs");
}

However, since my document has 5 pages, I would like to check only the blank fields on the first 2 pages (Not all the document). How can this be specified within this code, please?

Thanks alot

This topic has been closed for replies.
Correct answer Kaxkul

I was going to mention the -1 page number for templates but figured it was a long shot you've have this issue. Assumptions always bite 😉  

Another assumption I made is that the lexical sort won't matter because you're only interested in single digit pages. 

But if you want to be totally correct, I think this code is more compact.

 

// This code filters the page list for pages (inclusively) between 0 and 1

var aPages = ((typeof(f.page)=="object")?f.page:[f.page]).filter(a=>a>-1 && a<2);

//if the list is not empty, then the field fits the requirement

if (f.type !== "button" && f.required && aPages.length) {

 

 


Thank you @Thom Parker much appreacited 🙂
I will go ahead with this being compact and bellow I will leave the full code for who would like to follow:

 

var emptyFields = [];

for (var i = 0; i < this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));

var aPages = (typeof(f.page) == "object") ? f.page : [f.page];
aPages = aPages.filter(a => a > -1 && a < 2);

// If the list is not empty, then the field fits the requirement
if (f.type !== "button" && f.required && aPages.length && f.required) {
if (f.valueAsString === f.defaultValue) {
emptyFields.push(f.name);
}
}
}

// Move the alert outside the loop
if (emptyFields.length > 0) {
app.alert("Please complete the following fields:\n" + emptyFields.join("\n"));
} else {
app.execMenuItem("SaveAs");
}

 

2 replies

Thom Parker
Community Expert
January 20, 2024

Add code to check the page number of the field. This is more difficult than you would think, because if a field appears on more than one page the field.page property is an array of page numbers. 

 

var nLowestPage = f.page;

if(typeof(f.page)=="object")

    nLowestPage = f.page.sort().shift();

 

if (f.type !== "button" && f.required && (nLowestPage < 2)) {

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
KaxkulAuthor
Inspiring
January 20, 2024

Thanks a lot @Thom Parker, I implemented but, something unclear is happening, and perhaps I should provide a better explanation.

My form consists of a total of 5 pages, all of which are templates except for Page 1:

  • Page 1: It includes a checkbox to accept terms and conditions. If the user checks 'yes,' the short form (spawned pages) is aggregated, and an additional page (Page 2) is added.

  • Page 2: This page features a button to check empty fields and save the page. It also includes a checkbox to aggregate the longer form (Page 3, 4, and 5).

  • Page 5: Here, there is another button to check empty fields, but this time it operates on the entire document and saves it.

When I applied your code, I observed that it works with normal pages but not with template-spawned pages. This led me to consider that it might be a different case.

Thom Parker
Community Expert
January 20, 2024

I was going to mention the -1 page number for templates but figured it was a long shot you've have this issue. Assumptions always bite 😉  

Another assumption I made is that the lexical sort won't matter because you're only interested in single digit pages. 

But if you want to be totally correct, I think this code is more compact.

 

// This code filters the page list for pages (inclusively) between 0 and 1

var aPages = ((typeof(f.page)=="object")?f.page:[f.page]).filter(a=>a>-1 && a<2);

//if the list is not empty, then the field fits the requirement

if (f.type !== "button" && f.required && aPages.length) {

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Abambo
Community Expert
January 20, 2024

You understand well, that this searches for empty required fields? If there are no required fields on page 3 ff, there will be no harm done. if there are required fields, they seem to be required, no?

ABAMBO | Hard- and Software Engineer | Photographer
KaxkulAuthor
Inspiring
January 20, 2024

Thanks @Abambo 
I have a two-part form. Users can either fill out the first part and submit it, or they can proceed to the second part and submit the entire form. That's why I'm looking to check both options.