Home > Code > VB.NET > How to Add Multiple Tables of Contents in a PDF Document inside .NET Apps

How to Add Multiple Tables of Contents in a PDF Document inside .NET Apps

by sher azam   on Dec 16, 2015   Category: VB.NET   |  Views: 580    |  Points: 25   |  Gold 


This technical tip shows how .NET developers can add multiple tables of contents in a PDF document inside .NET application using Aspose.Pdf for .NET. Adding more than one Table Of Contents is quite handy when using Aspose.Pdf for .NET. Please note that we have a property named TOC in [*Heading*] class which is the reference to corresponding TableOfContents. To make a paragraph to appear in a TableOfContents, you must set the value of [*IsInList*] to True and set the [*TOC*] property. If TOC is not set, Heading elements will be added to the first TableOfContents of the document. In following code snippet, we have first created first instance of ListSection and did not use the property TOC of Heading class. So as a result, the elements of first heading are added by default to frist List Section. However later we careated an other instance of ListSection and for Heading2 object, we passed the value of second ListSection against TOC property.

The following code snippet shows how to add multiple table of contents in a PDF document.

//[C# Code Sample]


// create a PDF instance
Pdf pdf = new Pdf();
//Create a list section
ListSection tocSection = new ListSection("Table Of Contents");
//Set its list type as table of of contents
tocSection.ListType = ListType.TableOfContents;
//Add the list section to the sections collection of the Pdf document
pdf.Sections.Add(tocSection);

//Define the format of the four levels list by setting the left margins and
//text format settings of each level
tocSection.ListFormatArray.Length = 4;
tocSection.ListFormatArray[0].LeftMargin = 0;
tocSection.ListFormatArray[0].TextInfo.IsTrueTypeFontBold = true;
tocSection.ListFormatArray[0].TextInfo.IsTrueTypeFontItalic = true;
tocSection.ListFormatArray[1].LeftMargin = 10;
tocSection.ListFormatArray[1].TextInfo.IsUnderline = true;
tocSection.ListFormatArray[1].TextInfo.FontSize = 10;
tocSection.ListFormatArray[2].LeftMargin = 20;
tocSection.ListFormatArray[2].TextInfo.IsTrueTypeFontBold = true;
tocSection.ListFormatArray[3].LeftMargin = 30;
tocSection.ListFormatArray[3].TextInfo.IsTrueTypeFontBold = true;

//Create a section in the Pdf document
Aspose.Pdf.Generator.Section sec1 = pdf.Sections.Add();

//Add four headings in the section
for (int Level = 1; Level < 5; Level++)
{
Heading heading1 = new Aspose.Pdf.Generator.Heading(pdf, sec1, Level);
Segment segment1 = new Segment(heading1);
heading1.Segments.Add(segment1);
heading1.IsAutoSequence = true;
segment1.Content = "this is heading of level ";
segment1.Content += Level.ToString();

//Add the heading into Table Of Contents.
heading1.IsInList = true;
//heading2.TOC = tocSection;
sec1.Paragraphs.Add(heading1);
}

//Create a list section
ListSection tocSection2 = new ListSection("Second Table Of Contents");
//Set its list type as table of of contents
tocSection2.ListType = ListType.TableOfContents;
//Add the list section to the sections collection of the Pdf document
pdf.Sections.Add(tocSection2);

//Define the format of the four levels list by setting the left margins and
//text format settings of each level
tocSection2.ListFormatArray.Length = 4;
tocSection2.ListFormatArray[0].LeftMargin = 0;
tocSection2.ListFormatArray[0].TextInfo.IsTrueTypeFontBold = true;
tocSection2.ListFormatArray[0].TextInfo.IsTrueTypeFontItalic = true;
tocSection2.ListFormatArray[1].LeftMargin = 10;
tocSection2.ListFormatArray[1].TextInfo.IsUnderline = true;
ocSection2.ListFormatArray[1].TextInfo.FontSize = 10;
tocSection2.ListFormatArray[2].LeftMargin = 20;
tocSection2.ListFormatArray[2].TextInfo.IsTrueTypeFontBold = true;
tocSection2.ListFormatArray[3].LeftMargin = 30;
tocSection2.ListFormatArray[3].TextInfo.IsTrueTypeFontBold = true;

//Create a section in the Pdf document
Aspose.Pdf.Generator.Section sec2 = pdf.Sections.Add();

