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

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

Participant ,
Jan 19, 2024 Jan 19, 2024

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

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF , PDF forms
1.1K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Jan 20, 2024 Jan 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 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
Participant ,
Jan 21, 2024 Jan 21, 2024
LATEST

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");
}

 

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 ,
Jan 19, 2024 Jan 19, 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
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
Participant ,
Jan 20, 2024 Jan 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.

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 ,
Jan 19, 2024 Jan 19, 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 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 ,
Jan 20, 2024 Jan 20, 2024

@Thom Parker ,

 

Is the first page page 0?

ABAMBO | Hard- and Software Engineer | Photographer
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 ,
Jan 20, 2024 Jan 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 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
Participant ,
Jan 20, 2024 Jan 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.

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 ,
Jan 20, 2024 Jan 20, 2024

That's because the page number of the widget on a (hidden) template is -1, plus the sort method doesn't work well on an array of numbers. It treats them as strings. Try this code, instead:

 

 

 

var nLowestPage = f.page;
if (typeof(f.page)=="object") {
    var pageNumbers = f.page.sort(sortAscending);
	nLowestPage = pageNumbers.shift();
	while (nLowestPage<0 && pageNumbers.length>0) nLowestPage = pageNumbers.shift();
}
if (f.type !== "button" && f.required && (nLowestPage>=0 && nLowestPage < 2)) {
	// do something
}

function sortAscending(a,b) {return a-b;}

 

[Edited: Code fixed]

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
Participant ,
Jan 21, 2024 Jan 21, 2024

Thanks a lot @try67

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 ,
Jan 20, 2024 Jan 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 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
Participant ,
Jan 21, 2024 Jan 21, 2024
LATEST

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");
}

 

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