How To Split A PDF Using PdfSharp

I recently completed a project which broker a PDF file into multiple files from which I converted to a MS Excel file and ultimately processed the data into a database.

This segment is dealing with the portion for splitting a multi-page Adobe PDF file into multiple pages. I mostly do this step, because we will be storing the individual page into a document management system along with the data that we strip from it. This page will be used later by our data entry clerks.

When breaking down to individual pages, we need to ensure that we keep the integrity of the original page. This ensures we can still convert to an Excel file to get the data from it.

There are many open-source and free PDF SDK kits that you can try. I had best luck doing most any PDF work using PdfSharp ( http://pdfsharp.com/PDFsharp/ ). Here is a modified code segment of how you can use PdfSharp to split.

Int32 iCount = 0;

PdfDocument inputDocument = PdfReader.Open(filename, PdfDocumentOpenMode.Import);
String directory = @"C:temp";
string name = Path.GetFileNameWithoutExtension(filename);
for (int idx = 0; idx < inputDocument.PageCount; idx++)
{
iCount++;
toolStripStatusLabel2.Text = " - Processing File# " + iCount + " of " + inputDocument.PageCount;
Application.DoEvents();

// Create new document
PdfDocument outputDocument = new PdfDocument();
outputDocument.Version = inputDocument.Version;
outputDocument.Info.Title = String.Format("Page {0} of {1}", idx + 1, inputDocument.Info.Title);
outputDocument.Info.Creator = inputDocument.Info.Creator;

// Add the page and save it
outputDocument.AddPage(inputDocument.Pages[idx]);
outputDocument.Save(Path.Combine(directory , String.Format("{0} - Page {1}.pdf", name, idx + 1)));
}
Posted in C#, PDF, Split

Leave a Reply

Your email address will not be published. Required fields are marked *

*