PDFsharp is the Open Source library that creates PDF documents from any .NET language. In this article, we will look into how to use the PDFsharp library for splitting a single PDF file into multiple PDF files.
Introduction
PDFsharp is the Open Source library that creates PDF documents from any .NET language. In this article, we will look into how to use the PDFsharp library for splitting a single PDF file into multiple PDF files.
Step by Step Explanation
Fire up a console application and from Nuget Package Console issue
Install-Package PdfSharp -Version 1.32.3057
Now let's say we have a pdf file as
We need to split it into individual parts by pages. The below program will do so.
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
// Output Folder
static string outputFolder = @"D:\PDFSplit\Example\outputFolder";
static void Main(string[] args)
{
// Input Folder
var inputFolder = @"D:\PDFSplit\Example\inputFolder";
// Input File name
var inputPDFFileName = "sample.pdf";
// Input file path
string inputPDFFilePath = Path.Combine(inputFolder, inputPDFFileName);
// Open the input file in Import Mode
PdfDocument inputPDFFile = PdfReader.Open(inputPDFFilePath, PdfDocumentOpenMode.Import);
//Get the total pages in the PDF
var totalPagesInInputPDFFile = inputPDFFile.PageCount;
while(totalPagesInInputPDFFile !=0)
{
//Create an instance of the PDF document in memory
PdfDocument outputPDFDocument = new PdfDocument();
// Add a specific page to the PdfDocument instance
outputPDFDocument.AddPage(inputPDFFile.Pages[totalPagesInInputPDFFile-1]);
//save the PDF document
SaveOutputPDF(outputPDFDocument, totalPagesInInputPDFFile);
totalPagesInInputPDFFile--;
}
}
private static void SaveOutputPDF(PdfDocument outputPDFDocument,int pageNo)
{
// Output file path
string outputPDFFilePath = Path.Combine(outputFolder, pageNo.ToString() + ".pdf");
//Save the document
outputPDFDocument.Save(outputPDFFilePath);
}
}
}
At first we are opening the input file in the Import Mode.A PDF document opened in this mode cannot be modified.
PdfDocument inputPDFFile = PdfReader.Open(inputPDFFilePath, PdfDocumentOpenMode.Import);
Next we are looping through the entire PDF documents pages by pages and for every iteration, creating a new instance of the PdfDocument in memory and inserting the specific page to the PdfDocument class via the AddPage method.
//Create an instance of the PDF document in memory
PdfDocument outputPDFDocument = new PdfDocument();
// Add a specific page to the PdfDocument instance
outputPDFDocument.AddPage(inputPDFFile.Pages[totalPagesInInputPDFFile-1]);
Finally we are saving the splitted document.
private static void SaveOutputPDF(PdfDocument outputPDFDocument,int pageNo)
{
// Output file path
string outputPDFFilePath = Path.Combine(outputFolder, pageNo.ToString() + ".pdf");
//Save the document
outputPDFDocument.Save(outputPDFFilePath);
}
The resultant output will be
References
PDFSharp
Conclusion
This article taught us how to split a single PDF file into multiple pdf files using PDFSharp. Hope this will be helpful. Thanks for reading. Zipped file is attached herewith.