How to Split PDF WinForms Level of Bookmarks Using C# and VB.NET?
Steps to split the PDF based on bookmarks programmatically:
- Create a new C# console application project.
- Include the following namespaces in the Program.cs file.
Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org.
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
- 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.
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.