Using ComponentOne PDF for .NET > Adding Bookmarks and Annotations > 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 .NET documents using the AddBookmark method. The AddBookmark(String,Int32,Double) 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:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Function RenderParagraph(text As String, font As Font, rect As RectangleF, rectPage As RectangleF, outline As Boolean) As RectangleF ' If it doesn't fit on this page, add a page break. rect.Height = _c1pdf.MeasureString(text, font, rect.Width).Height If rect.Bottom > rectPage.Bottom Then _c1pdf.NewPage() rect.Y = rectPage.Top End If ' Draw the string. _c1pdf.DrawString(text, font, Brushes.Black, rect) ' Add headings to outline. If outline Then _c1pdf.DrawLine(Pens.Black, rect.X, rect.Y, rect.Right, rect.Y) _c1pdf.AddBookmark(text, 0, rect.Y) End If ' Update rectangle for next time. rect.Offset(0, rect.Height) Return rect End Function |
To write code in C#
C# |
Copy Code
|
---|---|
private RectangleF RenderParagraph(string text, Font font, RectangleF rect, RectangleF rectPage, bool outline) { // If it doesn't fit on this page, add a page break. rect.Height = _c1pdf.MeasureString(text, font, rect.Width).Height; if (rect.Bottom > rectPage.Bottom) { _c1pdf.NewPage(); rect.Y = rectPage.Top; } // Draw the string. _c1pdf.DrawString(text, font, Brushes.Black, rect); // Add headings to outline. if (outline) { _c1pdf.DrawLine(Pens.Black, rect.X, rect.Y, rect.Right, rect.Y); _c1pdf.AddBookmark(text, 0, rect.Y); } // Update rectangle for next time. rect.Offset(0, rect.Height); return rect; } |
Note: You can also use more overloads for AddBookmark() method - to pass the boolean to specify whether the children of bookmark are initially visible or to pass the target name of the document. |