PDF for WPF and Silverlight Overview > Features: PDF for WPF and Silverlight > Adding Bookmarks to a PDF Document |
When you open a PDF document using Adobe's Acrobat Reader application, you will notice that most long documents contain an outline structure that is displayed on a pane on the left of the reader. The outline makes it easy to browse through a document's structure and find specific topics. The picture below shows a PDF document with an outline:
The outline entries are called Bookmarks, and you can add them to your PDF for WPF documents using the C1PdfDocument.AddBookmark method. The C1PdfDocument.AddBookmark method takes three parameters: the title of the outline entry, the outline level, and the 'y' position of the entry on the current page (measured in points from the top of the page).
For example, the routine below adds a paragraph to a document and optionally marks it as a level-zero outline entry:
VB |
Copy Code
|
---|---|
Private Function RenderParagraph(text As String, font As Font, rect As Rect, rectPage As Rect, outline As Boolean) As Rect ' If it doesn't fit on this page, add a page break rect.Height = pdf.MeasureString(text, font, rect.Width).Height If rect.Bottom > rectPage.Bottom Then pdf.NewPage() rect.Y = rectPage.Top End If ' Draw the string pdf.DrawString(text, font, Colors.Black, rect) ' Add headings to outline. If outline Then DrawLine(Pens.Black, rect.X, rect.Y, rect.Right, rect.Y) AddBookmark(text, 0, rect.Y) End If ' Update rectangle for next time rect.Offset(0, rect.Height) Return rect End Function |
Example Title |
Copy Code
|
---|---|
private Rect RenderParagraph(string text, Font font, Rect rect, Rect rectPage, bool outline) { // If it doesn't fit on this page, add a page break rect = new Rect(72, 72, 100, 50); rect.Height = pdf.MeasureString(text, font, rect.Width).Height; if (rect.Bottom > rectPage.Bottom) { pdf.NewPage(); rect.Y = rectPage.Top; } // Draw the string pdf.DrawString(text, font, Colors.Black, rect); // Add headings to outline if (outline) { pdf.DrawLine(System.Windows.Media.Colors.Black, rect.X, rect.Y, rect.Right, rect.Y); pdf.AddBookmark(text, 0, rect.Y); } // Update rectangle for next time rect.Offset(0, rect.Height); return rect; } |
You can also use more overloads for AddBookmark() method - such as to pass the Boolean arguments; to specify whether the children of bookmark are initially visible; or to pass the target name of the document. |