先上word文档
未生成目录前:前几页截图
1页2页
生成目录后:
仔细看上图,段落,上代码~
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
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 org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.awt.*;
import java.io.*;
import java.util.List;
public class PoiText {
public static void main(String[] args) throws InterruptedException, IOException, InvalidFormatException {
//加载测试文档
Document doc = new Document("D:\\myProject\\test\\src\\main\\resources\\templates\\test.docx");
//在文档最前面插入一个段落,写入文本并格式化
Paragraph parainserted = new Paragraph(doc);
TextRange tr = parainserted.appendText("目 录");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
doc.getSections().get(0).getParagraphs().insert(0, parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置文档中指定段落的大纲级别
//由于代码已经生成了目录这行文字占了文档的第一段,所以目录属于第一段
//基本信息这个段落属于文档的第5段,属于一级标题,(5+1)
doc.getSections().get(0).getParagraphs().get(6).applyStyle(BuiltinStyle.Heading_1);
//1.1患者基本信息这个段落属于文档的第6段,属于二级标题(6+1)doc.getSections().get(0).getParagraphs().get(7).applyStyle(BuiltinStyle.Heading_2);
//以此类推doc.getSections().get(0).getParagraphs().get(8).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(9).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(13).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(17).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(18).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(20).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(21).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(23).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(24).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(30).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(39).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(40).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(41).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(43).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(45).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(49).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(51).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(53).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(55).applyStyle(BuiltinStyle.Heading_2);
//添加目录
doc.getSections().get(0).getParagraphs().get(0).appendTOC(1, 3);
//更新目录表
doc.updateTableOfContents();
//保存文档
doc.saveToFile("AddToc2.docx", FileFormat.Docx_2010);
doc.close();
//去除Evaluation Warning: The document was created with Spire.Doc for JAVA
FileInputStream in = new FileInputStream("D:\\myProject\\test\\AddToc2.docx");
XWPFDocument docnew = new XWPFDocument(OPCPackage.open(in));
List<XWPFParagraph> paragraphs = docnew.getParagraphs();
XWPFParagraph firstParagraph = paragraphs.get(0);
if (firstParagraph.getText().contains("Spire.Doc")) {
docnew.removeBodyElement(docnew.getPosOfParagraph(firstParagraph));
}
OutputStream out = new FileOutputStream("D:\\myProject\\test\\AddToc2.docx");
docnew.write(out);
docnew.close();
}
}
文档位置随意在哪,写好路径就行,
目前只支持生成500页内的目录,待我优化。。。