Split PDF file in C# and VB.NET

With GemBox.Pdf, you can split content of one PDF file to several PDF files.

The following example demonstrates how to split a PDF file so that each page goes into its own PDF file.

Screenshot
PDF Library for C# / VB.NET Screenshot

See the full code below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SplitFileCs.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Split PDF file in C# and VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
        <br />
        Upload PDF file:<br />
        <br />
        <asp:FileUpload ID="pdfFileUpload" runat="server" Width="317px" />
        <br />
        <br />
        <asp:Button ID="generateZipButton" runat="server" OnClick="generateZipButton_Click" Text="Generate ZIP file" Width="128px" />
        <br />
    </form>
</body>
</html>
using System;
using System.IO;
using System.IO.Compression;
using GemBox.Pdf;

namespace SplitFileCs
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        }

        protected void generateZipButton_Click(object sender, EventArgs e)
        {
            if (!this.pdfFileUpload.HasFile)
                return;

            string fileNameWithoutExt = Path.GetFileNameWithoutExtension(this.pdfFileUpload.FileName);
            string fileExtension = Path.GetExtension(this.pdfFileUpload.FileName);

            if (fileExtension.ToUpperInvariant() != ".PDF")
            {
                this.Response.Write("Invalid file extension.");
                return;
            }

            // Open source PDF file.
            using (PdfDocument source = PdfDocument.Load(this.pdfFileUpload.FileContent))
            {
                using (MemoryStream archiveStream = new MemoryStream())
                {
                    // Create a destination ZIP file.
                    using (ZipArchive archive = new ZipArchive(archiveStream, ZipArchiveMode.Update, true))
                        // For each source document page:
                        for (int index = 0; index < source.Pages.Count; index++)
                        {
                            // Create new ZIP entry.
                            ZipArchiveEntry entry = archive.CreateEntry(string.Format("{0}{1}{2}", fileNameWithoutExt, index + 1, fileExtension));
                            // Open ZIP entry stream.
                            using (Stream entryStream = entry.Open())
                            // Create destination document.
                            using (PdfDocument destination = new PdfDocument())
                            {
                                // Clone source document page to destination document.
                                destination.Pages.AddClone(source.Pages[index]);

                                // Save destination document to ZIP entry stream.
                                destination.Save(entryStream);
                            }
                        }

                    this.Response.Clear();
                    this.Response.BufferOutput = false;
                    this.Response.ContentType = "application/zip";
                    this.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.zip", fileNameWithoutExt));

                    archiveStream.WriteTo(this.Response.OutputStream);

                    this.Response.End();
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression
Imports GemBox.Pdf

Public Class Index
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
    End Sub

    Protected Sub generateZipButton_Click(sender As Object, e As EventArgs) Handles generateZipButton.Click

        If Not Me.pdfFileUpload.HasFile Then
            Return
        End If

        Dim fileNameWithoutExt As String = Path.GetFileNameWithoutExtension(Me.pdfFileUpload.FileName)
        Dim fileExtension As String = Path.GetExtension(Me.pdfFileUpload.FileName)

        If fileExtension.ToUpperInvariant() <> ".PDF" Then
            Me.Response.Write("Invalid file extension.")
            Return
        End If

        ' Open source PDF file.
        Using source As PdfDocument = PdfDocument.Load(Me.pdfFileUpload.FileContent)
            Using archiveStream = New MemoryStream()
                ' Create a destination ZIP file.
                Using archive = New ZipArchive(archiveStream, ZipArchiveMode.Update, True)
                    ' For each source document page:
                    For index As Integer = 0 To source.Pages.Count - 1
                        ' Create new ZIP entry.
                        Dim entry As ZipArchiveEntry = archive.CreateEntry(String.Format("{0}{1}{2}", fileNameWithoutExt, index + 1, fileExtension))
                        ' Open ZIP entry stream.
                        Using entryStream As Stream = entry.Open()
                            ' Create destination document.
                            Using destination As PdfDocument = New PdfDocument()
                                ' Clone source document page to destination document.
                                destination.Pages.AddClone(source.Pages(index))

                                ' Save destination document to ZIP entry stream.
                                destination.Save(entryStream)
                            End Using
                        End Using
                    Next
                End Using

                Me.Response.Clear()
                Me.Response.BufferOutput = False
                Me.Response.ContentType = "application/zip"
                Me.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}.zip", fileNameWithoutExt))

                archiveStream.WriteTo(Me.Response.OutputStream)

                Me.Response.End()

            End Using
        End Using

    End Sub
End Class