This example demonstrates how to split a PDF documents.
Spliting a pdf document is needed when you want to split a pdf in two pdf documents
Choose a pdf document that needed to be splitted
Split at is a range selection where we need to choose from which page number we want to split the document in two .
Click generate button will split pdf in two pages on the choosen range and will be showed in explorer
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
using OfficeComponent.Xps;
using Path = System.IO.Path;
namespace OfficeComponent.Samples
{
class SplitDocumentsExample : ExampleBase
#if WEB
, IUIExample
#endif
{
public string SplitDoc
{
get;
set;
}
public int SplitFrom
{
get;
set;
}
public SplitDocumentsExample()
: base(null, null)
{
}
public SplitDocumentsExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public SplitDocumentsExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
#if WEB
ProcessForm();
#endif
if (string.IsNullOrWhiteSpace(SplitDoc))
{
ShowError("Please specify the document to split.");
return null;
}
if (SplitFrom <= 0)
{
ShowError("Invalid split from page. Must be greater than 0.");
return null;
}
// Create a new instance of PdfDocument class.
PdfDocument doc = new PdfDocument();
using (PdfImportedDocument ldoc = new PdfImportedDocument(SplitDoc))
{
//Create two pdf documents
PdfDocument doc1 = new PdfDocument();
PdfDocument doc2 = new PdfDocument();
if (SplitFrom > ldoc.Pages.Count)
{
ShowError(string.Format("Invalid split from page. Must be lesser than or equal to {0}.", ldoc.Pages.Count));
return null;
}
var n = SplitFrom - 1;
// Split the source document into two based on the split-at page value.
for (int i = 0; i < n; i++)
doc1.ImportPage(ldoc, i);
for (int j = n; j < ldoc.Pages.Count; j++)
doc2.ImportPage(ldoc, j);
var name = OutputDir + "\\" + Path.GetFileNameWithoutExtension(SplitDoc) + "_" + Guid.NewGuid().ToString();
string doc1FileName = name + "_Doc1.pdf";
string doc2FileName = name + "_Doc2.pdf";
//Save pdf document1
doc1.Save(doc1FileName);
//Save pdf document2
doc2.Save(doc2FileName);
doc1.Close();
doc2.Close();
#if WEB
var outputFile = name + ".zip";
using (System.IO.Compression.ZipStorer zip = System.IO.Compression.ZipStorer.Create(outputFile, ""))
{
zip.AddFile(System.IO.Compression.ZipStorer.Compression.Store, doc1FileName, Path.GetFileName(doc1FileName), "");
zip.AddFile(System.IO.Compression.ZipStorer.Compression.Store, doc2FileName, Path.GetFileName(doc2FileName), "");
}
return outputFile;
#else
return OutputDir;
#endif
}
}
public override string ActionTitle
{
get { return "Split Document"; }
}
#if WEB
void ProcessForm()
{
SplitDoc = GetPostFile("SourcePdf");
}
#endif
}
}
Imports System.Drawing.Imaging
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Imports OfficeComponent.Xps
Imports Path = System.IO.Path
Namespace OfficeComponent.Samples
#If WEB Then
Friend Class SplitDocumentsExample
Inherits ExampleBase
Implements IUIExample
#Else
Friend Class SplitDocumentsExample
Inherits ExampleBase
#End If
Private privateSplitDoc As String
Public Property SplitDoc() As String
Get
Return privateSplitDoc
End Get
Set(ByVal value As String)
privateSplitDoc = value
End Set
End Property
Private privateSplitFrom As Integer
Public Property SplitFrom() As Integer
Get
Return privateSplitFrom
End Get
Set(ByVal value As Integer)
privateSplitFrom = value
End Set
End Property
Public Sub New()
MyBase.New(Nothing, Nothing)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
#If WEB Then
ProcessForm()
#End If
If String.IsNullOrWhiteSpace(SplitDoc) Then
ShowError("Please specify the document to split.")
Return Nothing
End If
If SplitFrom <= 0 Then
ShowError("Invalid split from page. Must be greater than 0.")
Return Nothing
End If
' Create a new instance of PdfDocument class.
Dim doc As New PdfDocument()
Using ldoc As New PdfImportedDocument(SplitDoc)
'Create two pdf documents
Dim doc1 As New PdfDocument()
Dim doc2 As New PdfDocument()
If SplitFrom > ldoc.Pages.Count Then
ShowError(String.Format("Invalid split from page. Must be lesser than or equal to {0}.", ldoc.Pages.Count))
Return Nothing
End If
Dim n = SplitFrom - 1
' Split the source document into two based on the split-at page value.
For i As Integer = 0 To n - 1
doc1.ImportPage(ldoc, i)
Next i
For j As Integer = n To ldoc.Pages.Count - 1
doc2.ImportPage(ldoc, j)
Next j
Dim name = OutputDir & "\" & Path.GetFileNameWithoutExtension(SplitDoc) & "_" & Guid.NewGuid().ToString()
Dim doc1FileName As String = name & "_Doc1.pdf"
Dim doc2FileName As String = name & "_Doc2.pdf"
'Save pdf document1
doc1.Save(doc1FileName)
'Save pdf document2
doc2.Save(doc2FileName)
doc1.Close()
doc2.Close()
#If WEB Then
Dim outputFile = name & ".zip"
Using zip As System.IO.Compression.ZipStorer = System.IO.Compression.ZipStorer.Create(outputFile, "")
zip.AddFile(System.IO.Compression.ZipStorer.Compression.Store, doc1FileName, Path.GetFileName(doc1FileName), "")
zip.AddFile(System.IO.Compression.ZipStorer.Compression.Store, doc2FileName, Path.GetFileName(doc2FileName), "")
End Using
Return outputFile
#Else
Return OutputDir
#End If
End Using
End Function
Public Overrides ReadOnly Property ActionTitle() As String
Get
Return "Split Document"
End Get
End Property
#If WEB Then
Private Sub ProcessForm()
SplitDoc = GetPostFile("SourcePdf")
End Sub
#End If
End Class
End Namespace