How to create table of contents on PDF in C#

Table of contents (TOC) makes the PDF documents more accessible and easier to navigate, especially for large files. Usually the table of contents is included in the PDF as the first page. With the help of Spire.PDF, we can add a new blank page to the existing PDF file, and then create a TOC on it C# and VB.NET. This How To Guide for developers will explain the steps of create table of contents in C# with the help of a .NET PDF API Spire.PDF.

Here comes to the code snippets of how to add TOC to a PDF file in C#.

Step 1: Create a new PDF document and load from file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample2.pdf");

Step 2: Get the PDF file's page count and insert a new blank page as the first page.

int pageCount = doc.Pages.Count;
PdfPageBase tocPage = doc.Pages.Insert(0);

Step 3: Draw a title to the new page and set its font, style and location.

string title = "Table Of Contents";
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial", 16, FontStyle.Bold));
PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
PointF location = new PointF(tocPage.Canvas.ClientSize.Width / 2, titleFont.MeasureString(title).Height);
tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.Black, location, centerAlignment);

Step 4: Draw TOC text to the page.

PdfTrueTypeFont titlesFont = new PdfTrueTypeFont(new Font("Arial", 10));
String[] titles = new String[pageCount];
for (int i = 0; i < titles.Length; i++)
{
    titles[i] = string.Format("This is page{0}", i + 1);
}
float y = titleFont.MeasureString(title).Height + 10;
float x = 0;

for (int i = 1; i <= pageCount; i++)
{
    string text = titles[i - 1];
    SizeF titleSize = titlesFont.MeasureString(text);

    PdfPageBase navigatedPage = doc.Pages[i];

    string pageNumText = (i + 1).ToString();
    SizeF pageNumTextSize = titlesFont.MeasureString(pageNumText);
    tocPage.Canvas.DrawString(text, titlesFont, PdfBrushes.Black, 0, y);
    float dotLocation = titleSize.Width + 2 + x;
    float pageNumlocation = tocPage.Canvas.ClientSize.Width - pageNumTextSize.Width;
    for (float j = dotLocation; j < pageNumlocation; j++)
    {
        if (dotLocation >= pageNumlocation)
        {
            break;
        }
        tocPage.Canvas.DrawString(".", titlesFont, PdfBrushes.Black, dotLocation, y);
        dotLocation += 2;
    }
    tocPage.Canvas.DrawString(pageNumText, titlesFont, PdfBrushes.Black, pageNumlocation, y);

Step 5: Add TOC action.

location = new PointF(0, y);
 RectangleF titleBounds = new RectangleF(location, new SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height));
 PdfDestination Dest = new PdfDestination(navigatedPage, new PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left));
 PdfActionAnnotation action = new PdfActionAnnotation(titleBounds, new PdfGoToAction(Dest));
 action.Border = new PdfAnnotationBorder(0);
 (tocPage as PdfNewPage).Annotations.Add(action);
 y += titleSize.Height + 10;

Step 6: Save the document to file.

string output = "AddTableOfContent.pdf";
doc.SaveToFile(output, FileFormat.PDF);   

View the effective screenshot of the table of contents created by the Spire.PDF in C# via the above codes.

How to create table of contents on PDF in C#

Full codes:

static void Main(string[] args)
{
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile("sample2.pdf");

    int pageCount = doc.Pages.Count;
    PdfPageBase tocPage = doc.Pages.Insert(0);

    string title = "Table Of Contents";
    PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial", 16, FontStyle.Bold));
    PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
    PointF location = new PointF(tocPage.Canvas.ClientSize.Width / 2, titleFont.MeasureString(title).Height);
    tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.Black, location, centerAlignment);

    PdfTrueTypeFont titlesFont = new PdfTrueTypeFont(new Font("Arial", 10));
    String[] titles = new String[pageCount];
    for (int i = 0; i < titles.Length; i++)
    {
        titles[i] = string.Format("This is page{0}", i + 1);
    }
    float y = titleFont.MeasureString(title).Height + 10;
    float x = 0;

    for (int i = 1; i <= pageCount; i++)
    {
        string text = titles[i - 1];
        SizeF titleSize = titlesFont.MeasureString(text);

        PdfPageBase navigatedPage = doc.Pages[i];

        string pageNumText = (i + 1).ToString();
        SizeF pageNumTextSize = titlesFont.MeasureString(pageNumText);
        tocPage.Canvas.DrawString(text, titlesFont, PdfBrushes.Black, 0, y);
        float dotLocation = titleSize.Width + 2 + x;
        float pageNumlocation = tocPage.Canvas.ClientSize.Width - pageNumTextSize.Width;
        for (float j = dotLocation; j < pageNumlocation; j++)
        {
            if (dotLocation >= pageNumlocation)
            {
                break;
            }
            tocPage.Canvas.DrawString(".", titlesFont, PdfBrushes.Black, dotLocation, y);
            dotLocation += 2;
        }
        tocPage.Canvas.DrawString(pageNumText, titlesFont, PdfBrushes.Black, pageNumlocation, y);

        location = new PointF(0, y);
        RectangleF titleBounds = new RectangleF(location, new SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height));
        PdfDestination Dest = new PdfDestination(navigatedPage, new PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left));
        PdfActionAnnotation action = new PdfActionAnnotation(titleBounds, new PdfGoToAction(Dest));
        action.Border = new PdfAnnotationBorder(0);
        (tocPage as PdfNewPage).Annotations.Add(action);
        y += titleSize.Height + 10;
    }

    string output = "AddTableOfContent.pdf";

    doc.SaveToFile(output,FileFormat.PDF);          
    
}