Free Support Forum - aspose.com

Create PDF document with TOC from scratch using Aspose.PDF for .NET - TOC pages order backward

Hello,
I’m using Aspose evaluation version for testing so we can decide whether we are gonna purchase it.
Results are satisfying, but I have one issue regarding table of content.

PDF is created from scratch and it consists of several pages(since there is no page break option) .
All headings inside one page are displayed correctly and in order, but order of pages is all backwards.
Like this:
Page3 Heading…
Page3 Subheading1…
Page3 Subheading2…
Page2 Heading…
Page2 SubHeading1…
Page1 Heading…
Page1 Subheading1…
Page1 Subheading2…

Here is the code:

        Document document = new Document();

        Page tocPage = document.Pages.Add();
        TocInfo tocInfo = new TocInfo();
        TextFragment title = new TextFragment("Table Of Contents");
        title.TextState.FontSize = 20;
        title.TextState.FontStyle = FontStyles.Bold;
        tocInfo.Title = title;
        tocPage.TocInfo = tocInfo;

        Page page1 = document.Pages.Add();
        Heading pg1Heading = new Heading(1);
        pg1Heading.TocPage = tocPage;
        pg1Heading.Text = "Page1 Heading";
        page1.Paragraphs.Add(pg1Heading);
        Heading pg1SubHeading1 = new Heading(2);
        pg1SubHeading1.TocPage = tocPage;
        pg1SubHeading1.Text = "Page1 SubHeading1";
        page1.Paragraphs.Add(pg1SubHeading1);
        Heading pg1SubHeading2 = new Heading(2);
        pg1SubHeading2.TocPage = tocPage;
        pg1SubHeading2.Text = "Page1 SubHeading2";
        page1.Paragraphs.Add(pg1SubHeading2);

        Page page2 = document.Pages.Add();
        Heading pg2Heading = new Heading(1);
        pg2Heading.TocPage = tocPage;
        pg2Heading.Text = "Page2 Heading";
        page2.Paragraphs.Add(pg2Heading);
        Heading pg2SubHeading1 = new Heading(2);
        pg2SubHeading1.TocPage = tocPage;
        pg2SubHeading1.Text = "Page2 SubHeading1";
        page2.Paragraphs.Add(pg2SubHeading1);

        Page page3 = document.Pages.Add();
        Heading pg3Heading = new Heading(1);
        pg3Heading.TocPage = tocPage;
        pg3Heading.Text = "Page3 Heading";
        page3.Paragraphs.Add(pg3Heading);
        Heading pg3SubHeading1 = new Heading(2);
        pg3SubHeading1.TocPage = tocPage;
        pg3SubHeading1.Text = "Page3 SubHeading1";
        page3.Paragraphs.Add(pg3SubHeading1);
        Heading pg3SubHeading2 = new Heading(2);
        pg3SubHeading2.TocPage = tocPage;
        pg3SubHeading2.Text = "Page3 SubHeading2";
        page3.Paragraphs.Add(pg3SubHeading2);

Please help me resolve this issue.

Regards

@vlabob

In order to create PDF document with TOC from scratch, please use the referred code example in the API documentation. In case you need further assistance, please feel free to let us know.

