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
Abambo
Community Expert
January 20, 2024

@Thom Parker ,

 

Is the first page page 0?

ABAMBO | Hard- and Software Engineer | Photographer
Thom Parker
Community Expert
January 20, 2024
quote

@Thom Parker ,

 

Is the first page page 0?


By @Abambo

 

Yes, page numbers in the scripting model start at 0.

 

 

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.