Solved

# batch script to merge pdf files

Posted on 2013-11-04
9,385 Views
I have a folder with 2 files, file1.pdf and file2.pdf

is it possible to have a batch script, that takes all the files in the folder, prompts for a file name and merges both files into the new file with the new name? (the one we entered in the prompt?

also delete the old files

anybody has done anything like this?

thanks,
Vinnie
0
Question by:damixa
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 4
• 3
• +1

LVL 15

Assisted Solution

Giovanni Heward earned 250 total points
ID: 39622135
You can do this using PDFtk.

pdftk *.pdf cat output combined.pdf


This script will get the job done, with some safety checks built in.

@echo off
setlocal enabledelayedexpansion
set pdftk=c:\path\to\pdftk.exe
set /p t=PDF output name:
if [!t!]==[] set t=combined.pdf
if exist temp ( rd /s/q temp ) else ( md temp )
if exist !pdftk! (
if exist temp (
!pdftk! *.pdf cat output temp\!t!
if exist temp\!t! (
del *.pdf /f/q
move temp\!t! .
rd temp /s/q
)
)
)

0

LVL 54

Expert Comment

ID: 39622140
Hi Vinnie,
I recommend using the the PDF Toolkit (PDFtk), an excellent (free!) product that has numerous features to manipulate PDFs. It comes in both command line and GUI versions. The command line version is called PDFtk Server and may be downloaded here:
http://www.pdflabs.com/tools/pdftk-server/

Don't be misled by "Server" in the name. I don't know why they called it that, but it's just an EXE with a DLL that runs on XP, Vista, W7, and W8 (it does not have to run on a "server" OS...it also runs on Mac, but I've never used it on that).

Here's the one-line solution using PDFtk:

pdftk file1.pdf file2.pdf cat output outputfile.pdf

The rest of the batch file would be getting the output file name and deleting the source files. You can make it as fancy or as simple as you want. Here's the simple approach:

@echo off
set /p fn=Enter the output filename:
pdftk file1.pdf file2.pdf cat output %fn%.pdf
del file1.pdf
del file2.pdf


You'll need to handle paths and the location of PDFtk, but other than that, the above works. I just tested it with the input files and the two required PDFtk files (pdftk.exe and libiconv2.dll) residing in the same folder and it worked perfectly. Regards, Joe
0

LVL 54

Expert Comment

ID: 39622141
Sometimes timing is everything in life. :)
0

Author Comment

ID: 39622226
This worked great. Is there a way to delete all files except %fn% ?

thanks,
0

LVL 54

Expert Comment

ID: 39622240
I won't speak to x66_x72_x65_x65's script. Mine deletes just file1.pdf and file2.pdf. Regards, Joe
0

Author Comment

ID: 39622274

@echo off
set /p fn=Enter the output filename:
pdftk *.pdf cat output %fn%.pdf

because I dont know the names and how many files there would be. Just an extra step to delete * except for %fn%.pdf.

thanks again
0

LVL 54

Accepted Solution

Joe Winograd, EE MVE 2015&2016 earned 250 total points
ID: 39622359
Ah, I misunderstood you...I thought it was just those two files...but you figured it out with *.pdf – perfect!

Btw, I should have mentioned this before, but I'd be tempted not to delete the source PDFs in the same batch file, in case the merge (cat) does not work successfully (although I've never seen PDFtk fail on it). You could build a separate batch file to delete the source files, but run it only after you reviewed the output to make sure that the operation worked.

That said, one simple trick is to rename the output PDF file to a different file type, do the delete of *.pdf, then rename the output file back to a PDF. Something like this:

@echo off
set /p fn=Enter the output filename:
pdftk *.pdf cat output %fn%.pdf
ren %fn%.pdf %fn%.xxx
del *.pdf
ren %fn%.xxx %fn%.pdf

Simple, but effective. Regards, Joe
0

Author Comment

ID: 39622457
genius Joe,
thanks
0

Author Closing Comment

ID: 39622460
Thanks both of you guys
0

LVL 54

Expert Comment

ID: 39622476
You're welcome. And thanks to you for the kind words - much appreciated! Regards, Joe
0

Expert Comment

ID: 41828847
Hi Joe, I came across this site when I was looking for batch pdf file merge. I have a folder with man pdf files, and want to merge the files with the same id in to one file, for example:

111_1.pdf
111_2.pdf
222_1.pdf
222_2.pdf
I want to merge 111_1.pdf and 111_2.pdf into one file and merge 222_1.pdf and 222_2.pdf into another.

How can I do this.
0

LVL 54

Expert Comment

ID: 41828900
> How can I do this.

Write a program that looks for a match on the first three characters of the file names — or on all the characters in the file name before an underscore (needed if the file names can be a varying number of characters before the underscore). Build up a list of file names with the matching first characters and then feed the list to PDFtk with the cat command, naming the output file with the characters before the underscore. In other words, in your example above, it would combine/merge:

111_1.pdf
111_2.pdf

into:

111.pdf

and:

222_1.pdf
222_2.pdf

into:

222.pdf

33333_1.pdf
33333_2.pdf
33333_3.pdf

into:

33333.pdf

I've written several programs for clients to do this, but each one is slightly different based on each customer's unique requirements. One of the simpler programs is called Combine PDFs Based On Filenames, but it requires a fixed number of leading characters. Other programs allow multiple segments in the file names (i.e., more than just the two as in your case) and varying separator characters (underscore, hyphen, period, etc.). In any case, if you are a programmer, you shouldn't have much trouble writing a program to figure out which PDFs to combine/merge — the tough stuff is combining/merging them, but that's where PDFtk comes in — making it easy! Regards, Joe
1

Expert Comment

ID: 41829010
Thanks Joe for your prompt reply, I have some programming back ground, I do understand your concept, but I would appreciate if you can share any scripts that do this task.
0

LVL 54

Expert Comment

ID: 41830003
Do all of your files have just three characters before the underscore? If not, do they all have the same number characters before the underscore?
1

Expert Comment

ID: 41830918
Let me explain the exact scenario, I am generating customer statement using SSRS, each statement has 3 or more page, the first two pages needs to be in portrait orientation, and the rest in landscape. SSRS doesn't have the ability to set orientation per page, it is set on the report level only. To get around this issue, I am generating a report with the first two pages in portrait layout, and another report contains the rest of the pages in landscape layout, then export them in PDF format and finally merge both report for each customer into one pdf file. All the files will start with 9 character (which is the account number and will be followed by '-' and statement number and page number, as below:

123456789-23-page12.pdf
123456789-23-page3.pdf

123334455-4-page12.pdf
123334455-4-page3.pdf
0

LVL 54

Expert Comment

ID: 41830965
Thanks for the explanation — very helpful! I suggest we continue this discussion via the message system, since it has veered off-course from the original, already-closed question. I'll send you a message in a moment.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe INTRODUCTION This article presents a solution to a question (http://www.experts-exchange.com/Programming/Installation/Q_28396542.html)…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
###### Suggested Courses
Course of the Month3 days, 7 hours left to enroll