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

merging pdfs with javascript

stikxs
Registered: Feb 4 2011
Posts: 2
Answered

So I'm trying to set up a script that will merge a group of pdfs for me, but I'm having a bit of trouble with it. What I have is a folder that has a lot of pdfs in it, but they are grouped in groups of 4 based on an id number that is in the filename of each, with a different letter+underscore prefix to distinguish the 4 pdfs for each id.
 
This is my first attempt at javascript and acrobat w/ batch processing so I'm not familiar with it, but my plan was to have a batch process set up that would go through each pdf in the folder and run this script. The script would only do anything on 1 of the 4 pdfs in each group (easiest way I could find quickly to get the filename to work with, without running multiple times). Basically it would strip off the prefix part of the filename and the extension leaving the id number which would become the filename for the merged pdf. For some reason though the console is saying my local variables in the script are undefined and fails on the try block.
 
I've also noticed that the debugger doesn't seem very easy to use, and when I check "debug on start" it will come up, but the code shown is not the current code that is in my batch process. It does seem to update, but its always quite a few edits behind in the debugger. This is in acrobat 9 on osx 10.6.4. Thanks for any thoughts/help.
 
var replacement = /\.pdf$/i;
var filename = this.documentFileName.replace(replacement,"");
if (this.documentFileName.substr(0,1)=="c_" || this.documentFileName.substr(0,1)=="C_"){
var newdoc=app.newDoc();
console.println("RE "+replacement);

console.println("FN "+filename);
filename=filename.substr(2);

try
{
newdoc.insertPages({nPage: -1,cPath:("r_"+filename+".pdf")});
newdoc.insertPages({nPage: -1,cPath:("i_"+filename+".pdf")});
newdoc.insertPages({nPage: -1,cPath:("l_"+filename+".pdf")});
newdoc.insertPages({nPage: -1,cPath:("c_"+filename+".pdf")});
newdoc.saveAs({cPath:(filename+"xx.pdf")});
newdoc.closeDoc(true);
}
catch (e)
{
console.println("Aborted: " + e);
}
}

My Product Information:
Acrobat Pro 9.0, Macintosh
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
Don't use the debugger, turn it off in the preferences. It just slows things down and gets in the way.

The script looks good. Are you saying that both "replacement" and "filename" are being printed to the console as "undefined"?

I'd try a couple of things.

1. Open one of your files in Acrobat and run the code, line by line, from the console window. See what happens.

2. Remove the try block from the script and add in more console.println() statements to get a handle on what's happening with the script parameters. You could try replacing "this" with "event.target"

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

try67
Expert
Registered: Oct 30 2008
Posts: 2398
cPath must be a the full pathname to the file, not just the name of it.
This also goes for the saveAs command. You must specify the complete path for it to work.
Also, I would drop the parentheses around the cPath value. They are not necessary.

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

stikxs
Registered: Feb 4 2011
Posts: 2
Thanks guys. I removed the try and that helped find things. Turned out my substr should have been (0,2) instead, and I put in the full path to get things right. I had to play with the : path style and the / path style to get it happy also.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Here's an article you might want to look at for manipulating paths. There are also some file path tools built-into Acrobat JavaScript:

http://acrobatusers.com/tutorials/2006/string_splitting

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