Skip to content

2.9 Bookmarks Support

OgreTransporter edited this page Mar 2, 2020 · 1 revision

Bookmarks are described in the PDF specification (section 8.2.2 Document Outline) as follows: "A PDF Document may optionally display a document outline on the screen, allowing the user to navigate interactively from one part of the document to another. The outline consists of a tree-structured hierarchy of outline items (sometimes called bookmarks), which serve as a visual table of contents to display the document's structure to the user. The user can interactively open and close individual item by clicking them with the mouse."

The OtherExample.cs source code has an example of bookmarks. At one location there is a hierarchy of three levels. You can see the result in OtherExample.pdf file.

The first step in adding bookmarks to your application is:

// set the program to display bookmarks
// and get the bookmark root object
PdfBookmark BookmarkRoot = Document.GetBookmarksRoot();

This step activates bookmarks in your document and returns the root node.

Adding bookmarks is similar to adding controls to a windows form. The first level bookmarks are added to the root. Subsequent levels are added to existing bookmarks. At minimum you have to define a title, page, vertical position on the page and an open entries flag. Page is the PdfPage object of the page to go to. YPos is the vertical position relative to the bottom left corner of the page. Open entries flag is true if the lower level bookmarks are visible and false if the lower level are hidden. The first level is always visible by default.

// hierarchy example
PdfBookmark FirstLevel_1 = BookmarkRoot.AddBookmark("Chapter 1", Page, YPos, false);
PdfBookmark SecondLevel_11 = FirstLevel_1.AddBookmark("Section 1.1", Page, YPos, false);
PdfBookmark SecondLevel_12 = FirstLevel_1.AddBookmark("Section 1.2", Page, YPos, false);
PdfBookmark ThirdLevel_121 = SecondLevel_12.AddBookmark("Section 1.2.1", Page, YPos, false);
PdfBookmark ThirdLevel_122 = SecondLevel_12.AddBookmark("Section 1.2.2", Page, YPos, false);
PdfBookmark SecondLevel_13 = FirstLevel_1.AddBookmark("Section 1.3", Page, YPos, false);
PdfBookmark FirstLevel_2 = BookmarkRoot.AddBookmark("Chapter 2", Page, YPos, false);
PdfBookmark SecondLevel_21 = FirstLevel_2.AddBookmark("Section 2.1", Page, YPos, false);
PdfBookmark SecondLevel_22 = FirstLevel_2.AddBookmark("Section 2.2", Page, YPos, false);

AddBookmark() method has four overloading variations:

// basic
public PdfBookmark AddBookmark
    (
    String	Title,		// bookmark title
    PdfPage	Page,		// bookmark page
    Double	YPos,		// bookmark vertical position relative to bottom left corner of the page
    Boolean	OpenEntries	// true is display children. false hide children
    )

// title color and style
public PdfBookmark AddBookmark
    (
    String		Title,		// bookmark title
    PdfPage		Page,		// bookmark page
    Double		YPos,		// bookmark vertical position relative to bottom left corner of the page
    Color		Paint,		// bookmark color. Coloe.Empty is display title in default color
    TextStyle		TextStyle,	// bookmark text style: normal, bold, italic, bold-italic
    Boolean		OpenEntries	// true is display children. false hide children
    )

// XPos and zoom
public PdfBookmark AddBookmark
    (
    String		Title,		// bookmark title
    PdfPage		Page,		// bookmark page
    Double		XPos,		// bookmark horizontal position relative to bottom left corner of the page
    Double		YPos,		// bookmark vertical position relative to bottom left corner of the page
    Double		Zoom,		// Zoom factor. 1.0 is 100%. 0.0 is ignore zoom.
    Boolean		OpenEntries	// true is display children. false hide children
    )

// all options
public PdfBookmark AddBookmark
    (
    String		Title,		// bookmark title
    PdfPage		Page,		// bookmark page
    Double		XPos,		// bookmark horizontal position relative to bottom left corner of the page
    Double		YPos,		// bookmark vertical position relative to bottom left corner of the page
    Double		Zoom,		// Zoom factor. 1.0 is 100%. 0.0 is ignore zoom.
    Color		Paint,		// bookmark color. Coloe.Empty is display title in default color
    TextStyle		TextStyle,	// bookmark text style: normal, bold, italic, bold-italic
    Boolean		OpenEntries	// true is display children. false hide children
    )

PdfBookmark class exposes one more method GetChild. You can get any bookmark by calling GetChild with one or more integer arguments. Each argument is a zero base argument of the child position in the level. For example GetChild(2) is the third item of the first level. GetChild(2, 3) is the forth second level item of the third first level item.

Clone this wiki locally