• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16644
  • Last Modified:

batch script to merge pdf files

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
damixa
Asked:
damixa
  • 8
  • 4
  • 3
  • +1
2 Solutions
 
Giovanni HewardCommented:
You can do this using PDFtk.

pdftk *.pdf cat output combined.pdf

Open in new window


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
      )
   )
)

Open in new window

0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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

Open in new window


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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
Sometimes timing is everything in life. :)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

thanks,
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
I won't speak to x66_x72_x65_x65's script. Mine deletes just file1.pdf and file2.pdf. Regards, Joe
0
 
damixaAuthor Commented:
So I had to use


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

Open in new window

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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
 
damixaAuthor Commented:
genius Joe,
thanks
0
 
damixaAuthor Commented:
Thanks both of you guys
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
You're welcome. And thanks to you for the kind words - much appreciated! Regards, Joe
0
 
Sazan AbdCommented:
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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> 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

and, extending your example:

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
 
Sazan AbdCommented:
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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
 
Sazan AbdCommented:
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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now