Copy link to clipboard
Copied
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");
}
}
Copy link to clipboard
Copied
What values does you use for s1s and s1e? How many pages has the document?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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");
}
}
Copy link to clipboard
Copied
Thanks for your help so far Bernd. Unfortunately i'm still getting a range error as it doesn't seem to like my variables in the this.extractPages method.
Any more ideas?
Copy link to clipboard
Copied
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);
app.alert(s1s + " " + s1e + " " + this.numPages);
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");
}
}
Copy link to clipboard
Copied
I'm getting the error:
Exception in line 8 of function extract, script Document-Level:extract
Exception in line 1 of function top_level, script AcroForm:GO:Annot1:MouseUp:Action1
RangeError: Invalid argument value.
Doc.extractPages:8:AcroForm:GO:Annot1:MouseUp:Action1
The alert is correctly telling me the values for s1s, s1e and the document length.
Copy link to clipboard
Copied
What happens when you remove the third parameter:
this.extractPages(s1s,s1e);
Copy link to clipboard
Copied
That works! It extracts the page range. How can I get it to save the file using a variable name?
var job = this.getField("jN").valueAsString;
...is the variable I have at the moment.
Ideally I need to have a section number in the file name. So if job is 12345, i need the filename to be 12345-1.pdf.
I thought:
this.extractPages(s1s,s1e, job + "-1.pdf");
...would've been correct but i'm obviously wrong.
Copy link to clipboard
Copied
You can use the third parameter when you use a trusted function.
Copy link to clipboard
Copied
I'm totally lost now Bernd. Does that mean what i'm trying to do is not possible through a form?
Copy link to clipboard
Copied
Change your function in a trusted function and save it in a folder level script:
https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm
Copy link to clipboard
Copied
Thanks for all you help Bernd.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.