Home > Code > C# > How to Create Table of Contents in an Existing PDF File using .NET

How to Create Table of Contents in an Existing PDF File using .NET

by sher azam   on Feb 19, 2014   Category: C#   |  Views: 2933    |  Points: 25   |  Gold 


This technical tip shows how .NET developers can Create Table of Contents in an Existing PDF File using Aspose.Pdf for .NET. The ListSection class in the Aspose.Pdf.Generator namespace provides the capability to create a table of contents while creating a PDF document from scratch. In order to add headings, which serve as elements of the TOC, we use the Aspose.Pdf.Generator.Heading class. To also support adding a TOC to an existing PDF file, there's a Heading class in the Aspose.Pdf namespace. The Aspose.Pdf namespace can both create new and manipulate existing PDF files. To add a TOC to an existing PDF, use the Aspose.Pdf namespace.

The following code snippet shows how to create a table of contents inside an existing PDF file.

[C# Code Sample]

// Load an existing PDF files
Document doc = new Document("input.pdf");

// Get access to first page of PDF file
Page tocPage = doc.Pages.Insert(1);

// Create object to represent TOC information
TocInfo tocInfo = new TocInfo();
TextFragment title = new TextFragment("Table Of Contents");
title.TextState.FontSize = 20;
title.TextState.FontStyle = FontStyles.Bold;

// Set the title for TOC
tocInfo.Title = title;
tocPage.TocInfo = tocInfo;

// Create string objects which will be used as TOC elements
string[] titles = new string[4];
titles[0] = "First page";
titles[1] = "Second page";
titles[2] = "Third page";
titles[3] = "Fourth page";
for (int i = 0; i < 4; i++)
{
// Create Heading object
Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
TextSegment segment2 = new TextSegment();
heading2.TocPage = tocPage;
heading2.Segments.Add(segment2);

// Specify the destination page for heading object
heading2.DestinationPage = doc.Pages[i + 2];

// Destination page
heading2.Top = doc.Pages[i + 2].Rect.Height;

// Destination coordinate
segment2.Text = titles;

// Add heading to page containing TOC
tocPage.Paragraphs.Add(heading2);
}
// Save the updated document
doc.Save("TOC_Output.pdf");

[VB.NET Code Sample]


' Load an existing PDF files
Dim doc As Document = New Document("input.pdf")
' Get access to first page of PDF file
Dim tocPage As Page = doc.Pages.Insert(1)
' Create object to represent TOC information
Dim tocInfo As TocInfo = New TocInfo()
Dim title As TextFragment = New TextFragment("Table Of Contents")
title.TextState.FontSize = 20
title.TextState.FontStyle = FontStyles.Bold
' Set the title for TOC
tocInfo.Title = title
tocPage.TocInfo = tocInfo
' Create string objects which will be used as TOC elements
Dim titles As String() = {"First page", "Second page", "Third page", "Fourth page"}
For i As Integer = 0 To 3
' Create Heading object
Dim heading2 As Aspose.Pdf.Heading = New Aspose.Pdf.Heading(1)
Dim segment2 As TextSegment = New TextSegment()
heading2.TocPage = tocPage
heading2.Segments.Add(segment2)
' Specify the destination page for heading object
heading2.DestinationPage = doc.Pages(i + 2)
' Destination page
heading2.Top = doc.Pages(i + 2).Rect.Height
' Destination coordinate
segment2.Text = titles(i)
' Add heading to page containing TOC
tocPage.Paragraphs.Add(heading2)
Next
' Save the updated document
doc.Save("TOC_Output.pdf")



More about Aspose.Pdf for .NET

- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Read More about Working with Document Conversion: http://www.aspose.com/docs/display/pdfnet/Working+with+Document+Conversion



Post Code  |  Code Snippet Home

User Responses


  Re :How to Create Table of Contents in an Existing PDF File using .NET   
Posted by Leo_21
on 25/06/2014
Points : 5

Hi all,

As an another option,the following code snippet in C# will generate table in an existing PDF, check

PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.DataSource = dataSource;
PdfLayoutResult result = table.Draw(page, new PointF(0, y));
y = y + result.Bounds.Height + 5;
PdfBrush brush2 = PdfBrushes.Gray;
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
page.Canvas.DrawString(String.Format("* {0} countries in the list.", data.Length - 1), font2, brush2, 5, y);

AND this method is based on Spire.PDF avaliable here -https://pdfapi.codeplex.com
hope it helps

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts