Simple Merging Of PDF Documents with iTextSharp 5.4.5.0
As we were working on our first SQL Saturday in Slovenia, we came to a point when we had to print out the so-called SpeedPASS's for attendees. This SpeedPASS file is a PDF and contains thier raffle, lunch and admission tickets.
The problem is we have to download one PDF per attendee and print that out. And printing more than 10 docs at once is a pain. So I decided to make a little console app that would merge multiple PDF files into a single file that would be much easier to print. I used an open source PDF manipulation library called iTextSharp version 5.4.5.0
This is a console program I used. It’s brilliantly named MergeSpeedPASS. It only has two methods and is really short. Don't let the name fool you It can be used to merge any PDF files.
The first parameter is the name of the target PDF file that will be created.
The second parameter is the directory containing PDF files to be merged into a single file.
using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.IO;namespace MergeSpeedPASS { class Program { static void Main(string[] args) { if (args.Length == 0 || args[0] == "-h" || args[0] == "/h") { Console.WriteLine("Welcome to MergeSpeedPASS. Created by Mladen Prajdic. Uses iTextSharp 5.4.5.0."); Console.WriteLine("Tool to create a single SpeedPASS PDF from all downloaded generated PDFs."); Console.WriteLine(""); Console.WriteLine("Example: MergeSpeedPASS.exe targetFileName sourceDir"); Console.WriteLine(" targetFileName = name of the new merged PDF file. Must include .pdf extension."); Console.WriteLine(" sourceDir = path to the dir containing downloaded attendee SpeedPASS PDFs"); Console.WriteLine(""); Console.WriteLine(@"Example: MergeSpeedPASS.exe MergedSpeedPASS.pdf d:\Downloads\SQLSaturdaySpeedPASSFiles"); } else if (args.Length == 2) CreateMergedPDF(args[0], args[1]);
Console.WriteLine(<span class="str">""</span>); Console.WriteLine(<span class="str">"Press any key to exit..."</span>); Console.Read(); } <span class="kwrd">static</span> <span class="kwrd">void</span> CreateMergedPDF(<span class="kwrd">string</span> targetPDF, <span class="kwrd">string</span> sourceDir) { <span class="kwrd">using</span> (FileStream stream = <span class="kwrd">new</span> FileStream(targetPDF, FileMode.Create)) { Document pdfDoc = <span class="kwrd">new</span> Document(PageSize.A4); PdfCopy pdf = <span class="kwrd">new</span> PdfCopy(pdfDoc, stream); pdfDoc.Open(); var files = Directory.GetFiles(sourceDir); Console.WriteLine(<span class="str">"Merging files count: "</span> + files.Length); <span class="kwrd">int</span> i = 1; <span class="kwrd">foreach</span> (<span class="kwrd">string</span> file <span class="kwrd">in</span> files) { Console.WriteLine(i + <span class="str">". Adding: "</span> + file); pdf.AddDocument(<span class="kwrd">new</span> PdfReader(file)); i++; } <span class="kwrd">if</span> (pdfDoc != <span class="kwrd">null</span>) pdfDoc.Close(); Console.WriteLine(<span class="str">"SpeedPASS PDF merge complete."</span>); } } }
}
Hope it helps you and have fun.
Legacy Comments
anon
2014-01-11 |
re: Simple Merging Of PDF Documents with iTextSharp 5.4.5.0 It's better to use PDFSmartCopy for this task. Because it will remove duplicate streams from the final file. api.itextpdf.com/.../PdfSmartCopy.html |