Split PDF files in C# and VB.NET

With /products/pdf/index.php, you can split a PDF file into several PDF files in your C# or VB.NET application.

Because PDF pages are self-contained (all information necessary for their appearance and interactivity is stored on or referenced from the page), they can be easily cloned into other PDF files.

To split a PDF file, simply clone each page into a new PDF file.

The following example shows how to split a PDF file so that each page is a separate PDF file.

Complete code

using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
using System.Linq;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Split PDF files.
        /// </summary>
        /// <remarks>
        /// Details: http://sautinsoft/products/pdf/help/net/developer-guide/split-pdf-files.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Open a source PDF file and create a destination ZIP file.
            using (var source = PdfDocument.Load(@"..\..\..\simple text.pdf"))
            using (var archiveStream = File.OpenWrite("Output.zip"))
            using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
            {
                // Iterate through the PDF pages.
                for (int pageIndex = 0; pageIndex < source.Pages.Count; pageIndex++)
                {
                    // Create a ZIP entry for each source document page.
                    var entry = archive.CreateEntry($"Page {pageIndex + 1}.pdf");

                    // Save each page as a separate destination document to the ZIP entry.
                    using (var entryStream = entry.Open())
                    using (var destination = new PdfDocument())
                    {
                        destination.Pages.AddClone(source.Pages[pageIndex]);
                        destination.Save(entryStream);
                    }
                }
            }
        }
    }
}

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.