In the previous article, you have seen how to merge multiple PDF files into a single PDF. However, there could be a case when you need to split a single PDF file into multiple files. You can either split each page of the PDF or a collection of pages into multiple PDFs. In this article, you will learn how to cope up with such scenarios and split a PDF file using C#.
Split PDF C# API - Free Download
Aspose.PDF for .NET is a powerful PDF file manipulation API that lets you create, edit, and process PDF documents from within your .NET applications. In addition, the API allows you to split a single PDF into multiple files with high fidelity. You can download the API’s DLL or get it installed using NuGet.
PM> Install-Package Aspose.Pdf
Split a PDF File using C#
The PDF splitting criteria can vary as per your requirements. You can either split the document by each page or a collection of pages. First, let’s see how to split every page of a PDF file.
- Load the PDF document using Document class.
- Loop through the Document.Pages collection to access each page using Page class.
- In every iteration, create a new Document, add the current page to the document and save it as a PDF file using Document.Save(String) method.
The following code sample shows how to split a PDF document using C#.
Split Selected Pages of PDF using C#
You can also split the PDF by specifying the range of pages. For example, you can split the first or last N number pages, the even or odd pages, and etc. For the demonstration, the following are the steps to split even and odd pages from the PDF.
- Load the PDF document using Document class.
- Get the pages to be split into a Page[] array.
- Create a new Document and add pages to it using Document.Pages.Add(Page[]) method.
- Save the PDF file using the Document.Save(String) method.
The following code sample shows how to split a collection of pages from PDF.
Conclusion
In this article, you have learned how to split the PDF files using C#. The code samples demonstrated how to split every page or a collection of pages in a PDF to separate files. You can explore more about the .NET PDF API using the documentation.