Reading Outlines and Bookmarks
A PDF's outline and bookmarks are available when merging or appending PDF documents. You can read the existing outlines and bookmarks as you merge existing PDFs and create or modify outlines and bookmarks to the merged document to be outputted.
Refer to the topic, Modifying Outlines and Bookmarks, for more information on modifying outlines and bookmarks.
PdfDocument.Outlines
Recall that when merging one or more PDFs, the starting point is loading the existing PDF into a PdfDocument instance. That instance's outline is retrieved directly using its Outlines property. The Outlines property is a PdfOutlineList containing PdfOutline instances.
PdfOutline
A PdfOutline, although similar to an Outline, is a distinct class with different properties. A PdfOutline class's properties are read-only.
Properties
Property | Description |
---|---|
Action | Gets the action of the imported outline. |
ChildOutlines | Gets a list of imported child outlines. |
Color | Gets the color of the imported outline. |
Expanded | Gets a value indicating if the child outline is expanded by default. |
Style | Gets the text style of the imported outline. |
TargetPageNumber | Gets the target page number of the imported outlines action. |
Text | Get the text of the imported outline. |
Use the PdfOutline class to read an existing outline.
Reading Outlines/Bookmarks Example
The following example demonstrates how to read the text and a page number from an existing collection of PdfOutline instances in a PdfDocument instance.
public static void ReadOutline()
{
PdfDocument pdfDocument = new PdfDocument("outline-example.pdf");
for (int i = 0; i < pdfDocument.Outlines.Count; i++) {
PdfOutline outline = pdfDocument.Outlines[i];
PrintOutline(outline);
}
}
private static void PrintOutline(PdfOutline outline)
{
Console.WriteLine(outline.Text + ": " + outline.TargetPageNumber);
if (outline.ChildOutlines.Count > 0) {
for (int j = 0; j < outline.ChildOutlines.Count; j++){
PrintOutline(outline.ChildOutlines[j]);
}
}
}
Public Shared Sub ReadOutline()
Dim pdfDocument As New
PdfDocument("outline-example.pdf")
For i As Integer = 0 To pdfDocument.Outlines.Count - 1
Dim outline As PdfOutline = pdfDocument.Outlines(i)
PrintOutline(outline)
Next
End Sub
Private Shared Sub PrintOutline(ByVal outline As PdfOutline)
Console.WriteLine(outline.Text & ": " & outline.TargetPageNumber)
If outline.ChildOutlines.Count > 0 Then
For j As Integer = 0 To outline.ChildOutlines.Count - 1
PrintOutline(outline.ChildOutlines(j))
Next
End If
End Sub
Running the example prints the following to the console.
Outline1: 1
Outline1A: 2
Outline1A1: 2
Outline1A2: 2
Outline1B: 2
Outline2: 3
Outline2A: -1
Figure 1. Reading an outline.