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.

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>
    <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 />
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)

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

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

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

            // 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.

                                // Save destination document to ZIP entry stream.

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


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
    End Sub

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

        If Not Me.pdfFileUpload.HasFile Then
        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.")
        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.

                                ' Save destination document to ZIP entry stream.
                            End Using
                        End Using
                End Using

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



            End Using
        End Using

    End Sub
End Class