Insert table of contents to document

Hi Team,

I am generating TOC using below code…
private void generateTableOfContents(DocumentBuilder builder) throws Exception {
builder.getCurrentParagraph().getParagraphFormat().setStyleIdentifier(StyleIdentifier.TOC_HEADING);
builder.writeln(“Table of Contents”);

	//builder.insertTableOfContents("\\o \"1-5\" \\h \\z \\u");
	builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
	builder.insertBreak(BreakType.PAGE_BREAK);
	// Start the actual document content on the second page.
	builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}

I am able to generate TOC but TOC does not show “Table Of Contents text with Page number as first thing”.

Please see attached documents for more clarity and compare both.

Thanks,
Vaibhav DiwanTable of contents.zip (23.0 KB)

@diwanmail,

Please try using the following field code:

builder.InsertTableOfContents("\\o \"1-4\" \\h \\z");

No…result is same. I have attached expected output in my earlier post, please tell me the way to achieve this.

I want TABLE OF CONTENTS as heading and Table Of Contents with page number. Please see below expected output.

Table of Contents
Table of Contents 3
LIST OF ABBREVIATIONS, ACRONYMS, AND DEFINITION OF TERMS 5

@diwanmail,

The documents you attached earlier were empty i.e. there is no content other than TOC fields. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your actual input word document
  • Your expected document which shows the correct output
  • Aspose.Words generated output document which shows the undesired behavior

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