It's helpful to split a single PDF into several smaller ones in certain situations. For example, you can divide large contracts, reports, books, academic papers, or other documents into smaller pieces make them easy to review or reuse. In this article, you will learn how to split PDF into single-page PDFs and how to split PDF by page ranges in C# and VB.NET by using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Split PDF into One-Page PDFs in C#, VB.NET
Spire.PDF offers the Split() method to divide a multi-page PDF document into multiple single-page files. The following are the detailed steps.
- Create a PdfDcoument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Split the document into one-page PDFs using PdfDocument.Split(string destFilePattern, int startNumber) method.
- C#
- VB.NET
using System; using Spire.Pdf; namespace SplitPDFIntoIndividualPages { class Program { static void Main(string[] args) { //Specify the input file path String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf"; //Specify the output directory String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a PDF file doc.LoadFromFile(inputFile); //Split the PDF to one-page PDFs doc.Split(outputDirectory + "output-{0}.pdf", 1); } } }
Split PDF by Page Ranges in C#, VB.NET
No straightforward method is offered for splitting PDF documents by page ranges. To do so, we create two or more new PDF documents and import the page or page range from the source document into them. Here are the detailed steps.
- Load the source PDF file while initialing the PdfDocument object.
- Create two additional PdfDocument objects.
- Import the first page from the source file to the first document using PdfDocument.InsertPage() method.
- Import the remaining pages from the source file to the second document using PdfDocument.InsertPageRange() method.
- Save the two documents as separate PDF files using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using System; namespace SplitPdfByPageRanges { class Program { static void Main(string[] args) { //Specify the input file path String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf"; //Specify the output directory String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Load the source PDF file while initialing the PdfDocument object PdfDocument sourceDoc = new PdfDocument(inputFile); //Create two additional PdfDocument objects PdfDocument newDoc_1 = new PdfDocument(); PdfDocument newDoc_2 = new PdfDocument(); //Insert the first page of source file to the first document newDoc_1.InsertPage(sourceDoc, 0); //Insert the rest pages of source file to the second document newDoc_2.InsertPageRange(sourceDoc, 1, sourceDoc.Pages.Count - 1); //Save the two documents as PDF files newDoc_1.SaveToFile(outputDirectory + "output-1.pdf"); newDoc_2.SaveToFile(outputDirectory + "output-2.pdf"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.