Articles in this section
Category / Section

How to split PDF WinForms level of bookmarks using C# and VB.NET?

5 mins read
Syncfusion Essential PDF is a .NET PDF Library used to create, read, and edit the PDF documents. Using this library, you can split the PDF based on bookmarks using C# and VB.NET.

Steps to split the PDF based on bookmarks programmatically:

  1. Create a new C# console application project. creation of console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org.

  3. NuGet package reference
  4. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

 

  1. Use the following code snippet to create the PDF and draw table.

C#

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Sample.pdf");
Dictionary<PdfPageBase, int> pages = new Dictionary<PdfPageBase, int>();
//Maintain collection for page and its index
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
PdfPageBase page = loadedDocument.Pages[i] as PdfPageBase;
pages.Add(page, i);
}
//Get all the bookmarks
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
//Create dictionary with bookmark and split up page range
Dictionary<string, List<int>> splitRange = new Dictionary<string, List<int>>();
for (int i = 0; i < bookmarks.Count; i++)
{
if (bookmarks[i].Destination != null)
{
PdfPageBase page = bookmarks[i].Destination.Page;
 
if (pages.ContainsKey(page))
{
int startIndex = pages[page];
int endIndex = startIndex;
if (i + 1 < bookmarks.Count && bookmarks[i + 1].Destination != null)
{
page = bookmarks[i + 1].Destination.Page;
if (pages[page] - 1 > startIndex)
endIndex = pages[page] - 1;
}
else
{
foreach (string index in splitRange.Keys)
{
int checkStartIndex = splitRange[index][0];
int checkEndIndex = splitRange[index][1];
if (endIndex > checkStartIndex && endIndex < checkEndIndex)
{
endIndex = checkEndIndex;
break;
}
else
endIndex = loadedDocument.Pages.Count - 1;
}
}
 
List<int> range = new List<int>();
range.Add(startIndex);
range.Add(endIndex);
 
splitRange.Add(bookmarks[i].Title, range);
}
}
}
//Split the PDF based on range of bookmark
foreach (string index in splitRange.Keys)
{
int startIndex = splitRange[index][0];
int endIndex = splitRange[index][1];
 
foreach (string key in splitRange.Keys)
{
if (splitRange[key][0] > startIndex && splitRange[key][1] <= endIndex)
{
splitRange[index][1] = splitRange[key][0] - 1; ;
}
}
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Import the pages to the new PDF document
document.ImportPageRange(loadedDocument, startIndex, endIndex);
//Save the document in a specified folder
document.Save(DataPathOutput+"Output_" + index + ".pdf");
//Close the document
document.Close(true);
}
loadedDocument.Close(true);
splitRange.Clear();
pages.Clear();
//Opens the folder where the output documents are saved
System.Diagnostics.Process.Start(DataPathOutput);

 

VB.NET

'Load the PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(( "Sample.pdf"))
Dim pages As Dictionary(Of PdfPageBase, Integer) = New Dictionary(Of PdfPageBase, Integer)
'Maintain collection for page and its index
Dim j As Integer = 0
Do While (j < loadedDocument.Pages.Count)
Dim page As PdfPageBase = CType(loadedDocument.Pages(j), PdfPageBase)
pages.Add(page, j)
j = (j + 1)
Loop
Dim bookmarks As PdfBookmarkBase = loadedDocument.Bookmarks
Dim splitRange As Dictionary(Of String, List(Of Int32)) = New Dictionary(Of String, List(Of Int32))
Dim i As Integer = 0
Do While (i < bookmarks.Count)
If (Not (bookmarks(i).Destination) Is Nothing) Then
Dim page As PdfPageBase = bookmarks(i).Destination.Page
If pages.ContainsKey(page) Then
Dim startIndex As Integer = pages(page)
Dim endIndex As Integer = startIndex
If ((i + 1 < (bookmarks.Count)) _
AndAlso (Not (bookmarks((i + 1)).Destination) Is Nothing)) Then
page = bookmarks((i + 1)).Destination.Page
If ((pages(page) - 1) _
> startIndex) Then
endIndex = (pages(page) - 1)
End If
 
Else
For Each index As String In splitRange.Keys
Dim checkStartIndex As Integer = splitRange(index)(0)
Dim checkEndIndex As Integer = splitRange(index)(1)
If ((endIndex > checkStartIndex) _
AndAlso (endIndex < checkEndIndex)) Then
endIndex = checkEndIndex
Exit For
Else
endIndex = (loadedDocument.Pages.Count - 1)
End If
 
Next
End If
 
Dim range As List(Of Integer) = New List(Of Integer)
range.Add(startIndex)
range.Add(endIndex)
splitRange.Add(bookmarks(i).Title, range)
End If
 
End If
 
i = (i + 1)
Loop
 
'Split the PDF based on range of bookmark
For Each index As String In splitRange.Keys
Dim startIndex As Integer = splitRange(index)(0)
Dim endIndex As Integer = splitRange(index)(1)
For Each key As String In splitRange.Keys
If ((splitRange(key)(0) > startIndex) _
AndAlso (splitRange(key)(1) <= endIndex)) Then
splitRange(index)(1) = (splitRange(key)(0) - 1)
 
End If
 
Next
'Create a new PDF document
Dim document As PdfDocument = New PdfDocument
'Import the pages to the new PDF document
document.ImportPageRange(loadedDocument, startIndex, endIndex)
'Save the document in a specified folder
document.Save((DataPathOutput + ("Output_" _
+ (index + ".pdf"))))
'Close the document
document.Close(True)
Next
loadedDocument.Close(True)
splitRange.Clear()
pages.Clear()
'Opens the folder where the output documents are saved
System.Diagnostics.Process.Start(DataPathOutput)

 

A complete working sample can be downloaded from PdfBookmarkSplit.zip

By executing the program, you will get the split PDF documents saved individually as shown in output folder screenshot below. Split PDF documents saved in output folder

Take a moment to peruse the documentation for using bookmarks in  PDF document and working with PDF pages where you will find various options for importing page from other documents, rearranging pages, removing pages, rotating, and splitting PDF document.

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.



Conclusion
I hope you enjoyed learning on how to split PDF WinForms level of bookmarks using C# and VB.NET.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied