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

Merging multiple PDF files into One PDF through Automation

md16
Registered: May 16 2007
Posts: 8

Hi All,

Is there a way to merge multiple PDF files into one through some kind of automation and also a naming convention for those files? Automation like java script or command line function?

So for e.g. if I have 200 PDF files, with some naming convention of 10 each, can I create just 20 files with 10 files combining into 1 (with some naming convention link)?

Would really appreciate your help in this matter.

md16

kr4z33
Registered: Apr 25 2007
Posts: 6
I have done this using a VB script. my files and stuff are at work, I'll tell you how I did is as soon as I get to my work computer. my script takes a list of paths to pdf files and then will combine them and optionally add bookmarks (from another list) where each file begins. It can save a bunch of time :)
kr4z33
Registered: Apr 25 2007
Posts: 6
i can't claimn original authorship...i modified a script similar to the one found here:
http://groups.google.com/group/adobe.acrobat.windows/browse_thread/thread/524e7ce34e6d22ab/cb3d2cf85863a7c%23cb3d2cf85863a7c
or here:
http://groups.google.com/group/adobe.acrobat.windows/browse_thread/thread/6566e14a8a9ece4f?hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=1
note that some of the variables seem to be in some european language. i don't know what they mean so I just left them as-is and worked around it.

I made mine so that the bookmarks are taken from a seperate file than the filenames, heres the code:

Info = "Insert PDF-Files in Acrobat based on File-list."&VbCr&_"Acrobat should be opened"OK = MsgBox(Info, vbQuestion+vbYesNo)if OK = vbNo then WScript.quit '*********Settings in File**************************Datei = "files.txt"  '#Txt Filename for ReadAsk = 0    '#0=don't ask for Insert;1=askBookmark = 2 '#0=Insert No bookmarks;1=Insert base on Filename;2=Insert based on file BkMkListBkMkList = "bkmks.txt" '#Txt Filename for Reading Bookmarks'****************************************************'*******Some hints to special keys in "sendkeys": ^=Ctl, %=Alt, +=Shift and ~=Enter. So with "%fo" you get Alt=Menu -> File -> Open. '*******In the script I used only international Adobe shortcuts to avoid regional versions problem. set WshShell = CreateObject ("Wscript.Shell")set fs = CreateObject("Scripting.FileSystemObject")WshShell.AppActivate "Adobe Acrobat" FLPOS = 1

 if bookmark=2 then

if fs.FileExists(BkMkList) then

set BookMarkList = fs.OpenTextFile(BkMkList)

else

MsgBox("cannot find bookmarks list")end if

end if

 if fs.FileExists(datei) then

set dateiinhalt = fs.OpenTextFile(datei)

do until dateiinhalt.atEndOfStream

inhalt = dateiinhalt.ReadLine

if Ask=1 then if MsgBox(Page&": "&inhalt, vbOKCancel + vbInformation) = vbCancel then exit do'
#File->Open first Fileif FLPOS = 1 thenWshShell.run("Acrobat.exe """& Inhalt &"""")Set BASFL = CreateObject("AcroExch.PDDoc")BASFL.OpenAVDoc(mid(INHALT,InstrRev(INHALT,"\")+1))PageBAS = BASFL.GetNumPages

WScript.Sleep 2000

else

WScript.Sleep 500

WshShell.SendKeys "
^+I" '=>German Version (Dok. insert)WScript.Sleep 600

WshShell.SendKeys inhalt

WScript.Sleep 800

WshShell.SendKeys "
~"WScript.Sleep 300

WshShell.SendKeys "
%l~"WScript.Sleep 2000

end if

FLPOS = FLPOS+1

WshShell.SendKeys "
^+{PGDN}"WScript.Sleep 500

if bookmark = 1 then

