How to Split PDF File by Pages in C#

This basic tutorial provides the details on how to split PDF file by pages in C#. By following the steps and the code snippet in C# split PDF into multiple files using a few lines of code. Moreover, you can customize the splitting process by filtering the page number or performing a variety of operations before creating a separate document out of it.

Steps to Split PDF File by Pages in C#

  1. Add the reference to Aspose.PDF for .NET library in the application for splitting the PDF pages
  2. Load the input PDF file for splitting using the Document class
  3. Iterate through each page of the input PDF file using the Page class
  4. Write each page to a separate PDF document with the Save method

These steps simply explain how using C# split PDF into pages operation can be performed. Quickly configure the library and then load the input document for processing it. Iterate through each page one by one and save it as a separate file along with the option to rotate, add some header/footer, add watermark, crop the page, and a lot of other customizations too.

Code to Split PDF File by Pages in C#

using Aspose.Pdf;
namespace SplitPdfFileByPagesInCSharp
{
class Program
{
static void Main(string[] args)
{
// Load the license to avoid trial version watermark in the splitted PDF files
License pdfLicense = new License();
pdfLicense.SetLicense("Aspose.PDF.lic");
// Load input PDF file
Document document = new Document( "Test.pdf");
// Loop through each page
foreach (Page page in document.Pages)
{
// Create a new Document class object to split PDF pages
Document splitPDF = new Document();
// Add current page to the document
splitPDF.Pages.Add(page);
// Save current page as a seprate PDF
splitPDF.Save("Page number " + page.Number + ".pdf");
}
System.Console.WriteLine("Done");
}
}
}

For writing an application to split PDF pages C# code is demonstrated here. Before adding each page to a new PDF file, you can use all Page class features like you can change the page size, adding some background, stamping the page with image or text, setting page color, and so on. Similarly, after adding the page to a new PDF file, you can use Document class features like formatting it by embedding fonts into it, setting its expiry date, optimizing the PDF file by removing unused objects, compressing images, and modifying the contents in a variety of ways.

In this article, we have learned how using C# split PDF into pages functionality can be embedded into your .NET or .NET Core framework-based applications. Similarly, if you want to learn the merging of PDF pages, refer to the article on how to merge PDF files using C#.

 English