Split PDF based on bookmarks

Generate PDF
5/22/2014

Downloads

This code sample shows how to split a PDF document into multiple documents. The splitting is based on the bookmarks in the original PDF document.

The following code will go through all bookmarks in the PDF document and make copies of the bookmarked sections. These copies will be saved as seperate PDF documents. This shows you how you can work with the Bookmark class to get the information you need.

C# code sample

1 using (FileStream file = new FileStream( 2 @"..\..\..\inputDocuments\AnnualReportToCongress.pdf", 3 FileMode.Open, FileAccess.Read)) 4 { 5 Document document = new Document(file); 6 7 BookmarkCollection bookmarks = document.Bookmarks; 8 for (int index = 0, fromPage = 0; index < bookmarks.Count; index++) 9 { 10 // determine last page of next part 11 int toPage = -1; 12 if (index == bookmarks.Count - 1) 13 { 14 // last bookmark - so just append all remaining pages 15 toPage = document.Pages.Count; 16 } 17 else 18 { 19 // not the last bookmark - append up to the page where 20 // the next bookmark points to 21 GoToAction action = bookmarks[index + 1].Actions[0] as GoToAction; 22 if (null != action) 23 { 24 InternalDestination destination = action.Destination as InternalDestination; 25 if (null != destination) 26 { 27 toPage = destination.Page.Index; 28 } 29 } 30 } 31 32 // create a new part 33 if (-1 != toPage) 34 { 35 Document part = new Document(); 36 foreach (Page p in document.Pages) 37 { 38 part.Pages.Add(p.Clone()); 39 } 40 41 string title = bookmarks[index].Title; 42 using (FileStream fileOut = new FileStream( 43 string.Format(@"..\..\{0}.pdf", title), 44 FileMode.Create, FileAccess.Write)) 45 { 46 Console.WriteLine("Writing {0} - {1} of {2}", title, index, bookmarks.Count); 47 part.Write(fileOut); 48 } 49 50 fromPage = toPage; 51 } 52 } 53 }

VB.NET code sample

1 Using file As New FileStream("..\..\..\inputDocuments\AnnualReportToCongress.pdf", FileMode.Open, FileAccess.Read) 2 Dim document As New Document(file) 3 4 Dim bookmarks As BookmarkCollection = document.Bookmarks 5 Dim index As Integer = 0, fromPage As Integer = 0 6 While index < bookmarks.Count 7 ' determine last page of next part 8 Dim toPage As Integer = -1 9 If index = bookmarks.Count - 1 Then 10 ' last bookmark - so just append all remaining pages 11 toPage = document.Pages.Count 12 Else 13 ' not the last bookmark - append up to the page where 14 ' the next bookmark points to 15 Dim action As GoToAction = TryCast(bookmarks(index + 1).Actions(0), GoToAction) 16 If action IsNot Nothing Then 17 Dim destination As InternalDestination = TryCast(action.Destination, InternalDestination) 18 If destination IsNot Nothing Then 19 toPage = destination.Page.Index 20 End If 21 End If 22 End If 23 24 ' create a new part 25 If -1 <> toPage Then 26 Dim part As New Document() 27 For pageIndex As Integer = fromPage To toPage - 1 28 part.Pages.Add(document.Pages(pageIndex).Clone()) 29 Next 30 31 Dim title As String = bookmarks(index).Title 32 Using fileOut As New FileStream(String.Format("..\..\{0}.pdf", title), FileMode.Create, FileAccess.Write) 33 Console.WriteLine("Writing {0} - {1} of {2}", title, index, bookmarks.Count) 34 part.Write(fileOut) 35 End Using 36 37 fromPage = toPage 38 End If 39 index += 1 40 End While 41 End Using