Tomas, CEO

How to Split PDF in C#: Step-by-Step Guide with Working Code

Splitting a PDF document using code is a basic task in many C# and ASP.NET applications. Important for workflows such as processing large documents, extracting specific pages, or automating report generation. This guide will show you how to extract individual pages, divide by custom ranges or segment by text markers.

You will learn how to use ConvertAPI’s Split PDF converter from a .NET application with the official .NET SDK. This guide covers everything from extracting single pages to splitting based on patterns. You will find working code that you can easily add to your project.

Table of Contents

Prerequisites

  • A ConvertAPI account (grab your API token from the dashboard)
  • .NET 6.0 or later
  • A sample PDF file (e.g. sample.pdf - you can use any local PDF file you have)
  • Basic knowledge of C# and project setup

Installing the ConvertAPI .NET SDK

Add the official ConvertAPI package via NuGet:

dotnet add package ConvertApi

In your code file, include the namespace:

using ConvertApiDotNet;

Authentication

Before you can begin using ConvertAPI's functionality, you need to authenticate your requests using your secret API key. You can find your API token by signing into your ConvertAPI Dashboard.

To initialize the SDK with your API token, simply include the following line after installing the SDK:

var convertApi = new ConvertApi("api-token");

Bookmark-Based Splitting

Use the SplitByBookmark parameter to break your PDF at each bookmark level, generating a separate file for each bookmark entry:

var convert = await convertApi.ConvertAsync("pdf", "split",
    new ConvertApiFileParam("File", "files/sample.pdf"),
    // Create parameter to split into bookmarks
    new ConvertApiParam("SplitByBookmark", "true")
);
await convert.SaveFilesAsync("output/bookmark-splits"); 

Split a PDF by bookmarks

Pattern-Based Splitting

To split your document into consecutive chunks of specified sizes, use the SplitByPattern parameter, passing a comma-separated sequence of positive integers. For example, "2,3" would create one file with the first 2 pages and a second file with the next 3 pages.

var convert = await convertApi.ConvertAsync("pdf", "split",
    new ConvertApiFileParam("File", "files/sample.pdf"),
    // Chunks of 2 and 3 pages
    new ConvertApiParam("SplitByPattern", "2,3")
);
await convert.SaveFilesAsync("output/pattern-splits"); 

Split PDF by pattern

Text-Based Splitting

Use the SplitByTextPattern parameter with a regular expression, each time the pattern matches, a new split begins. This is ideal for dividing by chapters, sections, or any content marked by a consistent pattern.

var convert = await convertApi.ConvertAsync("pdf", "split",
    new ConvertApiFileParam("File", "files/sample.pdf"),
    // Matches "Chapter " followed by one or more digits
    new ConvertApiParam("SplitByTextPattern", @"Chapter\s+\d+") 
);
await convert.SaveFilesAsync("output/text-pattern-splits"); 

Split PDF using regex

Extracting Specific Pages

The ExtractPages parameter allows you to extract a specific range of pages, creating a separate output file for each individual page within that range. This is useful for getting single-page PDFs from a larger document. It accepts comma-separated page numbers or ranges (e.g., "5-10", "1,3,7").

var convert = await convertApi.ConvertAsync("pdf", "split",
    new ConvertApiFileParam("File", "files/sample.pdf"),
    // Extracts pages 5 through 10, saving each as a separate file (e.g., page-5.pdf, page-6.pdf, etc.)
    new ConvertApiParam("ExtractPages", "5-10")
);
await convert.SaveFilesAsync("output/extracted-pages");

Extract pages as individual files

Splitting by Ranges

Use the SplitByRange parameter to specify exact pages or ranges of pages to be included in separate output files. You can combine individual page numbers and ranges using commas (e.g., "1,3,5-7"). This will create one file for each specified number or range.

