Skip to main content
Participating Frequently
August 14, 2020
Question

Javascript extractPages with variables?

  • August 14, 2020
  • 2 replies
  • 1482 views

I'm creating a form to extract pages but i'm having some trouble with it. The page range form fields are called s1s and s1e but the error is saying that its an invalid range. Can anyone help?

Code:

function extract(){
var job = this.getField("jN").valueAsString;
if(this.getField("s1s").valueAsString!=="" && this.getField("s1e").valueAsString!==""){
var s1s = Number(this.getField("s1s").valueAsString);
var s1e = Number(this.getField("s1e").valueAsString);
this.extractPages(s1s,s1e, job + "-1.pdf");
}

}

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
August 14, 2020

Remember that page numbers in JavaScript are zero-based, so if the user enters that they want to extract pages 1-5 you have to convert it to 0-4 in your code for it to work correctly.

SWinter82Author
Participating Frequently
August 17, 2020

I plan to insert the document I need splitting after my form page making my form page 0 and the first page of the document I want to split as 1. I'm new to javascript in acrobat and i'm sure there is probably a lot easier / more efficient way of doing it. 

 

In my example code I have s1s and s1e but in the form it continues s2s, s2e all the way up to s16s and s16e.

Bernd Alheit
Community Expert
Community Expert
August 14, 2020

What values does you use for s1s and s1e? How many pages has the document?

SWinter82Author
Participating Frequently
August 17, 2020

s1s and s1e will have a each number value. The document lengths vary. My idea was to insert the document I want to divide into sections after this form page, fill out my fields and then split the document. 

Bernd Alheit
Community Expert
Community Expert
August 17, 2020

Try this:

function extract(){
var job = this.getField("jN").valueAsString;
if(this.getField("s1s").valueAsString!=="" && this.getField("s1e").valueAsString!==""){
var s1s = Number(this.getField("s1s").valueAsString);
var s1e = Number(this.getField("s1e").valueAsString);
if (s1s > 0 && s1s < this.numPages && s1e > 0 && s1e < this.numPages)
  this.extractPages(s1s,s1e, job + "-1.pdf");
else
  app.alert("Numbers out of range");
}
}