Dynamic TOC .net pdf

Hi All,

I am passing a dynamic html and generating pdf. I want to have a Table of Contents for each heading on a page. For example -

Heading1 -
…text
Heading 2-
…text
Heading 3-
…text

and so on, so the TOC should contain each heading from each page along with page numbers.

please help.

@jinivthakkar

Thanks for contacting support.

Please note that there is no way to determine/extract headings from PDF, as after becoming the part of PDF, they are represented only as text. Whereas we need heading text, in order to use it in TOC. However as a workaround, you can save all headings into a List<string> from your HTML string, while adding it into PDF document and use that list to create TOC.

Your code snippet to achieve the requirement, will be as follows:

/// ********************************
// Code to add dynamic HTML
/// ********************************
doc.ProcessParagraphs();
Page _tocPage = doc.Pages.Insert(1);
TocInfo tocInfo = new TocInfo { FormatArrayLength = 3 };
_tocPage.TocInfo = tocInfo;
// allHeadings is a List<string> which contains all heading from HTML
for (int i = 0; i < allHeadings.Count; i++)
{
 TextFragmentAbsorber absorber = new TextFragmentAbsorber(allHeadings[i]);
 doc.Pages.Accept(absorber);
 if (absorber.TextFragments.Count > 0)
 {
  Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
  TextSegment segment2 = new TextSegment();
  heading2.TocPage = _tocPage;
  heading2.Segments.Add(segment2);
  heading2.DestinationPage = absorber.TextFragments[1].Page;
  heading2.Top = absorber.TextFragments[1].Page.Rect.Height;
  segment2.Text = absorber.TextFragments[1].Text;
  _tocPage.Paragraphs.Add(heading2);
 }
}
doc.Save(dataDir + @"TOC_Text.pdf");

In case you face any issue, please share your sample HTML string/file, so that we can test the scenario in our environment and address it accordingly.

Any pointers on how to extract headings from html and store in array ?

@jinivthakkar

Thanks for writing back.

You can write your own logic in C# to extract the headings from HTML. In case you experience any issue, as requested earlier, please share a sample HTML string or file, so that we can try to create sample code snippet to achieve the functionality, you require.

Hi Team,

I have managed to do the same but now I am stuck with this - I have a ul which contains one < li> tag which is parent, example Country name and following ul li structure contain City and Pincode details like this :

< ul>
        < li><a href='#sbjNOVARTIS'><b>India</b></a></li>
        < div>
             < ul>
                < li>
                    < a href='#br88'>
                     Pune
                     
411046
                        
                    </a>
                </li>
            </ul>
        </div>
    </ul>

I want to show TOC as follows -

India
…Pune
…411046

Right now it shows like this -
India
Pune
411046

@jinivthakkar

Thanks for sharing the sample HTML string.

However, would you please share if TOC is appearing like this in PDF, you are generating? If so, please share the sample code snippet which you are using to generate TOC. We will try to modify it, so that it can create desired TOC.

        string html="";// above html

    HtmlDocument htmlDoc = new HtmlDocument();

        htmlDoc.LoadHtml(html);            

    Document doc = new Document("C://Test//htmlToPdfTest1.pdf")

    Page tocPage = doc.Pages.Insert(1);

        tocPage.Background = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

        // Create object to represent TOC information
        TocInfo tocInfo = new TocInfo();
        TextFragment title = new TextFragment("Table Of Contents");
        title.TextState.FontSize = 20;
        title.TextState.FontStyle = FontStyles.Bold;

        // Set the title for TOC
        tocInfo.Title = title;
        tocPage.TocInfo = tocInfo;


        for (int i = 0; i < 23; i++)
        {
            // Create Heading object
            Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

            
            
            TextSegment segment2 = new TextSegment();
             segment2 = new TextSegment();
            heading2.TocPage = tocPage;
            
            heading2.Segments.Add(segment2);

            // Specify the destination page for heading object
            heading2.DestinationPage = doc.Pages[i + 2];

            // Destination page
            heading2.Top = doc.Pages[i + 2].Rect.Height;

            // Destination coordinate
            segment2.Text = allHeading[i];
            

            // Add heading to page containing TOC
            tocPage.Paragraphs.Add(heading2);
        }
        // Save the updated document
        doc.Save("c:/test/TOC.pdf");

@jinivthakkar

Thanks for sharing the sample code snippet.

We have tried to run the shared code snippet but got no success as there were some undefined objects in the code snippet i.e allHeading[i]. Would you please share a complete sample code snippet, which is able to produce a PDF document? Also, it will be really helpful if you please share a sample output PDF document, generated at your end.

However, concerning to your above requirements, you can achieve this by changing the heading level for TOC item (e.g Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1); // where 1 is first level and it can also be 2 or 3 for child items).

Furthermore, please share complete code snippet, as requested above, so that we can test the scenario in our environment and address it accordingly.

Hi,

I was able to achieve it by using -

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

Thanks for your help.

@jinivthakkar

Thanks for kind feedback.

It is good to know that your were able to achieve your requirement by using suggested code. Please keep using our API and in case of any other query, please feel free to create a new topic. We will be glad to assist you accordingly.