Custom style is not working for Table of content, Table of figures

Hi,
I am working on creating table of content (TOC) and creating table of Figures(TOF).
I am using aspose-words-18.5-jdk16 api for java.

I want TOC, in following format

1.TEST…1
1.1.1 Description…1
1.1.2 Where Used…2

2.SECTION 2… 3
2.1.1 Description…3
2.1.2 Where Used…3

3.SECTION 3…4
3.1.1 Description…4
3.1.2 Where Used…4

Resulting TOC
output.png (12.8 KB)

Expected TOC
expected .png (11.9 KB)

I have used custom style code, but it is not working. I have attached project please check.

com.aspose.words.zip (2.4 MB)

I am also facing problem regarding Table Of Figure (TOF) and Table Of Table. Please help me creating those also.
To create TOF, I am using following code

      -builder.InsertField(" \\h \\z \\c \"Picture\"", null);

      -builder.InsertField(" \\o \"1-3\" \\h \\z \\u", null);

But none of them is working.

@ngshinde999,

Please also ZIP and attach your expected Word document (DOCX file) showing the correct final output here for our reference. Please create this expected document by using MS Word. We will then investigate the structure of your expected document as to how you want your final output be generated like. Thanks for your cooperation.

Thanks for your reply,
I have attached Sample Expected docx. Please check TOC (table of content) and TOF (table of figure) and TOT (table of tables)

The original DOCX has same TOC, TOF and TOT as in sample docx. Please check it.

Expected Doc.zip (332.5 KB)

@ngshinde999,

But, please open this “Expected Doc.docx” with MS Word 2016. Right click on “Table of Contents” or “Table of Figures” and choose “Update Field” then “Update entire table” and then press OK. This will return the layout of “Table of Contents” or “Table of Figures” back to their original state. Please provide expected document where MS Word’s Update Field command does not have any effect on the layout of TOC fields.

Hi,
I have attached the Expected Docx. Please check it.

AS6091.zip (4.3 MB)

@ngshinde999,

But using MS Word 2016, if you copy the TOC field codes (TOC \o “1-3” \h \z \u ) from “AS6091.docx” to “sample.docx”, the output is still not desirable. One way to workaround this issue is as follows:

Document docA = new Document("D:\\temp\\Expected Doc\\AS6091.docx");
Document docB = new Document("D:\\temp\\Expected Doc\\sample.docx");

docA.removeAllChildren();
docA.appendDocument(docB, ImportFormatMode.USE_DESTINATION_STYLES);

DocumentBuilder builder = new DocumentBuilder(docA);
builder.moveToDocumentStart();
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u ");

docA.updateFields();

docA.save("D:\\Temp\\Expected Doc\\awjava-18.5.docx");

Hi Awais,

Thanks for reply,

The solution you provided will not work in our case, because in future, we will not have source DOCX ( e.g.AS6091.docx) always.

  1. Is there any other way to create custom Table of Content (AS6091.zip) through Aspose API?

  2. We also want to create Table of Figures and Table Of Tables and apendix.

TOC.png (23.4 KB)

@ngshinde999,

As a workaround, you can create a Template document to use with such problematic documents (see template.zip (18.4 KB)).

And you can insert Table of Figures and Appendix by using the following code:

Document docA = new Document("D:\\temp\\Expected Doc\\template.docx");
Document docB = new Document("D:\\temp\\Expected Doc\\sample.docx");

docA.appendDocument(docB, ImportFormatMode.USE_DESTINATION_STYLES);

DocumentBuilder builder = new DocumentBuilder(docA);
builder.moveToDocumentStart();
builder.insertTableOfContents(" \\o \"1-3\" \\h \\z \\u ");
builder.writeln();
builder.insertTableOfContents(" \\h \\z \\t \"Heading 7,8,Heading 8,9,Appendix,7\" ");

docA.updateFields();

docA.save("D:\\Temp\\Expected Doc\\awjava-18.5.docx");

Basically to simplify, you can build a Table of Content field by using MS Word, Toggle Fields, right click on TOC field and choose Edit Field. Then choose Field Codes to get TOC field code (see screenshot). You can simply pass this field code to DocumentBuilder.insertTableOfContents method.

Thanks you very much…,

It is working properly.

Can you please share the steps to create template, so we can create new template for us.

@ngshinde999,

The “template.docx” was generated from “AS6091.docx” by using the following code:

Document doc = new Document("D:\\temp\\Expected Doc\\AS6091.docx");
doc.removeAllChildren();
doc.ensureMinimum();
doc.save("D:\\Temp\\Expected Doc\\template.docx");