Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
PDF (Portable Document Format) files are widely used for sharing and presenting documents, and there are times when you may need to split a PDF into multiple files. Whether you want to extract specific pages, divide a large document into smaller sections, or create individual files for each chapter, splitting PDFs can be a valuable task in various scenarios.
In this article, we'll learn how to split PDFs using C#. C# is a versatile and powerful language, and there are several libraries available that make it relatively straightforward to manipulate PDFs. We will explore the following two Libraries for Splitting PDF in C#.
IronPDF is a powerful C# library designed for working with PDF files. It provides features for creating, modifying, and extracting content from PDF documents. Developers can generate PDFs from scratch, edit existing PDFs, and merge or split them. Additionally, IronPDF excels at converting HTML content to PDF format, making it useful for generating reports or documentation. With support for digital signatures, security features, and high-quality output, IronPDF simplifies PDF-related tasks in .NET applications.
iTextSharp (iText 7) is a widely used library for working with PDF files in the .NET framework. It provides powerful features for creating, modifying, and extracting content from PDF documents programmatically. Developers can use iTextSharp to add text, images, tables, and other graphical elements to PDFs. Additionally, it supports document assembly, digital signatures, and compliance with archival and accessibility standards. Originally a Java library, iTextSharp was ported to .NET and has an active community of developers and users.
To install the IronPDF NuGet package using Package Manager Console in Visual Studio, follow these steps:
Use the following command to install the IronPDF NuGet package:
Install-Package IronPdf
This will download and install the IronPDF package along with its dependencies into your project. Once the installation is complete, you can start using IronPDF in your C# project for PDF-related tasks.
Alternatively, you can install IronPDF using the NuGet Package Manager in Visual Studio, or add the package directly to your project file. Another option is downloading the package from the official website and manually adding it to your project. Each method provides a straightforward way to integrate IronPDF into your C# project for PDF-related functionalities.
To install iTextSharp using Package Manager Console in Visual Studio, you can follow these steps:
Use the following command to install the iTextSharp NuGet package:
Install-Package itext7
This will download and install the iTextSharp package along with its dependencies into your project. Once the installation is complete, you can start using iTextSharp in your C# project for working with PDFs.
We can split a PDF file into multiple PDF files Using IronPDF. It provides a simple way to achieve this. The following code will take the source PDF file as input and split it into multiple PDF files.
static void Main(string [] args)
{
string file = "input.pdf"'
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
int firstPage = 1;
int lastPage = 2;
int totalPageInOneFile = sourceFile.PageCount / NumberOfSplitFiles;
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
// Copy multiple pages into a new document
PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage);
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
newSplitPDF.SaveAs(name);
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
static void Main(string [] args)
{
string file = "input.pdf"'
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
int firstPage = 1;
int lastPage = 2;
int totalPageInOneFile = sourceFile.PageCount / NumberOfSplitFiles;
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
// Copy multiple pages into a new document
PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage);
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
newSplitPDF.SaveAs(name);
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
Shared Sub Main(ByVal args() As String)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
' string file = "input.pdf"' SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles); } static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles) { PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath); int firstPage = 1; int lastPage = 2; int totalPageInOneFile = sourceFile.PageCount / NumberOfSplitFiles; for(int i = 1; i <= NumberOfSplitFiles; i++) { PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage); string name = string.Format("{0}\SplitPDF_IronPDF_{1}.pdf", outputFolder, i); newSplitPDF.SaveAs(name); firstPage = lastPage + 1; lastPage += totalPageInOneFile; } }
The purpose of this code is to split a given PDF file into multiple smaller PDF files using the IronPDF library. The SplitPdfUsingIronPDF method is defined to achieve this functionality.
Inside the SplitPdfUsingIronPDF method:
You should replace "input.pdf" with the actual path to your input PDF file. Ensure that the IronPDF library is properly installed and referenced in your project.
The output files are created as:
Now, we will use iTextSharp to split our PDF document into multiple PDF files. The following code will take the source file as input and split that PDF document into multiple smaller files.
static void Main(string [] args)
{
string inputPath = "input.pdf";
// Output PDF files path (prefix for the generated files)
string outputPath = "output_split";
int NumberOfSplitFiles = 3;
// Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles);
}
static void SplitPdfUsingiTextSharp(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
using (PdfReader Reader = new PdfReader(inputPdfPath))
{
using (PdfDocument doc = new PdfDocument(Reader))
{
int totalPageInOneFile = doc.GetNumberOfPages() / NumberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile; // int pagenumber
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) // create output file
{
//pdfDocument.get
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
static void Main(string [] args)
{
string inputPath = "input.pdf";
// Output PDF files path (prefix for the generated files)
string outputPath = "output_split";
int NumberOfSplitFiles = 3;
// Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles);
}
static void SplitPdfUsingiTextSharp(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
using (PdfReader Reader = new PdfReader(inputPdfPath))
{
using (PdfDocument doc = new PdfDocument(Reader))
{
int totalPageInOneFile = doc.GetNumberOfPages() / NumberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile; // int pagenumber
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) // create output file
{
//pdfDocument.get
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
Shared Sub Main(ByVal args() As String)
Dim inputPath As String = "input.pdf"
' Output PDF files path (prefix for the generated files)
Dim outputPath As String = "output_split"
Dim NumberOfSplitFiles As Integer = 3
' Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles)
End Sub
Shared Sub SplitPdfUsingiTextSharp(ByVal inputPdfPath As String, ByVal outputFolder As String, ByVal NumberOfSplitFiles As Integer)
Using Reader As New PdfReader(inputPdfPath)
Using doc As New PdfDocument(Reader)
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim totalPageInOneFile As Integer = doc.GetNumberOfPages() / NumberOfSplitFiles
Dim firstPage As Integer = 1
Dim lastPage As Integer = totalPageInOneFile ' int pagenumber
For i As Integer = 1 To NumberOfSplitFiles
Dim filename As String = $"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"
Using pdfDocument As New PdfDocument(New PdfWriter(filename)) ' create output file
'pdfDocument.get
doc.CopyPagesTo(firstPage, lastPage, pdfDocument)
End Using
firstPage = lastPage + 1
lastPage += totalPageInOneFile
Next i
End Using
End Using
End Sub
This above code demonstrates how to split a large PDF file into smaller chunks using iTextSharp. Each smaller PDF document will contain a portion of the original document.
Inside the SplitPdfUsingiTextSharp method:
A loop iterates from 1 to NumberOfSplitFiles:
Ensure that the iTextSharp library is properly installed and referenced in your project. Replace "input.pdf" with the actual path to your input PDF file.
To compare the two split methods using iTextSharp and IronPDF, let's evaluate them based on several factors:
In conclusion, both iTextSharp and IronPDF offer robust solutions for splitting PDF files in C#, each with its own set of advantages. IronPDF stands out for its simplicity, readability, and potentially better performance due to a more straightforward approach.
Developers seeking a balance between versatility and ease of use may find IronPDF to be a compelling choice. Additionally, IronPDF offers a free trial. Ultimately, the selection between iTextSharp and IronPDF depends on individual project requirements and the preferred development style.
9 .NET API products for your office documents