How to Add Multiple Tables of Contents in a PDF Document inside .NET Apps
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//your code here...
//[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");
X
Url: http://www.aspose.com/.net/pdf-component.aspx
Language: C# | User: Sheraz Khan | Created: Dec 16, 2015 | Tags: Add multipleTable of Contents to PDF create a instance section in document Create list heading into Table Of TOC Existing .NET API