//Add four headings in the section
for (int Level = 1; Level < 5; Level++)
{
Heading heading2 = new Aspose.Pdf.Generator.Heading(pdf, sec1, Level);
Segment segment2 = new Segment(heading2);
heading2.Segments.Add(segment2);
heading2.IsAutoSequence = true;
segment2.Content = "this is heading of level ";
segment2.Content += Level.ToString();

//Add the heading into Table Of Contents.
heading2.IsInList = true;
// add the heading elements to second ListSection
heading2.TOC = tocSection2;
// add the heading obejct to paragraphs colelction of section2
sec2.Paragraphs.Add(heading2);
}
//save the resultant PDF document
pdf.Save("d:/pdftest/Multiple_TOC.pdf");

//[VB.NET Code Sample]


' create a PDF instance
Dim pdf As Pdf = New Pdf()
'Create a list section
Dim tocSection As ListSection = New ListSection("Table Of Contents")
'Set its list type as table of of contents
tocSection.ListType = ListType.TableOfContents
'Add the list section to the sections collection of the Pdf document
pdf.Sections.Add(tocSection)

'Define the format of the four levels list by setting the left margins and
'text format settings of each level
tocSection.ListFormatArray.Length = 4
tocSection.ListFormatArray(0).LeftMargin = 0
tocSection.ListFormatArray(0).TextInfo.IsTrueTypeFontBold = True
tocSection.ListFormatArray(0).TextInfo.IsTrueTypeFontItalic = True
tocSection.ListFormatArray(1).LeftMargin = 10
tocSection.ListFormatArray(1).TextInfo.IsUnderline = True
tocSection.ListFormatArray(1).TextInfo.FontSize = 10
tocSection.ListFormatArray(2).LeftMargin = 20
tocSection.ListFormatArray(2).TextInfo.IsTrueTypeFontBold = True
tocSection.ListFormatArray(3).LeftMargin = 30
tocSection.ListFormatArray(3).TextInfo.IsTrueTypeFontBold = True

'Create a section in the Pdf document
Dim sec1 As Aspose.Pdf.Generator.Section = pdf.Sections.Add()

Dim Level As Integer
'Add four headings in the section
For Level = 1 To 4

Dim heading1 As Heading = New Aspose.Pdf.Generator.Heading(pdf, sec1, Level)
Dim segment1 As Segment = New Segment(heading1)
heading1.Segments.Add(segment1)
heading1.IsAutoSequence = True
segment1.Content = "this is heading of level "
segment1.Content += Level.ToString()

'Add the heading into Table Of Contents.
heading1.IsInList = True
'heading2.TOC = tocSection;
sec1.Paragraphs.Add(heading1)
Next

'Create a list section
Dim tocSection2 As ListSection = New ListSection("Second Table Of Contents")
'Set its list type as table of of contents
tocSection2.ListType = ListType.TableOfContents

'Add the list section to the sections collection of the Pdf document
pdf.Sections.Add(tocSection2)

'Define the format of the four levels list by setting the left margins and
'text format settings of each level
tocSection2.ListFormatArray.Length = 4
tocSection2.ListFormatArray(0).LeftMargin = 0
tocSection2.ListFormatArray(0).TextInfo.IsTrueTypeFontBold = True
tocSection2.ListFormatArray(0).TextInfo.IsTrueTypeFontItalic = True
tocSection2.ListFormatArray(1).LeftMargin = 10
tocSection2.ListFormatArray(1).TextInfo.IsUnderline = True
tocSection2.ListFormatArray(1).TextInfo.FontSize = 10
tocSection2.ListFormatArray(2).LeftMargin = 20
tocSection2.ListFormatArray(2).TextInfo.IsTrueTypeFontBold = True
tocSection2.ListFormatArray(3).LeftMargin = 30
tocSection2.ListFormatArray(3).TextInfo.IsTrueTypeFontBold = True

'Create a section in the Pdf document
Dim sec2 As Aspose.Pdf.Generator.Section = pdf.Sections.Add()

'Add four headings in the section
For Level = 1 To 4

Dim heading2 As Heading = New Aspose.Pdf.Generator.Heading(pdf, sec1, Level)
Dim segment2 As Segment = New Segment(heading2)
heading2.Segments.Add(segment2)
heading2.IsAutoSequence = True
segment2.Content = "this is heading of level "
segment2.Content += Level.ToString()

'Add the heading into Table Of Contents.
heading2.IsInList = True
' add the heading elements to second ListSection
heading2.TOC = tocSection2
' add the heading obejct to paragraphs colelction of section2
sec2.Paragraphs.Add(heading2)
Next
'save the resultant PDF document
pdf.Save("d:/pdftest/Multiple_TOC.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


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts