Articles in this section
Category / Section

How to create TOC when merging PDF documents using C# and VB.NET?

5 mins read

The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create table of content when merging PDF documents using C# and VB.NET.

Steps to create TOC when merging PDF documents programmatically:

  1. Create a new C# Windows Forms application project. Create new Windows Forms application in .NET PDF library
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. Install nuget packages in .NET PDF library
  3. Include the following namespaces in Form.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using System.Drawing;
using System.IO;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Interactive
Imports System.Drawing
Imports System.IO

 

  1. Use the following code snippet to create TOC when merging PDF documents.

C#

PdfDocument document;
PdfFont font;
private void button1_Click(object sender, EventArgs e)
{
    //File streams to merge
    Stream stream1 = new FileStream("F# Succinctly.pdf", FileMode.Open, FileAccess.Read);
    Stream stream2 = new FileStream("HTTP Succinctly.pdf", FileMode.Open, FileAccess.Read);
    Stream stream3 = new FileStream("Windows Store Apps Succinctly.pdf", FileMode.Open, FileAccess.Read);
    //Load the documents as streams
    PdfLoadedDocument doc1 = new PdfLoadedDocument(stream1);
    PdfLoadedDocument doc2 = new PdfLoadedDocument(stream2);
    PdfLoadedDocument doc3 = new PdfLoadedDocument(stream3);
    object[] dobj = { doc1, doc2, doc3 };
    //Initialize a new instance of PDF document
    document = new PdfDocument();
    //Set page size
    document.PageSettings.Size = doc1.Pages[0].Graphics.Size;
    //Add a new PDF page
    PdfPage page = document.Pages.Add();
    //Set font for TOC and bookmark contents
    font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f);
    //Create a new instance of string format to set text layout information
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
    page.Graphics.DrawString("Table Of Contents", font, PdfBrushes.Black, new RectangleF(PointF.Empty, new SizeF(page.Graphics.ClientSize.Width, 20)), format);
    //Merge PDF documents
    PdfDocument.Merge(document, dobj);
    //Draw table of content 
    AddBookmark(document.Pages[1], page, "F# Succinctly", new PointF(10, 30));
    AddBookmark(document.Pages[doc1.Pages.Count + 1], page, "HTTP Succinctly", new PointF(10, 45));
    AddBookmark(document.Pages[doc1.Pages.Count + doc2.Pages.Count + 1], page, "Windows Store Apps Succinctly", new PointF(10, 60));
    //Save and close PDF document
    document.Save("TableOfContent.pdf");
    document.Close(true);
}
 
//Add bookmarks and table of content in newly added page
private void AddBookmark(PdfPage page, PdfPage toc, string content, PointF point)
{
    PdfGraphics graphics = page.Graphics;
    //Add bookmark in PDF document
    PdfBookmark bookmarks = document.Bookmarks.Add(content);
    //Add table of contents
    AddTableOfContent(page, toc, content, point);
    //Adding bookmark with named destination
    PdfNamedDestination namedDestination = new PdfNamedDestination(content);
    namedDestination.Destination = new PdfDestination(page, new PointF(point.X, point.Y));
    namedDestination.Destination.Mode = PdfDestinationMode.FitToPage;
    document.NamedDestinationCollection.Add(namedDestination);
    bookmarks.NamedDestination = namedDestination;
}
 
//Add table of content with page number and document link annotations
private void AddTableOfContent(PdfPage page, PdfPage toc, string content, PointF point)
{
    //Draw title in TOC
    PdfTextElement element = new PdfTextElement(content, 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, point, format);
    //Draw page number in TOC
    PdfTextElement pageNumber = new PdfTextElement(document.Pages.IndexOf(page).ToString(), font, PdfBrushes.Black);
    pageNumber.Draw(toc, new PointF(toc.Graphics.ClientSize.Width - 40, point.Y));
    //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 = content;
    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);
}

 

VB.NET

