你好,我在Windows环境下将生成的原wordwin-原.docx (1.1 MB)添加目录得到得wordwin-得.docx (1.1 MB)
我在Linux环境下将生成的原wordlinux-原.docx (1.1 MB)添加目录得到linux-得.docx (1.1 MB)
发现在Linux服务器上页码对应不上对比图.png (46.2 KB)
我生成目录的方法如下
public static void creat(String resourceWordPath, String targetWordPath, String pageHeader) throws Exception
{
Document doc = new Document(resourceWordPath);
DocumentBuilder builder = new DocumentBuilder(doc);
doc.getFirstSection().getBody().prependChild(new Paragraph(doc));
//将光标移到文档开始的位置
builder.moveToDocumentStart();
// 设置目录的格式
// “目录”两个字居中显示、加粗、宋体
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.setBold(true);
builder.getFont().setName("宋体");
builder.getFont().setSize(16);
builder.writeln("目 录");
// 清清除所有样式设置
builder.getParagraphFormat().clearFormatting();
// 目录居左
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
// 插入目录,这是固定的
builder.insertTableOfContents("\\o \"1-4\" \\h \\z \\u");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// 添加目录和正文的页眉页脚
addHeaderFooter(builder, pageHeader);
// 更新域
doc.updateFields();
// 1、给目录设置相应的样式
for (Field field : doc.getRange().getFields())
{
if (field.getType() == (FieldType.FIELD_PAGE_REF))
{
FieldPageRef pageRef = (FieldPageRef) field;
if (!Objects.equals(pageRef.getBookmarkName(), "") || pageRef.getBookmarkName().startsWith("_Toc"))
{
Paragraph tocItem = (Paragraph) field.getStart().getAncestor(NodeType.PARAGRAPH);
tocItem.getParagraphFormat().setLineSpacing(15);
for (Run run : tocItem.getRuns())
{
run.getFont().setName("宋体");
run.getFont().setSize(12);
run.getFont().setBold(false);
}
}
}
}
// 2、锁定目录,避免手动更新目录后,格式失效
for (Field field : doc.getRange().getFields())
{
if (field.getType() == (FieldType.FIELD_TOC))
{
field.isLocked(true);
}
}
doc.save(targetWordPath);
}
其中添加页码页脚方法:
public static void addHeaderFooter(DocumentBuilder builder, String pageHeader)
{
try
{
SectionCollection sections = builder.getDocument().getSections();
for (int i = 2; i < sections.getCount(); i++)
{
Section section1 = sections.get(i);
// 页码格式: https://reference.aspose.com/words/java/com.aspose.words/NumberStyle
section1.getPageSetup().setPageNumberStyle(NumberStyle.ARABIC);
section1.getPageSetup().setRestartPageNumbering(false);
}
// 正文设置页眉页脚
Section section = builder.getCurrentSection();
// https://forum.aspose.com/t/wod/204805/2
// 在该部分的开头重新开始页码,则PageSetup.RestartPageNumbering返回true
// 如果其值设置为false,则RestartPageNumbering属性将覆盖PageStartingNumber属性,以便可以从上一节继续进行页码编号
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageNumberStyle(NumberStyle.ARABIC);
section.getHeadersFooters().linkToPrevious(false);
// 页码
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// 设置页脚与页面底部之间的距离(以磅为单位)
builder.getPageSetup().setFooterDistance(50);
// FieldType.FIELD_PAGE
builder.insertField("Page");
// 页眉
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.getFont().setSize(9);
// 设置页眉和页面顶部之间的距离(以磅为单位)
builder.getPageSetup().setHeaderDistance(40);
builder.write(pageHeader);
// 目录页眉
builder.moveToSection(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.getFont().setSize(9);
builder.getPageSetup().setHeaderDistance(40);
builder.write(pageHeader);
}
catch (Exception e)
{
e.printStackTrace();
}
}
生成目录
public static void main(String[] args) throws Exception
{
creat("D:/word/原.docx", "D:/word/得.docx");
}