目录是一种能够快速、有效地帮助读者了解文档或书籍主要内容的方式。在Word中,插入目录首先需要设置相应段落的大纲级别,根据大纲级别来生成目录表。本文中生成目录分2种情况来进行:

1.文档没有设置大纲级别,生成目录前需要手动设置

2.文档已设置大纲级别,通过域代码生成目录

使用工具:

  • Free Spire.Doc for Java (免费版)
  • IntelliJ IDEA

工具获取途径1:通过官网下载jar文件包,解压并导入jar文件到IDEA程序。

工具获取途径2:通过Maven仓库导入到Maven项目中,参考导入方法

Java示例代码(供参考)

【示例1】手动设置大纲级别并生成目录

import com.spire.doc.*;
import com.spire.doc.documents.BuiltinStyle;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*; public class AddToc {
public static void main(String[]args){
//加载测试文档
Document doc = new Document("test.docx"); //在文档最前面插入一个段落,写入文本并格式化
Paragraph parainserted = new Paragraph(doc);
TextRange tr= parainserted.appendText("目 录");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
doc.getSections().get().getParagraphs().insert(,parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //设置文档中指定段落的大纲级别
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get().getParagraphs().get().applyStyle(BuiltinStyle.Heading_3); //添加目录
doc.getSections().get().getParagraphs().get().appendTOC(,); //更新目录表
doc.updateTableOfContents(); //保存文档
doc.saveToFile("AddToc.docx",FileFormat.Docx_2010);
}
}

目录生成效果:

【示例2】已设置大纲级别,通过域代码直接生成目录

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TableOfContent;
import com.spire.doc.fields.TextRange; import java.awt.*; public class AddTOC2 {
public static void main (String[] args){
//加载已设置大纲级别的测试文档
Document doc = new Document("sample.docx"); //在文档最前面插入一个段落,写入文本并格式化
Paragraph parainserted = new Paragraph(doc);
TextRange tr= parainserted.appendText("目 录");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
doc.getSections().get().getParagraphs().insert(,parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //通过域代码添加目录表
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
doc.getSections().get().getParagraphs().get().appendTOC(,);
doc.updateTableOfContents(); //保存文档
doc.saveToFile("AddToc2.docx", FileFormat.Docx_2010);
}
}

目录生成效果:

PS:关于通过域代码生成目录,可参考这篇文章,获取更多目录设置方法

转载请注明出处!

(本文完)

Java 添加Word目录的2种方法的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. java字符串大小写转换的两种方法

    转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString {          pu ...

  3. C#中得到程序当前工作目录和执行目录的五种方法

    string str="";str += "\r\n" + System.Diagnostics.Process.GetCurrentProcess().Mai ...

  4. Java中创建数组的几种方法

    Java中创建数组的几种方法 public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intVa ...

  5. Java List转换为字符串的几种方法

    Java List转换为字符串的几种方法 import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import ...

  6. java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

    转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...

  7. Java遍历List集合的三种方法

    Java遍历List集合的三种方法 List<String> list = new ArrayList<String>(); list.add("aaa") ...

  8. (转)java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

    java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明);部分资料参考网络资源 1. java向MySQL插入当前时间的四种方式 第一种:将java.util.Date ...

  9. Java 读取clob字段的几种方法

    Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...

随机推荐

  1. Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

    目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationP ...

  2. spyder错误合集

    SyntaxError: invalid syntax是非法语句的意思,检查语法是否出现错误,漏写等   SyntaxError: (unicode error) 'unicodeescape' co ...

  3. IO - 同步 异步 阻塞 非阻塞的区别,学习Swoole有帮助

    同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?本文较长需耐心阅读,基础 ...

  4. Prometheus学习系列(九)之Prometheus 联盟、迁移

    前言 本文来自Prometheus官网手册 和 Prometheus简介 FEDERATION 允许Prometheus服务器从另一台Prometheus服务器抓取选定的时间序列. 一,用例 联盟有不 ...

  5. Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.1】

    使用Spring Boot 本节将详细介绍如何使用Spring Boot.它涵盖了诸如构建系统,自动配置以及如何运行应用程序之类的主题.我们还将介绍一些Spring Boot最佳实践.尽管Spring ...

  6. Go使用变量类型声明和方法的注意事项

    当我们通过把一个现有(非interface)的类型定义为一个新的类型时,新的类型不会继承现有类型的方法. 神马意思?来一段简短错误的代码: package main import "sync ...

  7. Flask 特殊装饰器

    请求进入函数之前 before_request # -*- coding: utf-8 -*-   from flask import Flask, session, redirect, reques ...

  8. document.write() 为什么会清空页面

    很久以前遇到的问题,放着放着就忘记去研究了最近看到一篇文章总结一下作者:abloumeurl:   http://blog.csdn.net/u013451157/article/details/78 ...

  9. 记一次在node.js中使用crypto的createCipheriv方法进行加密时所遇到的坑

    Node.js的crypto模块提供了一组包括对OpenSSL的哈希.HMAC.加密.解密.签名,以及验证等一整套功能的封装.具体的使用方法可以参考这篇文章中的描述:node.js_crypto模块. ...

  10. 升鲜宝V2.0_杭州生鲜配送行业,升鲜宝供应链B端订货系统使用说明_升鲜宝生鲜供应链管理系统_15382353715_余东升

    升鲜宝V2.0_杭州生鲜配送行业,升鲜宝供应链B端订货系统使用说明_升鲜宝生鲜供应链管理系统_15382353715             升鲜宝生鲜配送供应链系统经过这些年的发展,形成一套独特的订 ...