简体   繁体   English

Java如何在docx中创建具有不同样式的三个目录?

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

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? 如何在docx文件中创建三个或更多目录,其中一个目录用于标题级别1、2、3,其他目录用于程序创建的另一种样式? 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. 我希望这些TOC不在文件的末尾放在特殊的段落中。

Which one is better to do this, Apache-poi? Apache-poi哪个更好地做到这一点? docx4j ? docx4j? Aspose? 阿瑟

I write my other code with apache-poi. 我用apache-poi编写其他代码。

Using Aspose.Words for Java, you can use insertTableOfContents() method with required switches parameter to add TOC field in Word document. 使用Aspose.Words for Java,可以将insertTableOfContents()方法与必需的switchs参数一起使用,以在Word文档中添加TOC字段。 You can add as many TOC fields as required. 您可以根据需要添加任意多个TOC字段。 Following code adds three different TOC fields with different styles. 以下代码添加了具有不同样式的三个不同的TOC字段。

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. 指定开关的最简单方法是使用“插入”->“参考”->“索引和表”菜单将目录插入并配置到Word文档中,然后打开显示域代码以查看开关。 You can press Alt+F9 in Microsoft Word to toggle display of field codes on or off. 您可以在Microsoft Word中按Alt + F9来打开或关闭域代码的显示。

For example, after creating a table of contents, the following field is inserted into the document: { TOC \\o "1-3" \\h \\z \\u }.\u003c/i> 例如,创建目录后,将以下字段插入到文档中:{TOC \\ o“ 1-3” \\ h \\ z \\ u}。 You can copy \\o "1-3" \\h \\z \\u and use it as the switches parameter. 您可以复制\\ o“ 1-3” \\ h \\ z \\ u并将其用作开关参数。

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. 请注意,insertTableOfContents方法仅添加TOC字段,要填充TOC字段,您需要在代码中调用updateFields()方法或在MS Word中按F9。

// 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. 我与Aspose合作,担任开发人员福音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM