We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert TOC headers with InsertHTML

I am building a document with ASPose.Words .NET. Starting with a .doc template file that contains headers and a table contents, we insert text in several sections using the InsertHTML function. I am trying to have sub-headers in the HTML text that later appear in the Table of Contents when UpdateFields() and UpdatePageLayout() is called. I have tried

,

, and other tags, but they do not appear in the TOC. Is there any way for me to have content in the HTML that later appears as needed in the Table of Contents?

Hi there,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.


  • Please attach your input Word document and Html.
  • Please

    create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Here is a working sample application that contains the template files and the HTML. Please note that this sample exhibits another problem that exporting to .doc or .docx format does not work properly. Only the .pdf file displays.

Hi there,

Thanks for sharing the detail. We have tested the scenario using latest version of Aspose.Words for .NET 15.12.0 and have not found the shared issue. Please use Aspose.Words for .NET 15.12.0. We have attached the output documents with this post for your kind reference.


I can see the sub-headers in the PDF version. Is there a way to fix the spacing so that the empty line is after the sub-headers instead of before it? See attachment

Hi there,

Thanks for your inquiry.


Please note that Aspose.Words mimics the same behavior as MS Word does. When you press F9 in MS Word or call Document.UpdateFields using Aspose.Word, the TOC is generated/updated.

A table of contents in a Word document can be built in a number of ways and formatted using a variety of options. The way the table is built and displayed by Microsoft Word is controlled by the field switches. For example, after creating a table of contents, the following field is inserted into the document: { TOC \o “1-3” \h \z \u }.
We suggest you please read following article for your kind reference.
How-to Insert and Work with the Table of Contents Field

In your case, we suggest you please use the following workaround. Hope this helps you. Please use this code after calling Document.UpdateFields. Hope this helps you.

doc.UpdateFields();<o:p></o:p>

foreach (FieldStart fstart in doc.GetChildNodes(NodeType.FieldStart, true))

{

if (fstart.FieldType == Aspose.Words.Fields.FieldType.FieldPageRef)

{

String fieldText = fstart.GetField().GetFieldCode();

if (fieldText.Contains("_Toc"))

{

fstart.ParentParagraph.ParagraphFormat.LineSpacing = 12;

}

if (fieldText.Contains("_Toc") &&

fstart.ParentParagraph.ToString(SaveFormat.Text).Trim().StartsWith("Sub Header"))

{

fstart.ParentParagraph.ParagraphFormat.LineSpacing = 24;

fstart.ParentParagraph.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;

}

}

}

doc.Save(MyDir + "Out.docx");

doc.Save(MyDir + "Out.pdf");

That code snip-it works great! Is it be possible to have similar code that loops through the table of contents and changes text or adds entries? I am particularly interested in moving to the end and adding some appendix pages that I will be adding later.

Hi there,

Thanks for your inquiry. In your case, we suggest you please insert the contents at the end of document first and then call Document.UpdateFields method before saving the final document. Hope this helps you.