A bookmark is similar to a hyperlink which is used to jump to any location or topic in a document. When you open a PDF document using Adobe's Acrobat Reader application, you might have noticed that most of the long documents contain an outline structure that is displayed in the left pane. This outline structure makes it easy to browse through a document's structure and find specific topics. The outline entries in this structure are called the bookmarks which can be used to organize and display the document structure to the user so that the they can interactively navigate to a particular topic or a location in a document.
The following GIF shows a PDF document with a bookmark which when clicked navigates you to a specific location in the document:
PDF for .NET allows you to add bookmarks in a Pdf document using the AddBookmark method. This method takes following three parameters:
The AddBookmark method also has other overloads which can be used based on your requirements.
To add a bookmark in a Pdf document, use the following code. In this example, we create a Pdf document which has a page by default, add a second page in the document and add text on both the pages. Then, we add a bookmark using the AddBookmark method and link it to the text in the second page.
C# |
Copy Code
|
---|---|
C1PdfDocument _c1pdf = new C1PdfDocument(); private void Form1_Load(object sender, EventArgs e) { //sample text for first page _c1pdf.DrawString("This is a sample Text", this.Font, new SolidBrush(Color.Black), new PointF(50, 50)); //add new page _c1pdf.NewPage(); //adding bookmark Text to second Page RenderParagraph("This is a bookmark", this.Font, new Rectangle(50, 50, 200, 200), _c1pdf.PageRectangle, true); //save the file _c1pdf.Save("Bookmark.pdf"); } 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 a bookmark if (outline) { _c1pdf.AddBookmark(text, 0, rect.Y); } // Update rectangle for next time. rect.Offset(0, rect.Height); return rect; } |