您好,我对一个文档的目录设置字体信息,但是目录的页码却总是设置不成功,请问是什么原因呢?
描述如图:我设置了字体为五号Arial,这里仍然是10号字体
代码如下:
public static void main(String[] args) throws Exception {
Document document = new Document("D:\\目录测试.docx");
FieldCollection fields = document.getRange().getFields();
List<Paragraph> paras=new ArrayList<>();
List<Run> runs=new ArrayList<>();
try {
for (Field field : fields) {
if (field.getType() == FieldType.FIELD_HYPERLINK) {
FieldHyperlink hyperlink = (FieldHyperlink) field;
//此超链接跳转的位置
if (hyperlink.getSubAddress() != null && hyperlink.getSubAddress().startsWith("_Toc")) {
Paragraph tocItemParagraph = (Paragraph) field.getStart().getAncestor(NodeType.PARAGRAPH);
paras.add(tocItemParagraph);
runs.addAll(Arrays.asList(tocItemParagraph.getRuns().toArray()));
}
}
}
}catch (Exception e) {
throw new Exception(e.getMessage());
}
for (Run run : runs) {
Font font = run.getFont();
font.setNameFarEast("宋体");
font.setNameAscii("Arial");
font.setSize(10.5);
font.setBold(false);
font.setItalic(false);
}
document.save("D:\\testResult.docx");
}
原文档:
目录测试.docx (31.4 KB)
设置后的文档:
testResult.docx (36.6 KB)