A few months ago, I wrote a small article about extracting pages from a PDF document to create a new PDF document. This article will use the same library, iTextSharp, to merge pages from one PDF document to create a second PDF document.
For this utility, imagine having a PDF document with pages that are 8 1/2″ x 11″ and you want to combine 2 pages into one larger page. The resulting output document would be 17″ x 11″ and show two pages from the input document on one page on the output document.
I’ll just brush over the basic program setup.
- I used a C# console application as my starting point
- There are 2 command line arguments (input file and output file)
- Validate that the input file exists
- Validate that the input file is a PDF document
- Add two using directives for iTextSharp.text and iTextSharp.text.pdf
Here’s the primary method of the application. The code comments should provide enough information to walk you through the steps.
private static void MergePages(string inputFile, string outputFile) { // get input document PdfReader inputPdfReader = new PdfReader(inputFile); // load the input document Document inputPdfDoc = new Document(inputPdfReader.GetPageSizeWithRotation(1)); // create the filestream using (FileStream fs = new FileStream(outputFile, FileMode.Create)) { // create the output writer PdfWriter outputWriter = PdfWriter.GetInstance(inputPdfDoc, fs); inputPdfDoc.Open(); PdfContentByte pdfCb = outputWriter.DirectContent; // loop through each page in input pdf reader for (int i = 1; i <= inputPdfReader.NumberOfPages; i++) { float leftOffset = 1f; // if even page (0,2,4,...) if (i % 2 == 1) { Rectangle inputPageSize = inputPdfReader.GetPageSizeWithRotation(i); Rectangle outputPageSize = new Rectangle(0, 0, inputPageSize.Width * 2, inputPageSize.Height); //add new page that is x2 width inputPdfDoc.SetPageSize(outputPageSize); inputPdfDoc.NewPage(); // draw vertical line pdfCb.MoveTo(inputPageSize.Width, 0); pdfCb.LineTo(inputPageSize.Width, inputPageSize.Height); pdfCb.Stroke(); } else { leftOffset = inputPdfReader.GetPageSizeWithRotation(i).Width; } // get page[i] from input pdf PdfImportedPage inputPage = outputWriter.GetImportedPage(inputPdfReader, i); // add page[i] to new pdf on current page with offset pdfCb.AddTemplate(inputPage, leftOffset, 0); } inputPdfDoc.Close(); } }
Resources
- iTextSharp home page (SourceForge)