java-如何通过java在docx中创建三个具有不同样式的目录 [英] How create three TOC with diffrent styles in docx by java?

查看:290
本文介绍了java-如何通过java在docx中创建三个具有不同样式的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在docx文件中创建三个或更多目录,其中一个目录用于标题级别1、2、3,其他目录用于程序创建的另一种样式?例如,我为表标题创建样式,并且我想为具有此样式的段落创建目录. 我希望这些TOC不在文件的末尾放在特殊的段落中.

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.

Apache-poi,哪个做得更好? docx4j?摆姿势?

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

我用apache-poi编写其他代码.

I write my other code with apache-poi.

推荐答案

使用Aspose.Words for Java,可以将insertTableOfContents()方法与必需的switchs参数一起使用,以在Word文档中添加TOC字段.您可以根据需要添加任意多个TOC字段.以下代码添加了具有不同样式的三个不同的TOC字段.

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.

指定开关的最简单方法是使用插入"->参考"->索引和表格"菜单将目录插入并配置到Word文档中,然后打开显示域代码以查看开关.您可以在Microsoft Word中按Alt + F9来打开或关闭域代码的显示.

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.

例如,在创建目录之后,将以下字段插入到文档中:{TOC \ o"1-3" \ h \ z \ u}.您可以复制\ o"1-3" \ h \ z \ u并将其用作switchs参数.

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.

请注意insertTableOfContents方法仅添加TOC字段,要填充TOC字段,您需要在代码中调用updateFields()方法或在MS Word中按F9.

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");

我与Aspose合作,担任开发人员推广者.

I work with Aspose as developer Evangelist.

这篇关于java-如何通过java在docx中创建三个具有不同样式的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