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

Free Support Forum - aspose.com

Format Table of Content and Add TabStop to Paragraph using .NET

I need to create a TOC.
I have collection of the chapters (name and number of its page).
How can I create a TOC manually by adding only the header name and page number?
Also, is there any way for me to change the text formatting for headings? thanks for the answer

I have 3 chapters

  • Chapter Qwerty 1 (page 1)
  • Chapter Qwerty 1.2 (page 2)
  • Chapter Asd 2.1 (page 5)
    And I want to create TOC like this:

image.png (3.0 KB)


Please use DocumentBuilder.InsertTableOfContents method to insert a TOC (table of contents) field into the document. For more detail, please refer to the following article.

To change the text formatting, you need to modify the TOC1, TOC2, etc. styles to achieve your requirement. Please check the following code example.

Document doc = new Document(MyDir + "input.docx");
doc.Styles[StyleIdentifier.Toc1].Font.Size = 11;
doc.Styles[StyleIdentifier.Toc1].Font.Name = "Arial";
TabStop tabstop = doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.TabStops[0];
tabstop.Leader = TabLeader.Line;

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

Thanks for the answear.
Of course I tried the examples from the above article.
This example led me to the following output:

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.Writeln("Heading 1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.Writeln("Heading 2");
builder.Writeln("Heading 3");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

builder.Writeln("Heading 3.1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;

builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

builder.Writeln("Heading 3.2");
builder.Writeln("Heading 3.3");


image.png (12.3 KB)

You can see that I have two pages, instead of one with TOC.
And I want to manage the page numbers but in the examples they are generated automatically.
How can I change page numbers manually?
And how can I prevent the second page from the screenshot from appearing?
Thanks for the answer!


Please remove following line of code from the code example to get the desired output.


The table of contents is built by Microsoft Word when the field is updated.

Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Thank you for the anwear!
Attached a file to the message.
The basic idea is as follows:
I know about names and pages. I only need to manually add it all from scratch to one file that will only contain the TOC.

TOC sample.docx (23.3 KB)


In your document, you are creating paragraphs with page numbers. You can write text into document using DocumentBuilder.Writeln method. Please refer to the following article.

To get the page number of paragraph e.g. following paragraph, you can use LayoutCollector.GetStartPageIndex method.
Chapter 1: Corrosion Behaviour of Carbon and Low Alloy Carbon Steels

Moreover, you do not need to write each paragraph and page numbers into document. Please change the paragraph style to Heading style e.g. ‘Heading 1’, ‘Heading 2’ etc. and insert table of content field at the start of document.

I tried to work with paragraphs.
The only problem was that I don’t know how to use indent with dots and the page number at the end. Perhaps you can tell me how I can do this?
image.png (19.8 KB)


You need to add tab stop to paragraph as shown below to get the desired output. Hope this helps you.

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

builder.Font.Bold = true;
builder.Write("A Case Study on Inner Corrosion of 3%Cr Casing Steel in the CO2 Environments");
builder.Font.Bold = false;
builder.Write(" L. Xu, S.Y. Li and M.B. Xu");

TabStopCollection tabStops = builder.ParagraphFormat.TabStops;
tabStops.Add(new TabStop(432.0, TabAlignment.Right, TabLeader.Dots));

builder.Writeln(ControlChar.Tab + "3");

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

Thanks a lot!
It helped me.


Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.