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, Outlines and Bookmarks, for more information on creating 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.

Do not be confused by the Document class's Outlines property, which is an OutlineList containing Outline instances. A PdfDocument instance's Outlines property comprises 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.

Reading Outlines 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.

PdfDocument pdfDocument = new PdfDocument(pdfFilePath);
for (int i = 0; i < pdfDocument.Outlines.Count; i++)
{
    Console.WriteLine(pdfDocument.Outlines[i].Text + ": " + pdfDocument.Outlines[i].TargetPageNumber);
}
Console.ReadLine();        
Dim MyPdfDocument As New PdfDocument(pdfFilePath) 
For i As Integer = 0 To MyPdfDocument.Outlines.Count - 1 
    Console.WriteLine(MyPdfDocument.Outlines(i).Text + ": " + MyPdfDocument.Outlines(i).TargetPageNumber) 
Next 
Console.ReadLine()

Reading Bookmarks Example

Read bookmarks the same way as reading an outline. For example, the following example loads a PDF that contains bookmarks and then prints the bookmarks to the console.

Figure 1. An example illustrating bookmarks.

PdfDocument pdfDocument = new PdfDocument("bookmarksexisting.pdf");

for (int i = 0; i < pdfDocument.Outlines.Count; i++)
{
  PdfOutline outline = pdfDocument.Outlines[i];
  if(outline.ChildOutlines.Count > 0)
  {
      for(int j = 0; j < outline.ChildOutlines.Count; j++)
      {
          Console.WriteLine(outline.ChildOutlines[j].Text + ":" + outline.ChildOutlines[j].TargetPageNumber);
      }
  }

  Console.WriteLine(outline.Text + ": " + pdfDocument.Outlines[i].TargetPageNumber);
 }

//prints
// Bookmark to page 1:1
// Bookmark to page 2:2
// Bookmark to page 3:3
// Parent Outline: -1
// Top level bookmark to page 1: 1

Adding Outlines Example

Although the documentation refers to the Outlines and Bookmarks documentation for more information on creating bookmarks and outlines, the following is an example of adding an outline to a PDF created using the MergeDocument child class of Document. Note that it contains both code relevant to PdfOutline and Outline. The code first obtains the PdfOutline instance from the PdfDocument and adds it to the MergeDocument instance. The MergeDocument instance then creates a new Outline and adds it to the existing outline. The result is a new PDF with both outlines.

Figure 2. An example that adds outlines to a PDF.

// get an existing PDF and get the first PdfOutline then write to console
PdfDocument pdfDocument = new PdfDocument("preexisting.pdf");
PdfOutline pdfOutline = pdfDocument.Outlines[0];
Console.WriteLine(pdfOutline.Text);

//create a new mergedocument from PdfDocument, add a new Outline
//then save resultant PDF
MergeDocument document = new MergeDocument(pdfDocument);
Outline outline1 = document.Outlines.Add("ChildA");
Outline outline1A = outline1.ChildOutlines.Add("ChildA 1");
document.Draw(@"output.pdf");

Adding Bookmarks Example

Adding bookmarks to an existing PDF and then saving is the same as creating a PDF from scratch; only the MergeDocument class is used rather than the Document class. Of course, the MergeDocument class is a child class of Document, so in reality you are using the inherited methods from the parent Document class. The following example illustrates adding bookmarks. Note that when adding a bookmark, you are adding it to a Page instance's Elements property.

Refer to the Outlines and Bookmarks documentation for more information.

PdfDocument pdfDocument = new PdfDocument("preexisting.pdf");
PdfOutline pdfOutline = pdfDocument.Outlines[0];
Console.WriteLine(pdfOutline.Text);

//create a new mergedocument from PdfDocument, add a new Outline
//then save resultant PDF
MergeDocument document = new MergeDocument(pdfDocument);
Page page1 = new Page(PageSize.Letter);
Outline parentOutline = document.Outlines.Add("Parent Outline");
page1.Elements.Add(new Bookmark("Top level bookmark to page 1", 0, 0));
page1.Elements.Add(new Bookmark("Bookmark to page 1", 0, 0, parentOutline));
document.Pages.Add(page1);
document.Draw(@"preexisting3.pdf");

In this topic