ImageGear .NET v24.12 - Updated
Split a PDF Document
User Guide > How to Work with... > PDF > How to... > Split a PDF Document

Splitting a PDF document is the process of saving each page into a new, separate PDF document. For example, splitting a three-page PDF document will produce three, single-page PDF documents.

To split a PDF document using ImageGear .NET:

  1. Read the PDF document into a System.IO.Stream object.
  2. Read the stream using ImGearFileFormats.LoadDocument(), casting the ImGearDocument object returned to an ImGearPDFDocument object.
  3. Determine the PDF page count using the ImGearPDFDocument.Pages property.
  4. For each PDF page:

The following is a sample method that illustrates how to split a single PDF document into several single-page PDF documents:

C#
Copy Code
using System;
using System.IO;
using System.Text;

using ImageGear.Core;
using ImageGear.Formats;
using ImageGear.Windows.Forms;
using ImageGear.Formats.PDF;

public void SplitPDFDocument(String inputPath, String outputDirectory)
{
   const int FIRST_PAGE = 0;
   const int ONE_PAGE = 1;

   // Ensure output directory exists.
   if ( !System.IO.Directory.Exists(outputDirectory) )
   {
       Directory.CreateDirectory(outputDirectory);
   }

   // Open file for reading.
   using ( FileStream pdfData = new FileStream(inputPath, FileMode.Open,
       FileAccess.Read) )
   {
       // Read PDF document to memory.
       using ( ImGearPDFDocument igSourceDocument = ImGearFileFormats.LoadDocument(
           pdfData, FIRST_PAGE, (int)ImGearPDFPageRange.ALL_PAGES)
           as ImGearPDFDocument )
       {
           // For each page in document.
           for ( int i = 0; i < igSourceDocument.Pages.Count; i++ )
           {
               // Construct the output filepath.
               String outputFileName = String.Format("{0}_{1}.pdf",
                   Path.GetFileNameWithoutExtension(inputPath), i + 1);
               String outputPath = System.IO.Path.Combine(outputDirectory, outputFileName);

               // Create a new empty PDF document.
               using ( ImGearPDFDocument igTargetDocument = new ImGearPDFDocument() )
               {
                   // Insert page into new PDF document.
                   igTargetDocument.InsertPages(
                       (int)ImGearPDFPageNumber.BEFORE_FIRST_PAGE, igSourceDocument,
                       i, ONE_PAGE, ImGearPDFInsertFlags.DEFAULT);

                   // Save new PDF document to file.
                   igTargetDocument.Save(outputPath, ImGearSavingFormats.PDF,
                       FIRST_PAGE, FIRST_PAGE, ONE_PAGE,
                       ImGearSavingModes.OVERWRITE);
               }
           }
       }
   }
}
VB.NET
Copy Code
Imports System
Imports System.IO
Imports System.Text

Imports ImageGear.Core
Imports ImageGear.Formats
Imports ImageGear.Formats.PDF

Public Sub SplitPDFDocument(ByVal inputPath As String, ByVal outputDirectory As String)

   Const FIRST_PAGE As Integer = 0
   Const ONE_PAGE As Integer = 1

   ' Ensure output directory exists.
   If Not System.IO.Directory.Exists(outputDirectory) Then
       Directory.CreateDirectory(outputDirectory)
   End If

   ' Open file for reading.
   Using pdfData As New FileStream(inputPath, FileMode.Open, FileAccess.Read)

       ' Read PDF document to memory.
       Using igSourceDocument As ImGearPDFDocument =
           TryCast(ImGearFileFormats.LoadDocument(
               pdfData, FIRST_PAGE, DirectCast(ImGearPDFPageRange.ALL_PAGES,
               Integer)), ImGearPDFDocument)

           ' For each page in document
           For i As Integer = 0 To igSourceDocument.Pages.Count

               ' Construct the output filepath
               Dim outputFileName As String = String.Format("{0}_{1}.pdf",
                   Path.GetFileNameWithoutExtension(inputPath), i + 1)
               Dim outputPath As String = System.IO.Path.Combine(outputDirectory,
                   outputFileName)

               ' Create a new empty PDF document.
               Using igTargetDocument As New ImGearPDFDocument()

                   ' Insert page into new PDF document.
                   igTargetDocument.InsertPages(
                       DirectCast(ImGearPDFPageNumber.BEFORE_FIRST_PAGE, Integer),
                       igSourceDocument, i, ONE_PAGE, ImGearPDFInsertFlags.[DEFAULT])

                   ' Save new PDF document to file.
                   igTargetDocument.Save(outputPath, ImGearSavingFormats.PDF,
                       FIRST_PAGE, FIRST_PAGE, ONE_PAGE,
                       ImGearSavingModes.OVERWRITE)

               End Using

           Next

       End Using

   End Using

End Sub

Refer to Samples documentation.