How to create TOC from bookmarks in PDF using C# and VB.NET
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create table of contents from bookmarks in a PDF document using C# and VB.NET.
Steps to create TOC from bookmarks in PDF programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Form.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports System.Drawing
- Use the following code snippet to create TOC from bookmarks in PDF document.
C#
PdfDocument document = null; PdfFont font = null; PdfBrush brush = null; float yPos; private void button1_Click(object sender, EventArgs e) { //Initialize a new instance for PdfDocuemnt document = new PdfDocument(); //Set font for TOC and bookmark contents font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f); //Set brush for TOC and bookmark contents brush = new PdfSolidBrush(Color.Black); //Add page for TOC PdfSection SectionTOC = document.Sections.Add(); PdfPage pageTOC = SectionTOC.Pages.Add(); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); pageTOC.Graphics.DrawString("Table Of Contents", font, brush, new RectangleF(PointF.Empty, new SizeF(pageTOC.Graphics.ClientSize.Width, 20)), format); //Draw contents, bookmarks, and table of contents PdfSection SectionContent = document.Sections.Add(); yPos = 30; for (int i = 1; i <= 2; i++) { PdfPage pageContent = SectionContent.Pages.Add(); //Add bookmark in PDF document PdfBookmark bookmark = AddBookmark(pageContent, pageTOC, "Chapter " + i, new PointF(10, 30)); //Add sections to bookmark PdfBookmark section1 = AddSection(bookmark, pageContent, pageTOC, "Section " + i + ".1", new PointF(30, 50), false); //Add subsections to section PdfBookmark subsection1 = AddSection(section1, pageContent, pageTOC, "Paragraph " + i + ".1.1", new PointF(50, 70), true); PdfBookmark subsection2 = AddSection(section1, pageContent, pageTOC, "Paragraph " + i + ".1.2", new PointF(50, 170), true); PdfBookmark subsection3 = AddSection(section1, pageContent, pageTOC, "Paragraph " + i + ".1.3", new PointF(50, 270), true); PdfBookmark section2 = AddSection(bookmark, pageContent, pageTOC, "Section " + i + ".2", new PointF(30, 420), false); PdfBookmark subsection4 = AddSection(section2, pageContent, pageTOC, "Paragraph " + i + ".2.1", new PointF(50, 440), true); PdfBookmark subsection5 = AddSection(section2, pageContent, pageTOC, "Paragraph " + i + ".2.2", new PointF(50, 570), true); PdfBookmark subsection6 = AddSection(section2, pageContent, pageTOC, "Paragraph " + i + ".2.3", new PointF(50, 680), true); } document.Save("TableOfContents.pdf"); document.Close(true); } public PdfBookmark AddBookmark(PdfPage page, PdfPage toc, string title, PointF point) { PdfGraphics graphics = page.Graphics; //Add bookmark in PDF document PdfBookmark bookmarks = document.Bookmarks.Add(title); //Draw the content in the PDF page graphics.DrawString(title, font, brush, new PointF(point.X, point.Y)); //Add table of contents AddTableOfcontents(page, toc, title, point); //Adding bookmark with named destination PdfNamedDestination namedDestination = new PdfNamedDestination(title); namedDestination.Destination = new PdfDestination(page, new PointF(point.X, point.Y)); namedDestination.Destination.Mode = PdfDestinationMode.FitToPage; document.NamedDestinationCollection.Add(namedDestination); bookmarks.NamedDestination = namedDestination; return bookmarks; } public PdfBookmark AddSection(PdfBookmark bookmark, PdfPage page, PdfPage toc, string title, PointF point, bool isSubSection) { PdfGraphics graphics = page.Graphics; //Add bookmark in PDF document PdfBookmark bookmarks = bookmark.Add(title); //Draw the content in the PDF page graphics.DrawString(title, font, brush, new PointF(point.X, point.Y)); //Add table of contents AddTableOfcontents(page, toc, title, point); //Adding bookmark with named destination PdfNamedDestination namedDestination = new PdfNamedDestination(title); namedDestination.Destination = new PdfDestination(page, new PointF(point.X, point.Y)); if (isSubSection == true) namedDestination.Destination.Zoom = 2f; else namedDestination.Destination.Zoom = 1f; document.NamedDestinationCollection.Add(namedDestination); bookmarks.NamedDestination = namedDestination; return bookmarks; } public void AddTableOfcontents(PdfPage page, PdfPage toc, string title, PointF point) { //Draw title in TOC PdfTextElement element = new PdfTextElement(title, font, PdfBrushes.Blue); //Set layout format for pagination of TOC PdfLayoutFormat format = new PdfLayoutFormat(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate; PdfLayoutResult result = element.Draw(toc, new PointF(point.X, yPos), format); //Draw page number in TOC PdfTextElement pageNumber = new PdfTextElement(document.Pages.IndexOf(page).ToString(), font, brush); pageNumber.Draw(toc, new PointF(toc.Graphics.ClientSize.Width - 40, yPos)); //Creates a new document link annotation RectangleF bounds = result.Bounds; bounds.Width = toc.Graphics.ClientSize.Width - point.X; PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(bounds); documentLinkAnnotation.AnnotationFlags = PdfAnnotationFlags.NoRotate; documentLinkAnnotation.Text = title; documentLinkAnnotation.Color = Color.Transparent; //Sets the destination documentLinkAnnotation.Destination = new PdfDestination(page); documentLinkAnnotation.Destination.Location = point; //Adds this annotation to a new page toc.Annotations.Add(documentLinkAnnotation); if (toc != result.Page) { yPos = result.Bounds.Height + 5; } else { yPos += result.Bounds.Height + 5; } toc = result.Page; }
VB.NET
Private document As PdfDocument = Nothing Private font As PdfFont = Nothing Private brush As PdfBrush = Nothing Private yPos As Single Sub Main() 'Initialize a new instance for PdfDocuemnt document = New PdfDocument() 'Set font for TOC and bookmark contents font = New PdfStandardFont(PdfFontFamily.Helvetica, 10.0F) 'Set brush for TOC and bookmark contents brush = New PdfSolidBrush(Color.Black) 'Add page for TOC Dim SectionTOC As PdfSection = document.Sections.Add() Dim pageTOC As PdfPage = SectionTOC.Pages.Add() Dim format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle) pageTOC.Graphics.DrawString("Table Of Contents", font, brush, New RectangleF(PointF.Empty, New SizeF(pageTOC.Graphics.ClientSize.Width, 20)), format) Dim SectionContent As PdfSection = document.Sections.Add() yPos = 30 For i As Integer = 1 To 2 Dim pageContent As PdfPage = SectionContent.Pages.Add() 'Add bookmark in PDF document Dim bookmark As PdfBookmark = AddBookmark(pageContent, pageTOC, "Chapter " & i, New PointF(10, 30)) 'Add sections to bookmark Dim section1 As PdfBookmark = AddSection(bookmark, pageContent, pageTOC, "Section " & i & ".1", New PointF(30, 50), False) 'Add subsections to section Dim subsection1 As PdfBookmark = AddSection(section1, pageContent, pageTOC, "Paragraph " & i & ".1.1", New PointF(50, 70), True) Dim subsection2 As PdfBookmark = AddSection(section1, pageContent, pageTOC, "Paragraph " & i & ".1.2", New PointF(50, 170), True) Dim subsection3 As PdfBookmark = AddSection(section1, pageContent, pageTOC, "Paragraph " & i & ".1.3", New PointF(50, 270), True) Dim section2 As PdfBookmark = AddSection(bookmark, pageContent, pageTOC, "Section " & i & ".2", New PointF(30, 420), False) Dim subsection4 As PdfBookmark = AddSection(section2, pageContent, pageTOC, "Paragraph " & i & ".2.1", New PointF(50, 440), True) Dim subsection5 As PdfBookmark = AddSection(section2, pageContent, pageTOC, "Paragraph " & i & ".2.2", New PointF(50, 570), True) Dim subsection6 As PdfBookmark = AddSection(section2, pageContent, pageTOC, "Paragraph " & i & ".2.3", New PointF(50, 680), True) Next 'Save and Close document document.Save("TableOfContents.pdf") document.Close(True) End Sub Public Function AddBookmark(ByVal page As PdfPage, ByVal toc As PdfPage, ByVal title As String, ByVal point As PointF) As PdfBookmark Dim graphics As PdfGraphics = page.Graphics 'Add bookmark in PDF document Dim bookmarks As PdfBookmark = document.Bookmarks.Add(title) 'Draw the content in the PDF page graphics.DrawString(title, font, brush, New PointF(point.X, point.Y)) 'Add table of contents AddTableOfcontents(page, toc, title, point) 'Adding bookmark with named destination Dim namedDestination As PdfNamedDestination = New PdfNamedDestination(title) namedDestination.Destination = New PdfDestination(page, New PointF(point.X, point.Y)) namedDestination.Destination.Mode = PdfDestinationMode.FitToPage document.NamedDestinationCollection.Add(namedDestination) bookmarks.NamedDestination = namedDestination Return bookmarks End Function Public Function AddSection(ByVal bookmark As PdfBookmark, ByVal page As PdfPage, ByVal toc As PdfPage, ByVal title As String, ByVal point As PointF, ByVal isSubSection As Boolean) As PdfBookmark Dim graphics As PdfGraphics = page.Graphics 'Add bookmark in PDF document Dim bookmarks As PdfBookmark = bookmark.Add(title) 'Draw the content in the PDF page graphics.DrawString(title, font, brush, New PointF(point.X, point.Y)) 'Add table of contents AddTableOfcontents(page, toc, title, point) 'Adding bookmark with named destination Dim namedDestination As PdfNamedDestination = New PdfNamedDestination(title) namedDestination.Destination = New PdfDestination(page, New PointF(point.X, point.Y)) If isSubSection = True Then namedDestination.Destination.Zoom = 2.0F Else namedDestination.Destination.Zoom = 1.0F End If document.NamedDestinationCollection.Add(namedDestination) bookmarks.NamedDestination = namedDestination Return bookmarks End Function Public Sub AddTableOfcontents(ByVal page As PdfPage, ByVal toc As PdfPage, ByVal title As String, ByVal point As PointF) 'Draw title in TOC Dim element As PdfTextElement = New PdfTextElement(title, font, PdfBrushes.Blue) 'Set layout format for pagination of TOC Dim format As PdfLayoutFormat = New PdfLayoutFormat() format.Break = PdfLayoutBreakType.FitPage format.Layout = PdfLayoutType.Paginate Dim result As PdfLayoutResult = element.Draw(toc, New PointF(point.X, yPos), format) 'Draw page number in TOC Dim pageNumber As PdfTextElement = New PdfTextElement(document.Pages.IndexOf(page).ToString(), font, brush) pageNumber.Draw(toc, New PointF(toc.Graphics.ClientSize.Width - 40, yPos)) 'Creates a new document link annotation Dim bounds As RectangleF = result.Bounds bounds.Width = toc.Graphics.ClientSize.Width - point.X Dim documentLinkAnnotation As PdfDocumentLinkAnnotation = New PdfDocumentLinkAnnotation(bounds) documentLinkAnnotation.AnnotationFlags = PdfAnnotationFlags.NoRotate documentLinkAnnotation.Text = title documentLinkAnnotation.Color = Color.Transparent 'Sets the destination documentLinkAnnotation.Destination = New PdfDestination(page) documentLinkAnnotation.Destination.Location = point 'Adds this annotation to a new page toc.Annotations.Add(documentLinkAnnotation) If toc Is result.Page Then yPos += result.Bounds.Height + 5 Else yPos = result.Bounds.Height + 5 End If toc = result.Page End Sub
A complete working sample can be downloaded from CreateTOCFromBookmarks.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with bookmarks and working with named destination, where you can find other options like insert, remove, and modify bookmarks and named destinations from the PDF document.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to add bookmarks.
An online sample link to add named destinations.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.