About the issue of automatically generating custom format TableOfContents

It’s a bit difficult,About the issue of automatically generating custom format TableOfContents
My problem:
1 automatically generates a level 3 contents;
2 first-level contents font bold, 5 font size;
3 Secondary and third contents fonts are number 5 fonts, not bold, indented by 2 and 4 characters respectively

@lovecomputer It is not required to create TOC1-TOCN styles. These styles are built-in and can be accessed by style identifier. Please try using the following code:

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\t");
builder.InsertBreak(BreakType.SectionBreakNewPage);
Aspose.Words.Style toc1Style = doca.Styles[StyleIdentifier.Toc1];
toc1Style.Font.Bold = true;
toc1Style.Font.Size = 5;
toc1Style.ParagraphFormat.LeftIndent = 0;
Aspose.Words.Style toc2Style = doca.Styles[StyleIdentifier.Toc2];
toc2Style.Font.Bold = false;
toc2Style.Font.Size = 5;
toc2Style.ParagraphFormat.LeftIndent = 2;
Aspose.Words.Style toc3Style = doca.Styles[StyleIdentifier.Toc3];
toc3Style.Font.Bold = false;
toc3Style.Font.Size = 5;
toc3Style.ParagraphFormat.LeftIndent = 4;

According to the above code, only part of the requirements are implemented, and the secondary Contents and the third-level Contents want the font not to be bold, and the second-level Contents is indented by 2 characters, and the third-level Contents is indented by 4 characters is not realized, I want the catalog font to be number 5, but the actual display is a size 8 font.see the red line part marked in the attached figure

@lovecomputer This occurs because heading paragraphs in your document has the outline level 1. To get the expected result, the second level heading should have the second outline level, the third should have the third outline level.

@lovecomputer You can modify formatting of TOC levels by modifying the appropriate styles as mentioned above, i.e. StyleIdentifier.Toc1, StyleIdentifier.Toc2, StyleIdentifier.Toc3 etc.

Can it help delete word file in our conversation because there are some sensitive information in it, such as In.docx and out.docx file, thank you
https://forum.aspose.com/t/about-the-issue-of-automatically-generating-custom-format-tableofcontents/271441/4

I want to add a table of contents section in front of the body through code, and after execution, I find that the first line of ordinal in the body is gone? Here is my code:

Section section = new Section(doc);
doca.Sections.Insert(0, section);
builder.MoveToSection(doca.Sections.Count - 2);
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

this is test filetestFile.Docx (77.3 KB)
this is out test fileOuttestFile.Docx (68.1 KB)
The red font in the drawings indicates the difference between the two files, and the output file loses its serial number

@lovecomputer

I have removed the attached documents. Please note it is safe to attach files in the forum, only you as a topic starter and Aspose staff can see the attachments.

Please try using the following code to insert TOC:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Section section = new Section(doc);
section.EnsureMinimum();
doc.PrependChild(section);
builder.MoveToDocumentStart();
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

Why is the spacing of the multi-level numbering in the file different from the text that follows? For example, the spacing between the number and the text of the third-level heading is greater than the spacing between the number and the text of the second-level heading; The spacing between the numbering and the text of the fourth-level heading is also greater than that between the numbering and the text of the second-level heading; That is, the spacing between 2.3.1 and the following text ‘Test the titile2.3.1’ is greater than the spacing between 2.3 and ‘Test the title2.3’,
, I hope that the spacing size of the red part is the same

@lovecomputer This distance is controlled by list level tab position. If this position is less than list label, then default tab position is used. You can use ListLevels.TabPosition to configure the tab.

Why the number in the first level title in the contents and the following Chinese and English spacing is the same; However, the numbers in the secondary title and the following Chinese and English spacing are inconsistent, and the numbers in the tertiary title and the following Chinese and English spacing are also inconsistent.
, the spacing between red parentheses 1 and 2 is consistent; Red parentheses 3 and red parentheses 4 have different spacing; Red parentheses 5 and red parentheses 6 are also spaced differently
Also: Use ListLevels.TabPosition to set the spacing, which refers to the length of all numbered characters to the front end of the string, or the length of the last numbered character to the front end of the string

@lovecomputer the difference is caused by Allow Latin text to wrap in the middle of a word option, which is disabled for these paragraphs:

If enable this option, the spaces are consistent.

I inserted table of contents through the following code, my question:
How to control the size of the interval between the ordinal number and the text in the contents ?
Document doc = new Document(@“d:\testContents.Docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = new Section(doc);
section.EnsureMinimum();
doc.PrependChild(section);
builder.MoveToDocumentStart();

builder.InsertTableOfContents("\o “1-3” \h \z \u");
doc.UpdateFields();
doc.Save(@“d:\out.docx”);

thanks!

@lovecomputer The distance is controlled by tab stops defined in the TOC styles, if they are not defined in the TOC styles, then default tab stop is used:

You can access tab stopsvia 1. ParagraphFormat.TabStops property

I generate a table of contents with 3 level headings, I want to achieve no indentation on the left side of the primary title, the left indent position of the secondary title is aligned with the text part of the primary title, as shown in the figure red line to show the part
, the left indent position of the level 3 title is aligned with the text part of the secondary title, as shown in the figure blue line to illustrate the part
I see that many book contents are handled like this, I don’t know how to achieve it?
thanks!

@lovecomputer Could you please create the expected output document in MS Word and attach it here for our reference? We will check it and provide you more information.

My current challenge is that the third-level headings in the table of contents need to align both with the second-level headings in the format of x.x (2.9) and with the second-level headings in the format of x.xx (2.12) because they have different lengths…targetContents.Docx (76.4 KB)

@lovecomputer I am afraid there is no way to achieve this automatically. In the document you have attached, the 2.12.1 Test the titile2.3.1 item in the TOC is shifted right synthetically, if update TOC the item is shifted left and is lined with 2.9.1 Test the titile2.3.1 item according to TOC3 style.