Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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 PDFs 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
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
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.
using IronPdf;
class Program
{
static void Main(string[] args)
{
string file = "input.pdf";
// The folder to save the split PDFs
string outputFolder = "output_split";
int numberOfSplitFiles = 3; // Specify how many parts you want to split the PDF into
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int numberOfSplitFiles)
{
// Load the input PDF
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
// Initialize page range values
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);
// Generate the output file path
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
// Save the new split PDF
newSplitPDF.SaveAs(name);
// Update page range values for the next iteration
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
string file = "input.pdf";
// The folder to save the split PDFs
string outputFolder = "output_split";
int numberOfSplitFiles = 3; // Specify how many parts you want to split the PDF into
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int numberOfSplitFiles)
{
// Load the input PDF
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
// Initialize page range values
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);
// Generate the output file path
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
// Save the new split PDF
newSplitPDF.SaveAs(name);
// Update page range values for the next iteration
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim file As String = "input.pdf"
' The folder to save the split PDFs
Dim outputFolder As String = "output_split"
Dim numberOfSplitFiles As Integer = 3 ' Specify how many parts you want to split the PDF into
' Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles)
End Sub
Private Shared Sub SplitPdfUsingIronPDF(ByVal inputPdfPath As String, ByVal outputFolder As String, ByVal numberOfSplitFiles As Integer)
' Load the input PDF
Dim sourceFile As PdfDocument = PdfDocument.FromFile(inputPdfPath)
' Initialize page range values
Dim firstPage As Integer = 1
Dim lastPage As Integer = 2
'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 = sourceFile.PageCount / numberOfSplitFiles
For i As Integer = 1 To numberOfSplitFiles
' Copy multiple pages into a new document
Dim newSplitPDF As PdfDocument = sourceFile.CopyPages(firstPage, lastPage)
' Generate the output file path
Dim name As String = $"{outputFolder}\SplitPDF_IronPDF_{i}.pdf"
' Save the new split PDF
newSplitPDF.SaveAs(name)
' Update page range values for the next iteration
firstPage = lastPage + 1
lastPage += totalPageInOneFile
Next i
End Sub
End Class
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.
inputPdfPath
: A string representing the path to the input PDF file (e.g., "input.pdf").outputFolder
: A string representing the output folder where the split PDF files will be saved (e.g., "output_split").numberOfSplitFiles
: An integer indicating how many smaller PDF files the original PDF will be split into.Inside the SplitPdfUsingIronPDF
method:
PdfDocument
object named sourceFile
is created by loading the PDF from the specified inputPdfPath
.firstPage
and lastPage
, are initialized. These represent the page range for splitting.totalPageInOneFile
is calculated by dividing the total page count of the source PDF by the specified numberOfSplitFiles
.numberOfSplitFiles
:PdfDocument
object named newSplitPDF
is created.firstPage
to lastPage
(inclusive) are copied from the sourceFile
to newSplitPDF
.firstPage
and lastPage
values are updated for the next iteration.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.
using System;
using iText.Kernel.Pdf;
class Program
{
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; // Specify how many parts you want to split the PDF into
// 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))
{
// Calculate the number of pages for each split file
int totalPageInOneFile = doc.GetNumberOfPages() / numberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile;
for (int i = 1; i <= numberOfSplitFiles; i++)
{
// Generate the output file path
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
// Create a new document and attach a writer for the specified output file
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename)))
{
// Copy pages from the original document to the new document
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
// Update page range values for the next iteration
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
}
using System;
using iText.Kernel.Pdf;
class Program
{
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; // Specify how many parts you want to split the PDF into
// 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))
{
// Calculate the number of pages for each split file
int totalPageInOneFile = doc.GetNumberOfPages() / numberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile;
for (int i = 1; i <= numberOfSplitFiles; i++)
{
// Generate the output file path
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
// Create a new document and attach a writer for the specified output file
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename)))
{
// Copy pages from the original document to the new document
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
// Update page range values for the next iteration
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
}
Imports System
Imports iText.Kernel.Pdf
Friend Class Program
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 ' Specify how many parts you want to split the PDF into
' Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, numberOfSplitFiles)
End Sub
Private 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)
' Calculate the number of pages for each split file
'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
For i As Integer = 1 To numberOfSplitFiles
' Generate the output file path
Dim filename As String = $"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"
' Create a new document and attach a writer for the specified output file
Using pdfDocument As New PdfDocument(New PdfWriter(filename))
' Copy pages from the original document to the new document
doc.CopyPagesTo(firstPage, lastPage, pdfDocument)
End Using
' Update page range values for the next iteration
firstPage = lastPage + 1
lastPage += totalPageInOneFile
Next i
End Using
End Using
End Sub
End Class
This 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.
SplitPdfUsingiTextSharp
method.inputPdfPath
, outputFolder
, and numberOfSplitFiles
.Inside the SplitPdfUsingiTextSharp
method:
PdfReader
object named reader
is created by loading the PDF from the specified inputPdfPath
.PdfDocument
object named doc
is initialized using the reader
.numberOfSplitFiles
to determine how many pages each smaller PDF should contain.A loop iterates from 1 to numberOfSplitFiles
:
pdfDocument
), pages are copied from the original doc
:firstPage
to lastPage
(inclusive) are copied.firstPage
and lastPage
values are updated for the next iteration.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.
Two popular libraries for splitting PDFs in C# are iTextSharp and IronPDF. Both offer robust functionality for PDF manipulation. IronPDF provides a user-friendly API for these tasks.
To split a PDF using iTextSharp, you need to create a PdfReader, use a PdfDocument to work with the content, determine page ranges, and copy pages into new PdfDocuments, saving each as a separate file.
IronPDF simplifies the process by providing a straightforward API that allows you to load a PDF document, specify page ranges, and save the split sections as new PDF files with minimal code.
To install IronPDF, use the NuGet Package Manager Console in Visual Studio with the command 'Install-Package IronPdf'. Alternatively, you can add the package via the NuGet Package Manager UI or download it from the official website.
IronPDF offers a more straightforward API, better performance, and memory efficiency, making it easier for developers to implement PDF splitting with fewer steps compared to other libraries.
Yes, IronPDF can also be used for creating, modifying, merging PDFs, and converting HTML to PDF, among other tasks, providing a comprehensive solution for PDF manipulation.
IronPDF generally offers better performance due to its efficient page copying and fewer steps required, while other libraries like iTextSharp may use more memory due to repeated creation and disposal of PdfDocument instances.
To install iTextSharp, use the Package Manager Console in Visual Studio with the command 'Install-Package itext7'.
Consider factors like ease of use, code readability, performance, and memory usage. IronPDF is simpler and more readable, while other libraries like iTextSharp offer extensive functionality but may require more complex code.
Yes, IronPDF offers a free trial for developers to explore its features before committing to a purchase, allowing you to evaluate its capabilities.