|
|
package org.example;
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import com.aspose.words.*;
|
|
|
|
|
|
import static org.example.AsposeUtil.getLicense;
|
|
|
|
|
|
//测试生成Word文档的目录成功!2022-12-25
|
|
|
public class UpdateFields {
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
//注册信息
|
|
|
getLicense();
|
|
|
|
|
|
//创建目录
|
|
|
String dataDir = "C:\\Work\\Athena\\Test\\src\\main\\resources\\";
|
|
|
Document doc = new Document();
|
|
|
DocumentBuilder builder = new DocumentBuilder(doc);
|
|
|
|
|
|
//写目录两个字
|
|
|
builder.getFont().setSize(48);
|
|
|
builder.getFont().setName("黑体");
|
|
|
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);// 单倍行距 = 12 , 1.5 倍 = 18
|
|
|
builder.getParagraphFormat().setSpaceAfter(0);//段后
|
|
|
builder.writeln("目录");
|
|
|
|
|
|
|
|
|
//还原
|
|
|
builder.getFont().setSize(14);
|
|
|
builder.getFont().setName("宋体");
|
|
|
|
|
|
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
|
|
|
builder.writeln();
|
|
|
|
|
|
//全局宏定义,可以获取共多少页,当前第几页,今天日期等信息
|
|
|
builder.write("Page: ");
|
|
|
builder.insertField("PAGE");
|
|
|
builder.write(" of ");
|
|
|
builder.insertField("NUMPAGES");
|
|
|
builder.writeln();
|
|
|
builder.write("Date: ");
|
|
|
builder.insertField("DATE");
|
|
|
|
|
|
// 在第二页插入文档内容
|
|
|
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
|
|
|
|
|
|
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
|
|
|
builder.writeln("一级标题");
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
|
|
|
builder.writeln("二级标题 1.1");
|
|
|
builder.writeln("二级标题 1.2");
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
|
|
|
builder.writeln("一级标题 2");
|
|
|
builder.writeln("一级标题 3");
|
|
|
|
|
|
// Move to the next page.
|
|
|
for (int i = 3; i <= 13; i++) {
|
|
|
// builder.insertBreak(BreakType.PAGE_BREAK);
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
|
|
|
builder.writeln("标题 " + i + ".1");
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3);
|
|
|
builder.writeln("标题 " + i + ".1.1");
|
|
|
builder.writeln("标题 " + i + ".1.2");
|
|
|
builder.writeln("标题 " + i + ".1.3");
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
|
|
|
builder.writeln("标题 " + i + ".2");
|
|
|
builder.writeln("标题 " + i + ".3");
|
|
|
|
|
|
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT);
|
|
|
// 修改字体大小
|
|
|
double old = builder.getFont().getSize();
|
|
|
|
|
|
builder.getFont().setSize(18);
|
|
|
builder.getFont().setName("仿宋");
|
|
|
builder.getParagraphFormat().setLineSpacing(18);// 单倍行距 = 12 , 1.5 倍 = 18
|
|
|
builder.getParagraphFormat().setSpaceAfter(0);//段后
|
|
|
|
|
|
String content = FileUtil.readUtf8String(dataDir + "ChineseWord.txt");
|
|
|
builder.writeln(content);
|
|
|
|
|
|
builder.getFont().setName("宋体");
|
|
|
builder.getFont().setSize(old);
|
|
|
}
|
|
|
System.out.println("Updating all fields in the document.");
|
|
|
|
|
|
// Call the method below to update the TOC.
|
|
|
doc.updateFields();
|
|
|
doc.save(dataDir + "Document Field Update Out.docx");
|
|
|
System.out.println("Fields updated in the document successfully.");
|
|
|
|
|
|
//测试保存为PDF文档格式
|
|
|
doc.save(dataDir + "Document Field Update Out.pdf", SaveFormat.PDF);
|
|
|
}
|
|
|
} |