Aspose.Pdf

Manipulating List of Contents

Sometimes developers may like to add a list of contents in the PDF document. To save developer's efforts, Aspose.Pdf has provided ListSection class to display the list of contents in a PDF document. Three types of pre-defined lists are supported by Aspose.Pdf as follows:

 

 

Developers can use any type of the list to display their contents by setting the ListType property of ListSection class to any pre-defined value of ListType enumeration. The pre-defined values of ListType enumeration are listed below:

 

Types of List

Description

TableOfContents

Displays table of contents

ListOfTables

Displays list of tables

ListOfFigures

Displays list of figures

 

The document's list is automatically created by Aspose.Pdf . So, users only need to take care of following three issues:

 

 

The above three issues are discussed below in more detail.

 

Step 1: Adding the List

 

Since a ListSection is a Section , it can be added to the document just like a common Section . The following code shows how to add a ListSection into the document.

 

Example:

 

[C#]

 

//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);

 

[VB.NET]

 

'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)

 

Developers can add zero to three list sections in one PDF document. But, you must specify that which type of ListSection you want to use. For selecting a particular type of list section, ListType enumeration is used that contains three pre-defined types of list section as described in the beginning of this topic.

In the example below, we are going to add three list sections (TableOfContents, ListOfTables, ListOfFigures) into our PDF document.

 

Example:

 

[C#]

 

//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);

 

//Create a list section

ListSection lotSection = new ListSection("List of Tables");

 

//Set its list type as list of tables

lotSection.ListType = ListType.ListOfTables;

 

//Add the list section to the sections collection of the Pdf document

pdf.Sections.Add(lotSection);

 

//Create a list section

ListSection lofSection = new ListSection("List of Figures");

 

//Set its list type as list of figures

lofSection.ListType = ListType.ListOfFigures;

 

//Add the list section to the sections collection of the Pdf document

pdf.Sections.Add(lofSection);

 

[VB.NET]

 

'Create a list section

Dim tocSection As ListSection = new ListSection("Table Of Contents")

 

'Set its list type as table of contents

tocSection.ListType = ListType.TableOfContents

 

'Add the list section to the sections collection of the Pdf document

pdf.Sections.Add(tocSection)

 

'Create a list section

Dim lotSection As ListSection = new ListSection("List of Tables")

 

'Set its list type as list of tables

lotSection.ListType = ListType.ListOfTables

 

'Add the list section to the sections collection of the Pdf document

pdf.Sections.Add(lotSection)

 

'Create a list section

Dim lofSection As ListSection = new ListSection("List of Figures")

 

'Set its list type as list of figures

lofSection.ListType = ListType.ListOfFigures

 

'Add the list section to the sections collection of the Pdf document

pdf.Sections.Add(lofSection)

 

After this step, the user should define the format of the list in next step.

 

Step 2: Defining the Format

 

As we know that a list can be multi-level so, in order to define the format of every level of the list, Aspose.Pdf provides a new class, ListLevelFormat , which indicates the format of a single level of the list. The ListSection class has a member ListFormatArray , which is a dynamic array of ListLevelFormat objects. So, developers can make use of ListLevelFormat object through ListFormatArray property of ListSection class.

 

Note: ListLevelFormat has two properties: LeftMargin and TextInfo that allow to set the left margin and text format settings for any particular level of the list.

 

By default, the ListSection provides a three levels format whose demonstration is given below in the example.

 

Example:

 

[C#]

 

//Set the length of list levels to 3 and set their left margins and text formats

ListFormatArray.Length = 3;

ListFormatArray[0].LeftMargin = 0;

ListFormatArray[0].TextInfo.FontSize = 16;

ListFormatArray[0].TextInfo.IsTrueTypeFontBold = true;

ListFormatArray[1].LeftMargin = 16;

ListFormatArray[1].TextInfo.FontSize = 14;

ListFormatArray[2].LeftMargin = 32;

ListFormatArray[2].TextInfo.FontSize = 12;

 

But developers are free to define their own list format if they don't want to use the default format provided by Aspose.Pdf . This methodology is explained below in an example code snippet.

 

Example:

 

[C#]

 

//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;

 

[VB.NET]

 

'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

 

[XML]

 

<?xml version="1.0" encoding="utf-8"?>

<Pdf>

     <ListSection ListType="TableOfContents">

         <Title Alignment="Center">

             <Segment IsTrueTypeFontBold="true"

                 FontSize="30"> TableOfContents </Segment>

         </Title>

         <ListLevelFormat Level="1" LeftMargin="0">

             <TextInfo IsTrueTypeFontBold="true"

                 IsTrueTypeFontItalic="true">

             </TextInfo>

         </ListLevelFormat>

         <ListLevelFormat Level="2" LeftMargin="10">

             <TextInfo IsUnderline="true" FontSize="10">

             </TextInfo>

         </ListLevelFormat>

         <ListLevelFormat Level="3" LeftMargin="20">

             <TextInfo IsTrueTypeFontBold="true">

             </TextInfo>

         </ListLevelFormat>

         <ListLevelFormat Level="4" LeftMargin="30">

             <TextInfo IsTrueTypeFontBold="true">

             </TextInfo>

         </ListLevelFormat>

     </ListSection>

     <ListSection ListType="ListOfTables">

         <Title>

             <Segment IsTrueTypeFontBold="true"

                 FontSize="30">ListOfTables</Segment>

         </Title>

     </ListSection>

     <ListSection ListType="ListOfFigures">

         <Title>

              <Segment IsTrueTypeFontBold="true"

                 FontSize="30">ListOfFigures</Segment>

         </Title>

     </ListSection>

</Pdf>

 

Step 3: Specifying the List Items

 

After adding the ListSection to the document and defining the format of every level of the list, the last thing developers should do is to specify which object needs to be added to the list. The objects which can be specified as a list item include Heading , Table , Image and Graph . Set IsInList property of any of these four objects to true then that particular object will be added to the corresponding list automatically. It means that:

 

 

The following example code snippet demonstrates about how to specify list items:

 

Example:

 

[C#]

 

//Create a section in the Pdf document

Section sec1 = pdf.Sections.Add();

 

//Add four headings in the section

for (int Level = 1;Level != 5; Level++)

{

     Heading heading2 = new 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;

     sec1.Paragraphs.Add(heading2);

}

 

//Create a graph and add a curve shape to its shapes collection

Graph graph1 = new Graph(sec1,100,400);

sec1.Paragraphs.Add(graph1);

float[] posArr = new float[]{0,0,200,80,300,40,350,90};

Curve curve1 = new Curve(graph1,posArr);

graph1.Shapes.Add(curve1);

 

//Add the Graph to the List of Figures

graph1.IsInList = true;

 

[VB.NET]

 

'Create a section in the Pdf document

Dim sec1 As Section = pdf.Sections.Add()

 

'Add four headings in the section

Dim Level As Int

For Level=1 To 4

     Dim heading2 As Heading= new 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

     sec1.Paragraphs.Add(heading2)

Next Level

 

'Create a graph and add a curve shape to its shapes collection

Dim graph1 As Graph = new Graph(sec1,100,400)

sec1.Paragraphs.Add(graph1)

Dim posArr() As Single = New Single() {0, 0, 200, 80, 300, 40, 350, 90}

Dim curve1 As Curve = New Curve(graph1, posArr)

graph1.Shapes.Add(curve1)

 

'Add the Graph to the List of Figures

graph1.IsInList = true

 

[XML]

 

<Section>

     <Heading level="1" IsAutoSequence="true" IsInList="true">

         <Segment>this is heading of level 1</Segment>

     </Heading>

     <Heading level="2" IsAutoSequence="true" IsInList="true">

         <Segment>this is heading of level 2</Segment>

     </Heading>

     <Heading level="3" IsAutoSequence="true" IsInList="true">

         <Segment>this is heading of level 3</Segment>

     </Heading>

     <Heading level="4" IsAutoSequence="true" IsInList="true">

         <Segment>this is heading of level 4</Segment>

     </Heading>

     <Graph Height="100" Width="400" IsInList="true">

         <Curve Position="0 0 200 80 300 40 350 90"/>

             <Title>one curve</Title>

     </Graph>

</Section>