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

Javascript to replace pages

carisima
Registered: Apr 25 2008
Posts: 21
Answered

I'm trying to update a report by replacing each page with the updated monthly page. My preference is to replace rather than insert, that way I can keep the bookmark destinations in the table of content. When I insert pages, the bookmarks in the individual PDF files are lost (they don't come in).

I used the following script, but it's not working. The script runs, but when I look at the file, the pages aren't replaced. Can someone tell me what I'm doing wrong? I don't know much about coding; I took bits and pieces from the Adobe's guide, reference, and the forum.

Here is a snippet of my code:

============================
app.openDoc({
cPath:"/k/coco/pdf mktg internal reports/working folder/copy of AJO_Internal_Reports.pdf"
});

this.replacePages({
nPage: 37,
cPath:"/k/coco/pdf mktg internal reports/working folder/copy of 21_Portfolios Gained.pdf",
nStart: 1,
nEnd: 3
});

this.replacePages({
nPage: 40,
cPath:"/k/coco/pdf mktg internal reports/working folder/copy of 22_Portfolios Lost.pdf",
nStart: 1,
nEnd: 2
});

currentDoc.closeDoc(false);

//note: the above code is used for all the other pages

===========================

Thanks in advance.

My Product Information:
Acrobat Pro 7.0.5, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
There is no saving of the updated PDF.

George Kaiser

carisima
Registered: Apr 25 2008
Posts: 21
I opted to do a manual save; I coded it to leave the document open so I can proof it as soon as the script is done. Do you have to save when you finish a script? I added the save coding, but it still doesn't come out right.

Thanks for your help.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Is the problem that the pages are not inserted at all or that they don't have bookmarks anymore?
If the former you should check your file path and save before closing the document (as suggested).
If the latter, I think it can't be done just like that. Bookmarks in a PDF are not a part of a page. They are an independent entity that can POINT to a page. If you have existing bookmarks that you want to preserve I think you should have them point to a specific page number and not use destinations. Then it *might* work.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

carisima
Registered: Apr 25 2008
Posts: 21
Yes, the pages wouldn't replace (even though the script runs with no errors). Also, I have it saving to the original folder/document during the sequence.

Regarding the bookmark, I used the guide to create a script to add them.

I'll keep trying. If I resolve it in the meantime, will let you know.

Thanks for the help.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Make sure the setting to open the console on errors and warning is checked (Preferences-JavaScript).
It's possible something is going wrong but you don't see it.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

carisima
Registered: Apr 25 2008
Posts: 21
Hey, you're right. I changed the settings and this is the error I get:

TypeError: Invalid argument type.
Global.replacePages:28:Batch undefined:Exec
===> Parameter nPage.Exception in line 28 of function top_level, script Batch:ExecTypeError: Invalid argument type.
Global.replacePages:28:Batch undefined:Exec
===> Parameter nEnd.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The parameters nStart and nEnd are 0-based. This means that the first page of the document is number 0 and the last (let's say there are n pages) is n-1. So if you want to insert the first two pages nStart needs to be 0 and nEnd needs to be 1.
I think that this is what causing this error.

(Edit: also nPage is 0-based)

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

carisima
Registered: Apr 25 2008
Posts: 21
Yes, that was it. It's working how I want it to.

Thank you very much.