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

Free Support Forum - aspose.com

Selecting TOC content as per custom tag in Aspose word

I am using Aspose word 18.9. We know that the default way to insert Table of content is :

builder.InsertTableOfContents("\o “1-3” \h \z \u");

Here Only H1-H3 tags will be considered for TOC. But Header tags create extra spaces in the content which I don’t want.

So I want to use user-defined tag for TOC. Currently I have created user-defined tag toc .

For example:

<toc>Heading 1</toc>
<toc>Heading 2</toc>

I want this tag to be used to build the TOC.

Is there any way to do so?

Please help!


Please note that InsertTableOfContents will only insert a TOC field, but will not actually build the table of contents. The table of contents is built by Microsoft Word when the field is updated.

You can use hyperlink to bookmark feature to achieve your requirement. Please check the following code example. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Bookmarked text");

// Insert a hyperlink with a document builder
FieldHyperlink field = (FieldHyperlink)builder.InsertField(FieldType.FieldHyperlink, true);
field.Result = "Link to bookmark";
field.SubAddress = "bookmark";
doc.Save(MyDir + "output.docx");

Hi Tahir,

Thanks for quick reply. I am sorry I didn’t mention, but I am creating word using Html String. So the H1 - H6 tags are being considered by default. Can your solution work with Html content?


You can ignore the heading style in TOC field e.g. { TOC \h \z \t “custom_style,1” }.

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word and HTML documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.