var convert = await convertApi.ConvertAsync("pdf", "split",
    new ConvertApiFileParam("File", "files/sample.pdf"),
    // Creates three output files: one with page 1, one with page 3, and one with pages 5 through 7 combined.
    new ConvertApiParam("SplitByRange", "1,3,5-7")
);
await convert.SaveFilesAsync("output/split-by-ranges"); 

Split PDF by page ranges

Merging Output

The MergeOutput boolean parameter (set to "true") can be used in conjunction with splitting parameters like SplitByRange or ExtractPages when those parameters would normally result in multiple output files. Setting MergeOutput to "true" will instead merge all the resulting pages or ranges from the split operation into a single output PDF file.

var convert = await convertApi.ConvertAsync("pdf", "split",
    new ConvertApiFileParam("File", "files/sample.pdf"),
    // Specify ranges to split by
    new ConvertApiParam("SplitByRange", "1,3,5-7"),
    // Set MergeOutput to true to merge these ranges into a single file
    new ConvertApiParam("MergeOutput", "true")
);
// This will save a single file containing page 1, page 3, and pages 5-7
await convert.SaveFilesAsync("output/merged-range-output");

Merge splitted PDF output

Complete Example

using ConvertApiDotNet;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var convertApi = new ConvertApi("api-token");

        Directory.CreateDirectory("output/bookmark-splits-example");
        Directory.CreateDirectory("output/pattern-splits-example");
        Directory.CreateDirectory("output/text-pattern-splits-example");
        Directory.CreateDirectory("output/extract-specific-pages-example");
        Directory.CreateDirectory("output/split-by-ranges-example");
        Directory.CreateDirectory("output/merged-output-example");

        var res1 = await convertApi.ConvertAsync("pdf", "split",
            new ConvertApiFileParam("File", "files/sample.pdf"),
            new ConvertApiParam("SplitByBookmark", "true")
        );
        await res1.SaveFilesAsync("output/bookmark-splits-example");

         var res2 = await convertApi.ConvertAsync("pdf", "split",
            new ConvertApiFileParam("File", "files/sample.pdf"),
            new ConvertApiParam("SplitByPattern", "2,3")
        );
        await res2.SaveFilesAsync("output/pattern-splits-example");

        var res3 = await convertApi.ConvertAsync("pdf", "split",
            new ConvertApiFileParam("File", "files/sample.pdf"),
            new ConvertApiParam("SplitByTextPattern", @"Chapter \d+:")
        );
        await res3.SaveFilesAsync("output/text-pattern-splits-example");

         var res4 = await convertApi.ConvertAsync("pdf", "split",
            new ConvertApiFileParam("File", "files/sample.pdf"),
            new ConvertApiParam("ExtractPages", "5-10")
        );
        await res4.SaveFilesAsync("output/extract-specific-pages-example");

         var res5 = await convertApi.ConvertAsync("pdf", "split",
            new ConvertApiFileParam("File", "files/sample.pdf"),
            new ConvertApiParam("SplitByRange", "1,3,5-7")
        );
        await res5.SaveFilesAsync("output/split-by-ranges-example");

         var res6 = await convertApi.ConvertAsync("pdf", "split",
            new ConvertApiFileParam("File", "files/sample.pdf"),
            new ConvertApiParam("SplitByRange", "2,4-6,8"),
            new ConvertApiParam("MergeOutput", "true")
        );
        await res6.SaveFilesAsync("output/merged-output-example");

        Console.WriteLine("All splits complete!");
    }
}

Also, check out the full example project on GitHub: ConvertAPI Split PDF C# Examples

Conclusion

You now have a comprehensive approach to various PDF splitting tasks in C# using ConvertAPI. Whether you’re extracting individual pages or ranges, dividing by ranges or page counts, segmenting at text patterns, or splitting based on bookmarks, the official .NET SDK makes it straightforward. Integrate these snippets into your ASP.NET apps or background services to automate document workflows in minutes.

Ready to try it? Head over to the ConvertAPI PDF Split for C# page and get started today!


Related converters

Ready to Streamline Your File Conversions?