Skip to main content
Sturm359
Inspiring
September 21, 2016
Answered

Insert all pages to the end of a document

  • September 21, 2016
  • 4 replies
  • 1523 views

I'm attempting to do something that *should* be easy: Add all of the pages from document A to the end of document B. There is even a built-in command for copying pages from one document to another: Doc.insertPages. My problem lies in the limits of that method.

See, whenever this script will be run, I do not know how many pages are in document A beforehand. Doc.insertPages lets me specify the first and last page that I want to copy over, but it only expects a number. Also, sadly, using -1 to specify the last one doesn't work for page index numbers the way it works for an Array's index number. So here's what I have so far:

printDoc.insertPages({

    nPage: printDoc.numPages - 1,

    cPath: labelFile,

    nStart: 0,

    nEnd: -1

});

Here, printDoc is my document B, and labelFile is my document A. I can easily tell the method to insert the pages after that last existing page in document B by calling its numPages property and subtracting 1. After all, the document is already open and Acrobat can count the pages in it. Not so for document A; the Doc.insertPages method doesn't open up document A--it only wants a String path to it. Thus, I cannot tell ahead of time how many pages are in it.

I suppose I *could* just have the script open up document A temporarily, just long enough to store its number of pages into a variable, which will then be used in the Doc.insertPages method, but I was hoping for something a bit more tidy than that. Thoughts/ideas?

This topic has been closed for replies.
Correct answer George_Johnson

As the documentation states: "If a page range is not specified, the method gets all pages in the source document."

So simply don't specify nStart and nEnd. More info: https://acrobatusers.com/tutorials/inserting-pages-pdf-acrobat-javascript1

4 replies

George_JohnsonCorrect answer
Inspiring
September 21, 2016

As the documentation states: "If a page range is not specified, the method gets all pages in the source document."

So simply don't specify nStart and nEnd. More info: https://acrobatusers.com/tutorials/inserting-pages-pdf-acrobat-javascript1

Sturm359
Sturm359Author
Inspiring
September 21, 2016

Wow. I can't believe I completely didn't absorb that line in the documentation when I read it. Or didn't read it at all. In any case, I feel like a fool now. Sorry to have bothered you with something so trivial, George_Johnson​. But thank you immensely for helping, and so quickly, too!

Although, a thought experiment comes to me that would invalidate that possibility. What would someone do if they needed to copy over all pages *except* the first page? My guess is that you'd have to do that "Pre-open document A and count the pages" trick. Your thoughts?

Inspiring
September 21, 2016

You can copy all of the pages and then delete (doc.deletePages) the one(s) you don't want.