How create three TOC with diffrent styles in docx by java?

不打扰是莪最后的温柔 提交于 2019-12-11 15:14:50

问题


How can I create three or more TOC in docx file that one of them is for Headings level 1, 2, 3 and others are for another styles which are created by program? For example, I create a style for table title and I want to create a TOC for paragraphs with this style. And I want these TOCs to be in special paragraphs not at the end of the file.

Which one is better to do this, Apache-poi? docx4j ? Aspose?

I write my other code with apache-poi.


回答1:


Using Aspose.Words for Java, you can use insertTableOfContents() method with required switches parameter to add TOC field in Word document. You can add as many TOC fields as required. Following code adds three different TOC fields with different styles.

The easiest way to specify the switches is to insert and configure a table of contents into a Word document using the Insert->Reference->Index and Tables menu, then switch display of field codes on to see the switches. You can press Alt+F9 in Microsoft Word to toggle display of field codes on or off.

For example, after creating a table of contents, the following field is inserted into the document: { TOC \o "1-3" \h \z \u }. You can copy \o "1-3" \h \z \u and use it as the switches parameter.

Please note insertTableOfContents method only adds TOC field, to populate TOC filed you need to call updateFields() method in code or press F9 in MS Word.

// Use a blank document
com.aspose.words.Document doc = new com.aspose.words.Document();

// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a table of contents at the beginning of the document.
//TOC for Heading 1,2 and 3 styles
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
//TOC for specific style e.g. Heading 2
builder.insertTableOfContents("\\h \\z \\t \"Heading 2,1\"");
//TOC for specific style e.g. Heading 3
builder.insertTableOfContents("\\h \\z \\t \"Heading 3,1\"");


// Start the actual document content on the second page.
builder.insertBreak(BreakType.PAGE_BREAK);

// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

builder.writeln("Heading 1");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

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

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

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

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

builder.writeln("Heading 3.1");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3);

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

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

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

// Call the method below to update the TOC.
doc.updateFields();
doc.save("Sample_out_1710.docx");

I work with Aspose as developer Evangelist.



来源:https://stackoverflow.com/questions/46759829/how-create-three-toc-with-diffrent-styles-in-docx-by-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!