I’ve applied changes from proposed example and all I got is formatting and cosmetic fixes. Order is still messed up.
Here is the code:

        Document document = new Document();
        Page tocPage = document.Pages.Add();
        TocInfo tocInfo = new TocInfo();

        tocInfo.LineDash = TabLeaderType.Solid;
        TextFragment title = new TextFragment("Table Of Contents");
        title.TextState.FontSize = 20;
        tocInfo.Title = title;
        tocPage.TocInfo = tocInfo;

        tocInfo.FormatArrayLength = 4;
        tocInfo.FormatArray[0].Margin.Left = 0;
        tocInfo.FormatArray[0].Margin.Right = 30;
        tocInfo.FormatArray[0].LineDash = TabLeaderType.Dot;
        tocInfo.FormatArray[0].TextState.FontStyle = FontStyles.Bold | FontStyles.Italic;
        tocInfo.FormatArray[1].Margin.Left = 10;
        tocInfo.FormatArray[1].Margin.Right = 30;
        tocInfo.FormatArray[1].LineDash = TabLeaderType.None;
        tocInfo.FormatArray[1].TextState.FontSize = 10;
        tocInfo.FormatArray[2].Margin.Left = 20;
        tocInfo.FormatArray[2].Margin.Right = 30;
        tocInfo.FormatArray[2].TextState.FontStyle = FontStyles.Bold;
        tocInfo.FormatArray[3].LineDash = TabLeaderType.Solid;
        tocInfo.FormatArray[3].Margin.Left = 30;
        tocInfo.FormatArray[3].Margin.Right = 30;
        tocInfo.FormatArray[3].TextState.FontStyle = FontStyles.Bold;


        Page page1 = document.Pages.Add();
        Heading pg1Heading = new Heading(1);
        pg1Heading.IsAutoSequence = true;
        pg1Heading.TocPage = tocPage;
        pg1Heading.Text = "Page1 Heading";
        pg1Heading.IsInList = true;
        page1.Paragraphs.Add(pg1Heading);
        Heading pg1SubHeading1 = new Heading(2);
        pg1SubHeading1.IsAutoSequence = true;
        pg1SubHeading1.TocPage = tocPage;
        pg1SubHeading1.Text = "Page1 SubHeading1";
        pg1SubHeading1.IsInList = true;
        page1.Paragraphs.Add(pg1SubHeading1);
        Heading pg1SubHeading2 = new Heading(2);
        pg1SubHeading2.IsAutoSequence = true;
        pg1SubHeading2.TocPage = tocPage;
        pg1SubHeading2.Text = "Page1 SubHeading2";
        pg1SubHeading2.IsInList = true;
        page1.Paragraphs.Add(pg1SubHeading2);

        Page page2 = document.Pages.Add();
        Heading pg2Heading = new Heading(1);
        pg2Heading.IsAutoSequence = true;
        pg2Heading.TocPage = tocPage;
        pg2Heading.Text = "Page2 Heading";
        pg2Heading.IsInList = true;
        page2.Paragraphs.Add(pg2Heading);
        Heading pg2SubHeading1 = new Heading(2);
        pg2SubHeading1.IsAutoSequence = true;
        pg2SubHeading1.TocPage = tocPage;
        pg2SubHeading1.Text = "Page2 SubHeading1";
        pg2SubHeading1.IsInList = true;
        page2.Paragraphs.Add(pg2SubHeading1);

        Page page3 = document.Pages.Add();
        Heading pg3Heading = new Heading(1);
        pg3Heading.IsAutoSequence = true;
        pg3Heading.TocPage = tocPage;
        pg3Heading.Text = "Page3 Heading";
        pg3Heading.IsInList = true;
        page3.Paragraphs.Add(pg3Heading);
        Heading pg3SubHeading1 = new Heading(2);
        pg3SubHeading1.IsAutoSequence = true;
        pg3SubHeading1.TocPage = tocPage;
        pg3SubHeading1.Text = "Page3 SubHeading1";
        pg3SubHeading1.IsInList = true;
        page3.Paragraphs.Add(pg3SubHeading1);
        Heading pg3SubHeading2 = new Heading(2);
        pg3SubHeading2.IsAutoSequence = true;
        pg3SubHeading2.TocPage = tocPage;
        pg3SubHeading2.Text = "Page3 SubHeading2";
        pg3SubHeading2.IsInList = true;
        page3.Paragraphs.Add(pg3SubHeading2);

and result:

TestReport.pdf (40.7 KB)

@vlabob

We have logged an investigation ticket as PDFNET-47444 in our issue tracking system for the sake of investigation. We will further look into details of scenario and share our feedback with you as soon as the ticket is resolved. Please be patient and spare us little time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-47444) have been fixed in Aspose.PDF for .NET 20.1.

1 Like

Thanks for the fix!

@vlabob,

You are very welcome.