在看spire.doc.free官网API文档时,没有找到修改目录字体的方法
只有生成目录的方法,但是生成的是西文字体,我专门去问了他们官网留下的技术人员的QQ,询问到了修改目录字体的使用代码
就是再次加载生成了目录后的文档,遍历section/body中的元素,进行目录字体的修改,然后再生成一个word文档,功能非常好用,非常感谢技术人员的知道,但是不知道为什么他们不在官网API文档放这个方法,我找了一上午都没找见
com.spire.doc.Document doc1 = new com.spire.doc.Document(pathDoc);
doc1.loadFromFile(pathDoc);
// //循环遍历section/body 中的元素
for (int s = 0; s < doc1.getSections().getCount(); s++) {
Section section = doc1.getSections().get(s);
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
DocumentObject obj = section.getBody().getChildObjects().get((i));
FieldCollection field = doc1.getFields();
for (int m = 0; m < field.getCount(); m++) {
Field fd = field.get(m);
//判断是否含有域
if (obj.getChildObjects().contains(fd)) {
Paragraph para = (Paragraph) obj;
for (int j = 0; j < para.getChildObjects().getCount(); j++) {
DocumentObject cObj = para.getChildObjects().get(j);
if (cObj instanceof TextRange) {
TextRange tr1 = (TextRange) cObj;
//修改textrange 的字体
tr1.getCharacterFormat().setFontName("宋体");
}
}
}
}
}
}
doc1.saveToFile(pathDoc, FileFormat.Docx_2010);
本文档介绍如何在生成目录后使用Spire.Doc.Free修改目录字体。通过与官方技术支持交流,了解到需要再次加载文档,遍历并修改section/body中的元素来实现,这种方法虽然有效,但未在官方API中明确说明。
3万+

被折叠的 条评论
为什么被折叠?



