Splits PDF document by pages

Namespace:  SautinSoft
Assembly:  SautinSoft.ExcelToPdf (in SautinSoft.ExcelToPdf.dll) Version: 4.0.2.29

Syntax

      
 C#  Visual Basic 
public void SplitPDFFileToPDFFolder(
	string SourceFile,
	string OutputPath
)
Public Sub SplitPDFFileToPDFFolder ( _
	SourceFile As String, _
	OutputPath As String _
)

Parameters

SourceFile
String
Path to PDF file which you want to split by pages
OutputPath
String
Path to directory where to store each PDF page as single file after splitting

Return Value

0 - split successfully
2 - error, output directory doesn't exist

Remarks

Examples

CopySplit PDF by pages using C#
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 .xls.
            // 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 .xls.
                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();
        }
    }
}
CopySplit PDF by pages using VB.Net
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 .xls.
        ' 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 .xls.
            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

See Also