How to Split PDF Files using C#

Split PDF into Multiple Files using C#

PDF is one of the most commonly used file formats which is highly portable. As a developer, you may have faced the scenario to split large PDF files programmatically. In one of the articles, we learned to split the PDF files in Java. Today, this article discusses different ways of how to split PDF files using C# in .NET applications.

.NET API to Split PDF Files

In order to split PDF files, we will use GroupDocs.Merger for .NET. It is the API that allows rapid development to integrate features with very few lines of code. In addition to splitting, it supports merging, swapping, or trimming documents of different file formats.

You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Merger

Split PDF File into MultiPage Files using C#

The following steps guide how you can split PDF files into multipage files using C#:

  • Define the output file(s) format.
  • Define the page intervals using SplitOptions.
  • Load the PDF file using Merger class.
  • Split the loaded PDF according to defined interval using Split() method.

The following code sample shows how to split PDF files into multipage files.

Extract Pages from PDF Files by Range

The following steps guide how to extract pages from PDF using C# by splitting according to the given range:

  • Define the output file(s) format.
  • Provide the pages range using SplitOptions.
  • Load the PDF file using Merger class.
  • Use Split() method to split the loaded PDF according to the defined range.

The following code snippet shows how to split PDF and extract pages by providing the range.

Extract Even/Odd Pages from PDF Files using C#

The following steps guide how to extract even/odd pages from a PDF file by splitting within the given range by just applying filters in C#:

  • Define the output file(s) format.
  • Provide the pages range using SplitOptions.
  • Apply the filter for even, odd, or all pages using RangeMode.
  • Load the PDF file using Merger class.
  • Use Split() method to separate the loaded PDF according to the defined filter.

The following code snippet shows how to extract all the odd/even pages in the defined range of a PDF file.

Split PDF File into Multiple Single-Page Files

The following steps guide how we can split a PDF to extract pages as multiple single-page files in C#:

  • Define the output file(s) format.
  • Define the exact page numbers using SplitOptions.
  • Load the PDF file using Merger class.
  • Split the loaded PDF according to defined pages using Split() method.

The following code sample shows how to split PDF files into multiple single-page files.

Code Change Summary

In all the scenarios, the thing that changes is the way to define SplitOptions. Here is the summary of the change in each code snippet for each scenario. You can use the following settings as per your requirements within your code. Here, I used a PDF file having 10 pages.

  • For Multipage Files – Use Interval: [1,2], [3,4,5], [6,7], [8,9,10].
new SplitOptions(outputFile,  new int[] { 3, 6, 8 }, SplitMode.Interval)
  • Extract Pages in Range: [3], [4], [5], [6]
new SplitOptions(outputFile, 3, 6);
  • Range with Filter: [3], [5], [7]
new SplitOptions(outputFile, 3, 8, (Integer)RangeMode.OddPages);
  • Individual Pages: [3], [4], [9]
new SplitOptions(outputFile, new int[] { 3, 4, 9 });

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To conclude, we discussed the ways to split PDF files using C#. First, we split the PDF file into multipage & single-page documents. We also extracted pages from PDF files. First, we extracted all the pages, and then even/odd pages within the given range. You can try building your own PDF splitting .NET App using GroupDocs.Merger API.

To learn more about the API, visit the documentation. For queries, contact us via the forum.

See Also