BMNameIE = mid(Inhalt, InstrRev(Inhalt, "
\")+1)BMNameEE = left(BMNameIE, InstrRev(BMNameIE, ".")-1)WScript.Sleep 500

WshShell.SendKeys "
^b"WScript.Sleep 700

WshShell.SendKeys BmNameEE

WScript.Sleep 1000

WshShell.SendKeys "
~"WScript.Sleep 500

end if

if fs.FileExists(BkMkList) then

if bookmark = 2 then

BMNameEE = BookMarkList.ReadLine

WScript.Sleep 500

WshShell.SendKeys "
^b"WScript.Sleep 700

WshShell.SendKeys BmNameEE

WScript.Sleep 1000

WshShell.SendKeys "
~"WScript.Sleep 500

end if

end if

loop

if fs.FileExists(BkMkList) then

if bookmark = 2 then

BookMarkList.close

end if

end if

dateiinhalt.close

MsgBox "
DONE!" &vbCr &"Don't forget to save the File (save as)"else

MsgBox "Ups! " & datei & " doesn'
t exist? " & "Try new!", vbExclamationend if

-save this code as AcInsFL.vbs

-make your list of file paths in "files.txt", using something like printfolder found here:
http://www.no-nonsense-software.com/freeware/
can be useful..

-make a list of bookmarks if you want to in "bkmks.txt", note the bookmarking options in the above code (near the top).

-open acrobat, run AcInsFL.vbs

it had been a while since i used it, if any of the instructions dont make sense or don't work let me know and I'll try to figure it out again...

ps. this is my first acrobat script adventure, if anybody can suggest improvement on it I would be grateful :) good luck !
fchivu
Registered: Jul 10 2007
Posts: 4
You can build your own PDF merge utility using the PDF Merge and split libraries for .NET from from http://www.dotnet-reporting.com or http://www.winnovative-software.com .
You can use it to merge PDF files, html files, text files and images,
set the page orientation, compression level and page size.

All this can be accomplished with only a few lines of code:

PdfDocumentOptions pdfDocumentOptions = new PdfDocumentOptions();
pdfDocumentOptions.PdfCompressionLevel = PDFCompressionLevel.Normal;
pdfDocumentOptions.PdfPageSize = PdfPageSize.A4; pdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
PDFMerge pdfMerge = new PDFMerge(pdfDocumentOptions);
pdfMerge.AppendPDFFile(pdfFilePath);
pdfMerge.AppendImageFile(imageFilePath);
pdfMerge.AppendTextFile(textFilePath);
pdfMerge.AppendEmptyPage();
pdfMerge.AppendHTMLFile(htmlFilePath);
pdfMerge.SaveMergedPDFToFile(outFile);

You can find the full code sample on the PDF Merge Code Sample page

Regards,
Florin
Gulliver
Registered: Feb 11 2008
Posts: 1
Hi,
Here's my solution to your problem, building on the work of others. I've tested this under several scenarios and added some error trapping. Please let me know if anyone finds this useful. Thanks.
------WSH code follows------
if MsgBox("Insert and optionally bookmark PDFFiles in Acrobat based on a list of filenames."&VbCr&"Proceed?", vbQuestion+vbYesNo) = vbNo then WScript.quit'*********Settings in File**************************************************************
FileOfFileNames = "C:\files.txt" 'Text Filename for Read only
OutputFileName = "C:\adobe2.pdf" 'PDF filename for destructive Write
Ask = 0 '#0=don't ask for Insert;1=ask
Bookmark = 1 '#0=Insert No bookmarks;1=Insert base on Filename
'***************************************************************************************


Set app = CreateObject("AcroExch.App")
Set oPDDocOut = CreateObject("AcroExch.PDDoc")
Set oPDDocIn = CreateObject("AcroExch.PDDoc")

set WshShell = CreateObject ("Wscript.Shell") 'standard fare for scripting
set fs = CreateObject("Scripting.FileSystemObject") 'standard fare for scripting when reading from a file


BookMarkcount = 0

if not fs.FileExists(FileOfFileNames) then
MsgBox "Unrecoverable error: Script could not open the File of FileNames: " & FileOfFileNames, vbExclamation
wscript.quit
End If

set DataStream = fs.OpenTextFile(FileOfFileNames)

do until DataStream.atEndOfStream

FileNameToAdd = DataStream.ReadLine
if Ask=1 then if MsgBox("Would you like to insert this file: "&FileNameToAdd, vbOKCancel + vbInformation) = vbCancel then exit doIf BookMarkCount = 0 then
If not oPDDocOut.Open(FileNameToAdd) Then
MsgBox "Unrecoverable error: Script could not open "&FileNameToAdd, vbExclamation
wscript.quit
end if
if not oPDDocOut.Save (1,OutputFileName) then
MsgBox "Unrecoverable error: Script could not save output file at start.", vbExclamation
wscript.quit
end If
Set oJSO = oPDDocOut.GetJSObject
Set oBookMarkRoot = oJSO.BookMarkRoot
LastPageOut = 1
else
If not oPDDocIn.Open(FileNameToAdd) Then
MsgBox "Unrecoverable error: Script could not open "&FileNameToAdd, vbExclamation
wscript.quit
end if

LastPageOut = oPDDocOut.GetnumPages
LastPageIn = oPDDocIn.GetnumPages

If not oPDDocOut.insertPages(LastPageOut-1, oPDDocIn , 0, LastPageIn, False) then
MsgBox "Unrecoverable error: Script could not insert pages from the input file: "&FileNameToAdd&".", vbExclamation
wscript.quit
End if

if not oPDDocIn.Close then
MsgBox "Unrecoverable error: Script could not close the input file: "&FileNameToAdd&".", vbExclamation
wscript.quit
End if

end if

'now add the bookmark

TempString = mid(FileNameToAdd, InstrRev(FileNameToAdd, "\")+1)
BookMarkToAdd = left(TempString, InstrRev(TempString, ".")-1)

If len(BookMarkToAdd) = 0 then
MsgBox "Unrecoverable error: Script could not calculate a bookmark for the input file: "&FileNameToAdd&".", vbExclamation
wscript.quit
End if

if BookMark then
oJSO.BookMarkRoot.createChild BookMarkToAdd, "pageNum="&LastPageOut&";", BookMarkCount
BookMarkCount = BookMarkCount + 1
End if
loop

DataStream.close

if not oPDDocOut.Save (1,OutputFileName) then
MsgBox "Unrecoverable error: Could not save final file.", vbExclamation
wscript.quit
end If


MsgBox "Success!" &vbCr &"Saved the File"
-------Code ends with above line------------
yipchunyu
Registered: Apr 30 2009
Posts: 8
I use the above codes. It works well to merge PDFs and add the bookmarks.
However, when the book marks are in Chinese characters. It display correctly in Acrobat 5 but not other versions' acrobat.
More, if I open the file with acrobat 5 (which show the bookmarks correctly), when I copy the bookmark's text (via the rename command), it display strange characters when I paste it in other app. So, I guess the text showing in Acrobat 5 is incorrect. I searched the web but no info can be find. Any advice should help me a lot. thx in advance.