Split PDF Document into Several Ones – C#/VB.NET

In last post, I have shown my method to merge several documents into one, while, in this post, I want to show how to split one document with several pages into many ones.

When do we need to split PDF document? Take an example, there is a PDF document which shows three stories. If we just want to save them in three different documents respectively, we need to split the original document.

In my example, I have a PDF document with three pages. What I will do is to split them into three documents and open the last document.

Part of Original Document

At the beginning, add necessary references in project. Spire.PDF is also used in this method. Therefore, I add its dll reference as reference.

Step:

  1. Open the PDF document which I want to split into several ones.
  2. Save all pages in this PDF document as a document and name them as SplitDocument-{0}.pdf. Number in {} will be changed automatically according pages original document has. And number begins with 0. It means that if there are three pages, the split documents will be named SplitDocument-0.pdf, SplitDocument-1.pdf, SplitDocument-2.pdf.
  3. Declare one string lastPageFileName in order to be convenient for viewing the last document. The file name is name of the last document I split in the second step.
  4. Launch lastPageFileName document.

Main Coding:

C#

using System;

using Spire.Pdf;

namespace SplitPDF

{

    class Split

    {

        static void Main(string[] args)

        {

            //Open PDF

            PdfDocument doc = new PdfDocument(@"D:\work\My Documents\Breaking the Ice with Qawah.pdf");

 

            String pattern = "SplitDocument-{0}.pdf";

            doc.Split(pattern);

 

            String lastPageFileName

                = String.Format(pattern, doc.Pages.Count – 1);

 

            doc.Close();

 

            //Launch PDF

            System.Diagnostics.Process.Start(lastPageFileName);

        }

    }

}

VB.NET

Imports System

Imports Spire.Pdf

Namespace SplitPDF

       Friend Class Split

              Shared Sub Main(ByVal args() As String)

                     'Open PDF

                     Dim doc As New PdfDocument("D:\work\My Documents\Breaking the Ice with Qawah.pdf")

 

                     Dim pattern As String = "SplitDocument-{0}.pdf"

                     doc.Split(pattern)

 

                     Dim lastPageFileName As String = String.Format(pattern, doc.Pages.Count – 1)

 

                     doc.Close()

 

                     'Launch PDF

                     System.Diagnostics.Process.Start(lastPageFileName)

              End Sub

       End Class

End Namespace

Result Shown by Following:

____________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

 

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s