Insert Table Of Contents (TOC Field) pointing to Heading Paragraphs after Section/Page Breaks in Word DOCX Document C# .NET

Hi I want the develop functionality like below in c#

TOC-
Personal Info…1
About me…2
Address Info…4
Educational Info…5

on next page
Personal Info
this is test text for sample only…
some text here

end of personal info
Next page
About me
this is test about me text
conti…on page 2-3
last of page 3
end of about me
Address Info

so and so on

i have tried TOC of asposeword+c# but i did not get my solution
the output was
Personal Info…1
About me…2
Address Info…4
Educational Info…5

on next page
Personal Info
About me
Address Info
Educational Info

@awadhesh_jha,

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

  • Your simplified input Word document
  • Aspose.Words for .NET 20.8 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words for .NET.

TOC.zip (8.2 MB)

Hi
I have gave you sample of code here.
i also send the document that have been generated.

it should be
Heading of para
paragraph here…

please advice as soon as posible.

@awadhesh_jha,

You can build logic on the following code to get the desired output (DOCX containing TOC.zip (7.6 KB)):

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Page ");
builder.InsertField(FieldType.FieldPage, true);
builder.PageSetup.RestartPageNumbering = true;
builder.MoveToDocumentEnd();

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Personal Info");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("some text");
builder.InsertBreak(BreakType.PageBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("About me");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("some text");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("again some text");
builder.InsertBreak(BreakType.PageBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Address Info");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("some text");
builder.InsertBreak(BreakType.PageBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Educational Info");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("some text");
builder.InsertBreak(BreakType.PageBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("Rest of the document content");

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