Bookmarks in PDF are navigational links to content/points of interest within the document. PDF bookmarks provide users with a means of quickly navigating the document. They give users a powerful way to make PDF files more user-friendly, especially for on-screen reading. Bookmarks can automatically take readers to a specific page and they are used in PDFs as an interactive index that can instantly transport the reader to certain pages, sections, individual elements within the documents, and even external documents or media files. PDF bookmarks are especially helpful for new chapter pages and for new sections within a chapter. That’s why people say that any PDF document over 9 pages should have bookmarks included.
How to Use C# Add Bookmarks in PDF?
This article will describe the process of inserting bookmarks in PDF document using .NET PDF component Spire.PDF via C#. Spire.PDF can help us, especially PDF developers easily use C# insert bookmarks in PDF. Make sure Spire.PDF and Visual Studio are correctly installed on system and follow the simple instructions below to insert PDF bookmarks.
Step 1 Create Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Bookmarks;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using Spire.Pdf.Tables;
namespace Bookmark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Create PDF Document
Spire.PDF allows developers create PDF document easily. Here we can use Spire.PDF to create a PDf document and then insert bookmarks into the PDF document.
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
//create section
PdfSection section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = margin;
// Create one page
PdfPageBase page = section.Pages.Add();
float y = 10;
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Sales Report", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
y = y + font1.MeasureString("Sales Report", format1).Height;
y = y + 5;
Step 3 C# Insert PDF Bookmarks
After create a PDF document, we can import some data from database via Spire.PDF. And then, we can insert bookmark at will.
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb";
conn.Open();
OleDbCommand partQueryCommand = PreparePartQueryCommand(conn);
OleDbCommand orderItemQueryCommand = PrepareOrderItemQueryCommand(conn);
DataTable vendors = GetVendors(conn);
for (int i = 0; i < vendors.Rows.Count; i++) { if (i > 0)
{
//next page
page = section.Pages.Add();
y = 0;
}
//draw vendor
String vendorTitle = String.Format("{0}. {1}", i + 1, vendors.Rows[i].ItemArray[1]);
PdfLayoutResult drawVendorLayoutResult = DrawVendor(page, vendors, i, vendorTitle, y);
//insert vendor bookmark
PdfDestination vendorBookmarkDest = new PdfDestination(page, new PointF(0, y));
PdfBookmark vendorBookmark = doc.Bookmarks.Add(vendorTitle);
vendorBookmark.Color = Color.SaddleBrown;
vendorBookmark.DisplayStyle = PdfTextStyle.Bold;
vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);
y = drawVendorLayoutResult.Bounds.Bottom + 5;
page = drawVendorLayoutResult.Page;
//get parts of vendor
DataTable parts = GetParts(partQueryCommand, (double)vendors.Rows[i].ItemArray[0]);
for (int j = 0; j < parts.Rows.Count; j++) { if (j > 0)
{
//next page
page = section.Pages.Add();
y = 0;
}
//draw part
String partTitle = String.Format("{0}.{1}. {2}", i + 1, j + 1, parts.Rows[j].ItemArray[1]);
PdfLayoutResult drawPartLayoutResult = DrawPart(page, parts, j, partTitle, y);
//insert part bookmark
PdfDestination partBookmarkDest = new PdfDestination(page, new PointF(0, y));
PdfBookmark partBookmark = vendorBookmark.Add(partTitle);
partBookmark.Color = Color.Coral;
partBookmark.DisplayStyle = PdfTextStyle.Italic;
partBookmark.Action = new PdfGoToAction(partBookmarkDest);
y = drawPartLayoutResult.Bounds.Bottom + 5;
page = drawPartLayoutResult.Page;
//get order items
String orderItemsTitle = String.Format("{0} - Order Items", parts.Rows[j].ItemArray[1]);
DataTable orderItems = GetOrderItems(orderItemQueryCommand, (double)parts.Rows[j].ItemArray[0]);
DrawOrderItems(page, orderItems, orderItemsTitle, y);
}
}
}
Step 4 Save and Preview
The following code can save the output file as .pdf format and allows us to preview the effect.
//Save pdf file.
doc.SaveToFile("Bookmark.pdf");
doc.Close();
//Launching the Pdf file.
PDFDocumentViewer("Bookmark.pdf");
After finish writing the whole codes, press F5 to start the project and a PDF document will automatically generated. Effective screenshot:
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. Click to learn more…