Add PDF Bookmark in C#, VB.NET

PDF bookmark helps us quickly locate and link to points that we want to go. Especially when the PDF has huge pages, we need PDF bookmark more than ever. In this section, I will introduce a solution to add PDF bookmark via Spire.PDF for .NET in C#, VB.NET. Using this PDF component, we can add one level PDF bookmark as well as multi-level PDF bookmark. Besides, bookmark format such as font and color also can be set. Now let us first view the effect of PDF bookmark which is added by Spire.PDF for .NET as below picture:

Add PDF Bookmark

Form above picture, we can view that the bookmark in PDF has multiple levels. Also the bookmark font and color of the first level and second level are different. In below solution, first I connect with a database table called "vendors". In this datatable, there are three levels: the first level is "Vendor", the second is "Part" and the third is "Order". After connect with database, we need to prepare two commands of the datatables: "Part" and "Order" by this class System.Data.OleDb.OleDbCommand. After getting all the data of three-level datatable "vendors", we can add bookmark in PDF by a class provided by Spire.PDF: Spire.Pdf.Bookmarks.PdfBookmark. Now please download Spire.PDF for .NET. After adding Spire.Pdf dll on system, we can start our PDF bookmark task as below key code:

using (OleDbConnection conn = new OleDbConnection())
                conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb";
                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);

                    //add vendor bookmark
                    PdfDestination vendorBookmarkDest = new PdfDestination(page, new PointF(0, y));
                    PdfBookmark vendorBookmark = doc.Bookmarks.Add(vendorTitle);
                    vendorBookmark.Color = Color.SeaGreen;
                    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);

                        //add part bookmark
                        PdfDestination partBookmarkDest = new PdfDestination(page, new PointF(0, y));
                        PdfBookmark partBookmark = vendorBookmark.Add(partTitle);
                        partBookmark.Color = Color.Firebrick;
                        partBookmark.DisplayStyle = PdfTextStyle.Regular;
                        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);
Using conn As New OleDbConnection()
	conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb"
	Dim partQueryCommand As OleDbCommand = PreparePartQueryCommand(conn)
	Dim orderItemQueryCommand As OleDbCommand = PrepareOrderItemQueryCommand(conn)
	Dim vendors As DataTable = GetVendors(conn)
	For i As Integer = 0 To vendors.Rows.Count - 1
		If i > 0 Then
			'next page
			page = section.Pages.Add()
			y = 0
		End If
		'draw vendor
		Dim vendorTitle As [String] = [String].Format("{0}. {1}", i + 1, vendors.Rows(i).ItemArray(1))
		Dim drawVendorLayoutResult As PdfLayoutResult = DrawVendor(page, vendors, i, vendorTitle, y)

		'add vendor bookmark
		Dim vendorBookmarkDest As New PdfDestination(page, New PointF(0, y))
		Dim vendorBookmark As PdfBookmark = doc.Bookmarks.Add(vendorTitle)
		vendorBookmark.Color = Color.SeaGreen
		vendorBookmark.DisplayStyle = PdfTextStyle.Bold
		vendorBookmark.Action = New PdfGoToAction(vendorBookmarkDest)
		y = drawVendorLayoutResult.Bounds.Bottom + 5
		page = drawVendorLayoutResult.Page

		'get parts of vendor
		Dim parts As DataTable = GetParts(partQueryCommand, CDbl(vendors.Rows(i).ItemArray(0)))
		For j As Integer = 0 To parts.Rows.Count - 1
			If j > 0 Then
				'next page
				page = section.Pages.Add()
				y = 0
			End If
			'draw part
			Dim partTitle As [String] = [String].Format("{0}.{1}. {2}", i + 1, j + 1, parts.Rows(j).ItemArray(1))
			Dim drawPartLayoutResult As PdfLayoutResult = DrawPart(page, parts, j, partTitle, y)

			'add part bookmark
			Dim partBookmarkDest As New PdfDestination(page, New PointF(0, y))
			Dim partBookmark As PdfBookmark = vendorBookmark.Add(partTitle)
			partBookmark.Color = Color.Firebrick
			partBookmark.DisplayStyle = PdfTextStyle.Regular
			partBookmark.Action = New PdfGoToAction(partBookmarkDest)

			y = drawPartLayoutResult.Bounds.Bottom + 5
			page = drawPartLayoutResult.Page

			'get order items
			Dim orderItemsTitle As [String] = [String].Format("{0} - Order Items", parts.Rows(j).ItemArray(1))
			Dim orderItems As DataTable = GetOrderItems(orderItemQueryCommand, CDbl(parts.Rows(j).ItemArray(0)))
			DrawOrderItems(page, orderItems, orderItemsTitle, y)
End Using

Spire.PDF for .NET is a professional PDF library that enables users to create, edit, read and manipulate PDF files in .NET applications with C#, VB.NET.