Problem generating html with table of contents

Hi,

In my application I have a requirement in which I have to generate html with table of contents.I know that this feature is not directly supported in Aspose so I found a way around.

Using documentbuilder’s insertTableofContent method by passing field codes I created a document with TOC. I opened that document manually and updated the doc to make toc appear on doc. Now when I generate html by using this new generated document it is coming fine with proper TOC. Now my question is that is there any way by which i can avoid this manual update of document and can generate html directly or do we have any other way to achieve the same.

Please suggest.

Hello!

Thank you for your interest in Aspose.Words.

I’m sorry for verbosity but this question needs some explanation and clarification.

Tables of contents in paginated formats and in HTML are different. For instance you can convert a document to HTML with MS Word. When you open this HTML in a browser it doesn’t show page numbers. It just shows items each for corresponding topic and allows navigation to them. This is not a feature of HTML but an MS extension. Aspose.Words produces standard HTML (really with minimum MS attributes written on demand).

In standard HTML there are no pages and page numbers. The whole document is one page. And if a document seems to be huge its authors think about splitting it into separate pages and placing a table of contents to some start page. This is table of contents in terms of HTML.

So what kind of table of contents would you like to have in your HTML?

Paginated table of contents cannot be inserted fully automatically since this needs pagination to calculate particular page numbers. Non-paginated TOC can be built traversing headings in the document and putting links to them. Maybe that’s what you really need.

Please provide us more information and we’ll try suggesting a solution. You feedback end experience is much appreciated.

Regards,

Hi,

thankx for this quick reply.

As you have suggested that non paginated TOC can be built traversing heading in the document and putting links to them.Please explain that how can I achieve this.

Hi

Thanks for your request. You can use bookmarks and hyperlinks to achieve this. For example see the following code.

//Open a document
Document doc = new Document(@"Test121\in.doc");
//Create document builder, it will be needed for building TOC
DocumentBuilder builder = new DocumentBuilder(doc);
//Get collection of Paragraphs from document
NodeCollection paragraphsCollection = doc.GetChildNodes(NodeType.Paragraph, true);

int tocItemIndex = 0;
int paragraphsCount = paragraphsCollection.Count - 1;
for (int paragraphIndex = paragraphsCount; paragraphIndex >= 0; paragraphIndex--)
{
    Paragraph par = (Paragraph)paragraphsCollection[paragraphIndex];
    //Check if paragraph is Heading
    if (par.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 ||
    par.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2)
    {
        //Create bookmark name
        string bookmarkName = "item_" + tocItemIndex.ToString();
        //Increase index
        tocItemIndex++;
        //Move cursor to the end of the current para
        builder.MoveTo(par);
        //Insert bookmark
        builder.StartBookmark(bookmarkName);
        builder.EndBookmark(bookmarkName);
        //Move cursor document start
        builder.MoveToDocumentStart();
        //Clear formating
        builder.ParagraphFormat.ClearFormatting();
        //Format font as hyperlink
        builder.Font.Underline = Underline.Single;
        builder.Font.Color = Color.Blue;
        //InsertTOC item
        builder.InsertHyperlink(par.Range.Text, bookmarkName, true);
    }
}
//Save output document
doc.Save(@"Test121\out.html", SaveFormat.Html);

I hope this could help you.

Best regards.

Hi ,

Thankx for the solution.

I tried the same approach with attached document but I don’t know links are not coming in generated document. even paragraph.getText() function is giving me some weird result. I am sure that something wrong with document. Please have a look and suggest why it is happening ?

Hi,

How can we achieve the same by using page break ? If it is possible then that will be an excellent approach because we can place bookmark at begining and end of page.

Please suggest the changes in above solution which is using paragraph for segregation.

Hi

Thanks for your request.

  1. I found the problem in my code. Please try using the following code.
