Copy link to clipboard
Copied
I ran into a problem-can someone please help me?
i'm trying to write a javascript for bookmark.
my desire outcom would be:
1- extract the desire pages when clicked on the bookmark
2. make no changes to my original doc.
here is my code:
var pageNumbers = [0,1,20];
var newDoc = this.extractPages({
nStart: pageNumbers[0],
nEnd: pageNumbers[pageNumbers.length - 1]
});
// Save the new document (optional)
newDoc.saveAs("/C/Users/YourUserName/Documents/ExtractedPages.pdf");
// Close the new document (optional, if you don't want to leave it open)
newDoc.closeDoc();
the expected outcome is to extract and shows pages 1,2 and 21
but the issue is, it shows pages 1 to 21, i mean all the pages.
can you please correct my code inorder to shows only the 3 pages i asked the code.
thanks.
Copy link to clipboard
Copied
Take a look in the Console Window. There will be a security error reported.
The SaveAs function can only be called from a privileged context.
See this article:
https://www.pdfscripting.com/public/How-to-Save-a-PDF-2.cfm
Copy link to clipboard
Copied
nStart:pageNumbers[0] is 0, the first page.
nEnd: pageNumbers[pageNumbers.length-1] is 20
That means you are extracting pages 0-20 (or pages 1 through 21). If you want to extract pages 1,2 and 21 you will have to either extract pages 1 and 2, then use insertPages on the new doc to get page 21, or extract pages 1 through 21 and delete pages 3 through 20 like this:
var newDoc = this.extractPages({
nStart: pageNumbers[0],
nEnd: pageNumbers[pageNumbers.length - 1]
});
newDoc.deletePages(2,19);
As @Thom Parker stated, you can't save silently unless you are calling SaveAs from a privileged context.
Copy link to clipboard
Copied
var pageNumbers = [0,1,20];
var newDoc = this.extractPages({
nStart: pageNumbers[0],
nEnd: pageNumbers[pageNumbers.length - 1]
});
newDoc.deletePages(2,19);
newDoc.saveAs("/C/Users/YourUserName/Documents/ExtractedPages.pdf");
newDoc.closeDoc();
this line worked. finally
thanks guys
Copy link to clipboard
Copied
i ran into problem!
i'll be greatful if you could help me.
var pageNumbers = [0,1,20];
var newDoc = this.extractPages({
nStart: pageNumbers[0],
nEnd: pageNumbers[pageNumbers.length - 1]
});
newDoc.deletePages(2,19);
newDoc.saveAs("/C/Users/YourUserName/Documents/ExtractedPages.pdf");
newDoc.closeDoc();
when i run this script everything works smoothlly as long as "var pageNumbers = [0,1,20];" starts with page 0.
when i try to do for example :
var pageNumbers = [3,4,10];
var newDoc = this.extractPages({
nStart: pageNumbers[0],
nEnd: pageNumbers[pageNumbers.length - 1]
});
newDoc.deletePages(5,9);
newDoc.saveAs("/C/Users/YourUserName/Documents/ExtractedPages.pdf");
newDoc.closeDoc();
it does not delete the pages!! and shows all 8 pages
please help
Copy link to clipboard
Copied
You will have an error because you are trying to delete a range of pages in which the last page exceeds the number of pages in the document. In your example newDoc will have be an 8-page document (extracted page index numbers 3 through 10 with new page index numbers 0 through 7). Then you are trying to delete 5 through 9. There is no 9. Getting the page numbers from an array is confusing the issue.
pageNumbers[0] is the first array item (3 in your example). pageNumbers[pageNumbers.length-1] is the last array item (10 in your example). When you extract pages into a new Doc the page numbering is 0 to the number of pages minus 1.