These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Problems with javascript adding pages

prgittech702
Registered: Sep 23 2011
Posts: 7
Answered

Hi All,
 
I'm trying to get the javascript console to insert a PDF page from one of our network drives into another PDF document....but after each page. I can get the script to insert before the document or after the document, but I'm lost on what needs to happen to insert the same page more than once.
 
I'm going to then use that script into the action wizard. It's going to make two-sided printing a lot easier.
 
I was hoping someone could give me some insight on what I should be looking at to do this.
 
Also....the one page document is always the same, but the second PDF is not always the same amount of pages.

My Product Information:
Acrobat Pro 10.1, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Just run the same line of code multiple times to insert the same page multiple times.

If the number of inserted pages is variable then put the insertPages code into a loop.

Another option is to create the "insert" PDF with multiple repeats of the same page. Then insert the needed number by adjusting the nEnd value in the "insertPages()" function.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

prgittech702
Registered: Sep 23 2011
Posts: 7
I apologize. I'm really trying to learn javascript and I understand it to a certain extent. I know I can do this because, in my head, it seems like a simple code. However...I seem to be getting the same error over and over.

This is my code:

this.insertPages({
nPage: 1,
cPath:"P:\LOGOS\LOGOS old\logos\Invoice\RENTAL AGREEMENT.pdf",
nStart: 1,
nEnd: 1
})

So...this is saying that I would like the 1st (and only) page of the first document to be inserted after the first page of the second document.

I'm getting this error though.


RaiseError: A file error has occurred.
Doc.insertPages:7:Batch undefined:Exec
===> A file error has occurred.I'm not sure what that means. The code looks right, doesn't it? Is it the filepath?

thomp
Expert
Registered: Feb 15 2006
Posts: 4411

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

prgittech702
Registered: Sep 23 2011
Posts: 7
Ahhh...I didn't know that part.

Got it now though. It's still giving me an error. It's inserting the pages, but I think because there are less pages than script "npage"....it's giving an error because it doesn't know what to do.

Because of my knowledge of java, I'm just coping and pasting:

this.insertPages({
nPage: 0,
cPath:"/P/PRG LOGOS/PRG LOGOS old/logos/Invoice/RENTAL AGREEMENT PRG Las Vegas.pdf"
})

and changing the "0" to a "2" and a "4" and so on.....

How do I put it into a loop to just stop when it gets to no more odd number pages? Let's say there's 5 pages to begin with, it inserts a page after each, and it ends up with 10 pages, but there is no 11.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
Ok, you want to insert every other page. The script has to start at the end of the file and go backwards so that the page numbers are stable.


Something like this

// Get the last page number
var nStartPage = this.numPages-1;

// Loop from end
for(var i=nStart;i>=0;i--)
this.insertPages({nPage:i, ...);



Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

prgittech702
Registered: Sep 23 2011
Posts: 7
Just to be thorough, this is what the final code should look like:

/* INSERT RENTAL AGREEMENT */

// Get the last page number
var nStartPage = this.numPages-1;

// Loop from end
for(var i=nStart;i>=0;i--)
this.insertPages({
nPage:i,
cPath:"/P/PRG LOGOS/PRG LOGOS old/logos/Invoice/RENTAL AGREEMENT PRG Las Vegas.pdf"
})

Everything looks right to me, but I'm getting a

"7:Batch:Exec
ReferenceError: nStart is not defined"

But it is defined, isn't it?

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Well, I made a mistake in the script:( The name of the parameter that is defined is "nStartPage". This is the parameter that should be used in the for loop.

If you are going to be doing scripting then you really need to be able to figure these things out. This is an easy one.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

prgittech702
Registered: Sep 23 2011
Posts: 7
Wow....you're absolutely right. I can't believe I didn't see that.

This is an example of an IT guy venturing into dangerous waters. I appreciate your help. It makes total sense now.

Thanks again!