//加载html 文档
Document document = new Document();
document.loadFromFile("html.html", FileFormat.Html, XHTMLValidationType.None);
//根据标题在文档最开始生成目录
Paragraph parainserted = new Paragraph(document);
TextRange tr= parainserted.appendText("目 录");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
document.getSections().get(0).getParagraphs().insert(0,parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
document.getSections().get(0).getParagraphs().get(0).appendTOC(1,4);
document.updateTableOfContents();
//保存
document.saveToFile("out.docx", FileFormat.Docx);