//Open a document
Document doc = new Document(@"Test121\Demo.doc");
//Create document builder, it will be needed for building TOC
DocumentBuilder builder = new DocumentBuilder(doc);
//Get collection of Paragraphs from document
NodeCollection paragraphsCollection = doc.GetChildNodes(NodeType.Paragraph, true, false);
int tocItemIndex = 0;
int paragraphsCount = paragraphsCollection.Count - 1;
for (int paragraphIndex = paragraphsCount; paragraphIndex >= 0; paragraphIndex--)
{
    Paragraph par = (Paragraph)paragraphsCollection[paragraphIndex];
    //Check if paragraph is Heading
    if (par.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 ||
    par.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2 ||
    par.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading3)
    {
        //Create bookmark name
        string bookmarkName = "item_" + tocItemIndex.ToString();
        //Increase index
        tocItemIndex++;
        //Move cursor to the end of the current para
        builder.MoveTo(par);
        //Insert bookmark
        builder.StartBookmark(bookmarkName);
        builder.EndBookmark(bookmarkName);
        //Move cursor document start
        builder.MoveToDocumentStart();
        //Clear formating
        builder.ParagraphFormat.ClearFormatting();
        //Format font as hyperlink
        builder.Font.Underline = Underline.Single;
        builder.Font.Color = Color.Blue;
        //InsertTOC item
        builder.InsertHyperlink(par.ToTxt(), bookmarkName, true);
    }
}
//Save output document
doc.Save(@"Test121\out.html", SaveFormat.Html);
  1. Yes, I think you can find page break using Aspose.Words (I mean page break character “\f”) so you can use the following condition.
if (doc.FirstSection.Body.FirstParagraph.GetText().Contains("\f"))
{
    //Do something
}

But note that Aspose.Words document represents content and formatting of a document, not its layout into lines and pages. This feature is called pagination and it is not released yet.

Best regards.

Hi,

Thankx for your reply.

I tried this but it is still not working. Generated doc does not contail any link. I tried it with the same doc which I attached previously. When I executed this code

par.getParagraphFormat().getStyleIdentifier() returning 0 and par.toTxt() is giving empty string for each paragraph.

Please suggest how can I overcome this problem.

Hi

I can’t reproduce your issue on my side. I also tried this code in JAVA and all works fine on my side. Here is my code in java.

//Open a document
Document doc = new Document("Demo.doc");
//Create document builder, it will be needed for building TOC
DocumentBuilder builder = new DocumentBuilder(doc);
//Get collection of Paragraphs from document
NodeCollection paragraphsCollection = doc.getChildNodes(NodeType.PARAGRAPH, true, false);
int tocItemIndex = 0;
int paragraphsCount = paragraphsCollection.getCount() - 1;
for (int paragraphIndex = paragraphsCount; paragraphIndex >= 0; paragraphIndex--)
{
    Paragraph par = (Paragraph)paragraphsCollection.get(paragraphIndex);
    //Check if paragraph is Heading
    if (par.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_1 ||
    par.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_2 ||
    par.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_3)
    {
        //Create bookmark name
        String bookmarkName = "item_" + String.valueOf(tocItemIndex);
        //Increase index
        tocItemIndex++;
        //Move cursor to the end of the current para
        builder.moveTo(par);
        //Insert bookmark
        builder.startBookmark(bookmarkName);
        builder.endBookmark(bookmarkName);
        //Move cursor document start
        builder.moveToDocumentStart();
        //Clear formating
        builder.getParagraphFormat().clearFormatting();
        //Format font as hyperlink
        builder.getFont().setUnderline(Underline.SINGLE);
        builder.getFont().setColor(Color.blue);
        //InsertTOC item
        builder.insertHyperlink(par.toTxt(), bookmarkName, true);
    }
}
//Save output document
doc.save("out.html", SaveFormat.HTML);

Please make sure that you are using the latest version of Aspose.Words for java. IF you need I can send the generated document to your e-mail (if so please provide me your e-mail).

Best regards.

Hi,

Thankx for your reply.

My Email id is : ajay.sharma@lntinfotech.com

I am using demo version of aspose API for jdk 1.5. Please send generated document on my email id. Is this happening because I am using demo version of aspose API although I have taken one month license. If yes then please mention the location where I can download the latest version of Aspose.

I sent the document to your e-mail.

You can download the latest version of Aspose.Words for java from here.
https://releases.aspose.com/words/net/

Best regards.

Hi,

It worked. There was some mistake in my code. Now it is working fine. Thankx a lot for your help.