damixa
asked on
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sometimes timing is everything in life. :)
ASKER
This worked great. Is there a way to delete all files except %fn% ?
thanks,
thanks,
I won't speak to x66_x72_x65_x65's script. Mine deletes just file1.pdf and file2.pdf. Regards, Joe
ASKER
So I had to use
thanks again
@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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
genius Joe,
thanks
thanks
ASKER
Thanks both of you guys
You're welcome. And thanks to you for the kind words - much appreciated! Regards, Joe
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.
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.
> 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
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
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.
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?
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
123456789-23-page12.pdf
123456789-23-page3.pdf
123334455-4-page12.pdf
123334455-4-page3.pdf
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.
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:
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