How to split PDF document by pages
using C# and VB.Net.


Complete code.

using System;
using System.IO;
using SautinSoft;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Split PDF by pages.
            // 1. Let's get a PDF with 3 pages from .xlsx.
            // 2. Let's split it by 3 PDF files and show them.
            ExcelToPdf x = new ExcelToPdf();
            x.PageStyle.PageSize.Letter();
            x.PageStyle.PageMarginTop.mm(5);

            // Set PDF as output format.
            x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf;

            string excelFile = Path.GetFullPath(@"..\..\test.xlsx");
            FileInfo pdfFile = new FileInfo(Path.ChangeExtension(excelFile, ".pdf"));

            try
            {
                // 1. Let's get a PDF with 3 pages from .xlsx.
                x.ConvertFile(excelFile, pdfFile.FullName);

                // 2. Let's split it by 3 PDF files and show them.
                // Create a directory for storing separate PDFs.
                DirectoryInfo pdfDir = pdfFile.Directory.CreateSubdirectory("My Pages");
                x.SplitPDFFileToPDFFolder(pdfFile.FullName, pdfDir.FullName);

                // Show result
                FileInfo[] pageFiles = pdfDir.GetFiles("*.pdf");
                Console.WriteLine("Resulting PDF files:\n");
                foreach (FileInfo pageFile in pageFiles)
                {
                    Console.WriteLine(pageFile.Name);
                }
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);                
            }

            Console.ReadLine();
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft

Module Sample

    Sub Main()
        ' Split PDF by pages.
        ' 1. Let's get a PDF with 3 pages from .xlsx.
        ' 2. Let's split it by 3 PDF files and show them.
        Dim x As New ExcelToPdf()
        x.PageStyle.PageSize.Letter()
        x.PageStyle.PageMarginTop.mm(5)

        ' Set PDF as output format.
        x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf

        Dim excelFile As String = Path.GetFullPath("..\test.xlsx")
        Dim pdfFile As New FileInfo(Path.ChangeExtension(excelFile, ".pdf"))

        Try
            ' 1. Let's get a PDF with 3 pages from .xlsx.
            x.ConvertFile(excelFile, pdfFile.FullName)

            ' 2. Let's split it by 3 PDF files and show them.
            ' Create a directory for storing separate PDFs.
            Dim pdfDir As DirectoryInfo = pdfFile.Directory.CreateSubdirectory("My Pages")
            x.SplitPDFFileToPDFFolder(pdfFile.FullName, pdfDir.FullName)

            ' Show result
            Dim pageFiles() As FileInfo = pdfDir.GetFiles("*.pdf")
            Console.WriteLine("Resulting PDF files:" & ControlChars.Lf)
            For Each pageFile As FileInfo In pageFiles
                Console.WriteLine(pageFile.Name)
            Next pageFile

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

        Console.ReadLine()

    End Sub
End Module

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.