Dim document As PdfDocument
Dim font As PdfFont
Sub Main()
    'File streams to merge
    Dim stream1 As Stream = New FileStream("..\..\Data\F# Succinctly.pdf", FileMode.Open, FileAccess.Read)
    Dim stream2 As Stream = New FileStream("..\..\Data\HTTP Succinctly.pdf", FileMode.Open, FileAccess.Read)
    Dim stream3 As Stream = New FileStream("..\..\Data\Windows Store Apps Succinctly.pdf", FileMode.Open, FileAccess.Read)
    'Load the documents as streams
    Dim doc1 As PdfLoadedDocument = New PdfLoadedDocument(stream1)
    Dim doc2 As PdfLoadedDocument = New PdfLoadedDocument(stream2)
    Dim doc3 As PdfLoadedDocument = New PdfLoadedDocument(stream3)
    Dim dobj As Object() = {doc1, doc2, doc3}
    'Initialize a New instance of PDF document
    document = New PdfDocument()
    'Set page size
    document.PageSettings.Size = doc1.Pages.Item(0).Graphics.Size
    'Add a New PDF page
    Dim Page As PdfPage = document.Pages.Add()
    'Set font for TOC And bookmark contents
    font = New PdfStandardFont(PdfFontFamily.Helvetica, 10.0F)
    'Create a New instance of string format to set text layout information
    Dim Format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)
    Page.Graphics.DrawString("Table Of Contents", font, PdfBrushes.Black, New RectangleF(PointF.Empty, New SizeF(Page.Graphics.ClientSize.Width, 20)), Format)
    'Merge PDF documents
    PdfDocument.Merge(document, dobj)
    'Draw table of content 
    AddBookmark(document.Pages.Item(1), Page, "F# Succinctly", New PointF(10, 30))
    AddBookmark(document.Pages.Item(doc1.Pages.Count + 1), Page, "HTTP Succinctly", New PointF(10, 45))
    AddBookmark(document.Pages.Item(doc1.Pages.Count + doc2.Pages.Count + 1), Page, "Windows Store Apps Succinctly", New PointF(10, 60))
    'Save And close PDF document
    document.Save("TableOfContent.pdf")
    document.Close(True)
End Sub
 
'Add bookmarks and table of content in newly added page
Private Sub AddBookmark(ByVal page As PdfPage, ByVal toc As PdfPage, ByVal content As String, ByVal point As PointF)
    Dim graphics As PdfGraphics = page.Graphics
    'Add bookmark in PDF document
    Dim bookmarks As PdfBookmark = document.Bookmarks.Add(content)
    'Add table of contents
    AddTableOfContent(page, toc, content, point)
    'Adding bookmark with named destination
    Dim namedDestination As PdfNamedDestination = New PdfNamedDestination(content)
    namedDestination.Destination = New PdfDestination(page, New PointF(point.X, point.Y))
    namedDestination.Destination.Mode = PdfDestinationMode.FitToPage
    document.NamedDestinationCollection.Add(namedDestination)
    bookmarks.NamedDestination = namedDestination
End Sub
 
'Add table of content with page number and document link annotations
Private Sub AddTableOfContent(ByVal page As PdfPage, ByVal toc As PdfPage, ByVal content As String, ByVal point As PointF)
    'Draw title in TOC
    Dim element As PdfTextElement = New PdfTextElement(content, 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, point, format)
    'Draw page number in TOC
    Dim pageNumber As PdfTextElement = New PdfTextElement(document.Pages.IndexOf(page).ToString(), font, PdfBrushes.Black)
    pageNumber.Draw(toc, New PointF(toc.Graphics.ClientSize.Width - 40, point.Y))
    '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 = content
    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)
End Sub

 

A complete working sample can be downloaded from CreateTOC_When_PDFMerging.zip.

By executing the program, you will get the PDF document as follows. Screenshot of output PDF document in .NET PDF library

Take a moment to peruse the documentation for working with bookmarks, merge documents and working with named destination, where you can find other options like insert, remove, and modify bookmarks and named destinations from the PDF document and merge multiple PDF documents from disk and stream.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to merge documents.

An online sample link to add bookmarks.

An online sample link to add named destinations.

Note:

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied