DEV Community

Cover image for How to split PDF File in C# (Code Example Tutorial)
DroWzYOzIl
DroWzYOzIl

Posted on

How to split PDF File in C# (Code Example Tutorial)

Are you looking for how to convert a single PDF document int multiple pdf files using C# .NET programming language?

In this tutorial we will discuss how to split single PDF file into multiple PDF documents. There are various advantages to splitting a PDF, and you should be able to do it while keeping the entire document intact. One of the easiest ways to reduce the size of a PDF file without damaging it is IronPDF the C# .NET Library specifically created for creating and manipulating.

IronPDF

IronPDF PDF library is a robust Hyper Text Markup Language to PDF conversion API. IronPDF is an all-in-one solution for converting HTML sites in .NET and .NET Core development. It not only converts HTML but also performs a number of other tasks. IronPDF enables developers to generate, modify, and retrieve PDF files from .NET framework and Core projects. IronPDF allows developers to effortlessly create or modify high-fidelity PDFs from HTML pages.

PDF files are lightweight and a great mode of sending content over the internet with ease. Creating such PDF files Using IronPDF is easy, efficient, time saving and reduce workload on developers by one third.

To get started with IronPDF all you need is Visual studio and C# installed on your system and follow this step-by-step tutorial.

1. Creating a New Project in Visual Studio

Open the Visual studio editor

Go to the File menu in Visual Studio after starting it up. Select Console Application after choosing "New project".

In the relevant text box, type the project name and choose the path. Then, click the Create button. Select the required .NET framework, as in the screenshot below:

Creating a New Project in Visual Studio 1
Then a new window will appear select the target framework and click on create.

Creating a New Project in Visual Studio 2
program.cs file will open so you can enter the logic and create/run the application.

Creating a New Project in Visual Studio 3
Now we can add the IronPDF library and test the program.

2. Install the IronPDF C# Library

The IronPDF library can be downloaded and installed in many different ways. but we will only discuss only three of those:

Using the Visual Studio NuGet Package Manager

Using the Visual Studio Command-Line

Direct Download from the NuGet webpage

2.1. Using the Visual Studio NuGet Package Manager

The NuGet Package Manager option is available in the Visual Studio software to install the package directly into the solution. The below screenshot shows how to open it.

Using the Visual Studio NuGet Package Manager 4
It has a search box that returns a list of the package libraries available on the NuGet website. As shown in the screenshot below, we need to search for the keyword "IronPDF" in the package manager.

 Using the Visual Studio NuGet Package Manager 5
We can see the list of linked packages from the search in the image up above. We need to select the IronPDF option and install the package to our solution.

Using the Visual Studio NuGet Package Manager 6

2.2. Using the Visual Studio Command-Line

In Visual Studio menu go to Tools, move the cursor to NuGet package Manager and click on Package Manager Console.

Using the Visual Studio Command-Line 7
Package Manager Console will appear in the bottom of the screen just write the following command and press enter and IronPDF will install in an instance.
Install-Package IronPDF

Using the Visual Studio Command-Line 8

2.3 Direct Download from the NuGet webpage

The third way is to download the NuGet package directly from the webpage.

Navigate to the link "https://www.nuget.org/packages/IronPdf/"

Select the download package option from the menu on the right-hand side.

Double-click the downloaded package; it will be installed automatically.

Now reload the solution and begin using it in the project.

3. Splitting PDF document Using IronPDF

IronPDF offers PDF document manipulation to split one pdf document to two or more output PDF files. To split pdf files all you need is a source PDF file or create a new PDF document then split a PDF document using page numbers. Using IronPDF you can split PDF file into multiple files to reduce large PDF documents or to reduce PDF file size. Multiple single page files can be created using this C# .NET library.

In this tutorial we will discuss two different examples of splitting a three-page PDF document into two files and three files.

3.1. Slitting PDF file into two PDF files

Here we will see how to split single PDF document in to two individual files using C# programming language and IronPDF PDF library.

Following code sample shows how to render all the pages of the PDF document and split in into multiple PDF files.

using IronPdf;

// Instantiate Renderer
var pdf = new PdfDocument("url.pdf");
//take the first page
var pdf_page1 = pdf.CopyPage(0);
pdf_page1.SaveAs("Spli1.pdf");
//take the pages 2 & 3
var pdf_page2_3 = pdf.CopyPages(1, 2);
pdf_page2_3.SaveAs("Spli2.pdf");
Enter fullscreen mode Exit fullscreen mode

OUTPUT File 1

This output shows the result of above code sample shows the output of three-page PDF document split into a single page.

Slitting PDF file into two PDF files 9
OUTPUT File 2

This output image shows the output of remaining two pages of the original PDF document.

Slitting PDF file into two PDF files 10

3.2. Splitting PDF Document into multiple Single Page Files

Here we will split a PDF document into three single page PDF document using IronPDF split function.

using IronPdf;

// Instantiate Renderer
var pdf = new PdfDocument("url.pdf");

//take the first page
var pdf_page1 = pdf.CopyPage(0);
pdf_page1.SaveAs("Spli1.pdf");

//take the page 2 
var pdf_page2 = pdf.CopyPage(1);
pdf_page2.SaveAs("Spli2.pdf");

//take the page 3
var pdf_page3 = pdf.CopyPage(2);
pdf_page3.SaveAs("Spli3.pdf");
Enter fullscreen mode Exit fullscreen mode

OUTPUT File 1

Splitting PDF Document into multiple Single Page Files 11
OUTPUT File 2

Splitting PDF Document into multiple Single Page Files 12
OUTPUT File 3

Splitting PDF Document into multiple Single Page Files 13

4. Conclusion

In this tutorial article we have discussed how to split a PDF document into multiple pages using IronPDF library. here we show how easy it is to split PDF file into two or three different PDF files based on their pages. Also, IronPDF keeps the output identical to original PDF document and also maintain the integrity of the Links used and formatting.
For more examples on IronPDF please visit this link. and to know about how IronPDF works and features its offers to its users please visit this link.
If you buy the complete Iron Suite, you will get all 5 Products for the Price of 2. For further details about the licensing, please follow this link to Purchase the complete Package.

Latest comments (0)