Splitting Multiple PDF Files in C# and .NET

Splitting PDF files into separate pages or groups of pages is a common task in software development. This may be necessary for processing large documents, organizing content, or optimizing data management. In this article, we will look at how you can split PDF files into separate pages or groups of pages using C# and .NET.

To complete the task of splitting PDF files, we will need to perform the following steps:

  1. Add SautinSoft.Pdf from NuGet.
  2. Split the PDF document into pages.
  3. Save each page as a separate PDF file.

Output result:

Following the above code example, developers can easily integrate PDF separation functionality into their applications, ensuring efficient document processing.

Полный код

using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
using System.Linq;
using SautinSoft.Pdf.Facades;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Split PDF files in C# and .NET.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/split-pdf-files.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            // Split PDF document by pages.
            // The each page will be saves as a separate PDF file: "Page 0.pdf", "Page 1.pdf" ...
            // Can work with relative and absolute paths.
            PdfSplitter.Split(@"..\..\..\005.pdf", PdfLoadOptions.Default, 0,
                int.MaxValue, (pageInd) => $"Page {pageInd}.pdf");

            // The last parameter is "Func" to generate the output file name.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"Page 0.pdf") { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports System.IO.Compression
Imports System.Collections.Generic
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Imports System.Linq
Imports SautinSoft.Pdf.Facades

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' Split PDF files in C# and .NET.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/split-pdf-files.php
		''' </remarks>
		Shared Sub Main(ByVal args() As String)
			' Before starting this example, please get a free license:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			' Split PDF document by pages.
			' The each page will be saves as a separate PDF file: "Page 0.pdf", "Page 1.pdf" ...
			' Can work with relative and absolute paths.
			PdfSplitter.Split("..\..\..\005.pdf", PdfLoadOptions.Default, 0, Integer.MaxValue, Function(pageInd) $"Page {pageInd}.pdf")

			' The last parameter is "Func" to generate the output file name.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Page 0.pdf") With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.