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.
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.
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 :
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.
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.
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.