Free Support Forum - aspose.com

How to define 2 or Multple TOC inside a PDF product

Hi,

Is it possible to define more than 1 TOC inside a Single PDF file. i.e. is it possible to have multiple ListType.TableOfContents? If it is feasible please provide any code snippet.

With regards,

Rajat Nayyar

Hello Rajat,

Thanks for contacting support.

Please try using the following code snippet to create two Table Of Contents in a single PDF document. 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 IsInList = true, and set the TOC property. If TOC is not set, Heading elements will be added to the first TableOfContents of the document. For your reference, I have also attached the resultant PDF that I have generated using Aspose.Pdf for .NET 6.1.0

[C#]

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 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;
//heading2.TOC = tocSection;
sec1.Paragraphs.Add(heading2);
}

//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;
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
Aspose.Pdf.Generator.Section sec2 = pdf.Sections.Add();
//Add four headings in the section
for (int Level = 1; Level < 5; Level++)
{
Heading heading2_Sec = new Aspose.Pdf.Generator.Heading(pdf, sec1, Level);
Segment segment2_Sec = new Segment(heading2_Sec);
heading2_Sec.Segments.Add(segment2_Sec);
heading2_Sec.IsAutoSequence = true;
segment2_Sec.Content = "this is heading of level ";
segment2_Sec.Content += Level.ToString();
//Add the heading into Table Of Contents.
heading2_Sec.IsInList = true;
heading2_Sec.TOC = tocSection2;
sec2.Paragraphs.Add(heading2_Sec);
}

pdf.Save("d:/pdftest/Multiple_TOC.pdf");