当前位置: 代码迷 >> 综合 >> spire.doc.free2.7.3生成目录后修改目录的字体
  详细解决方案

spire.doc.free2.7.3生成目录后修改目录的字体

热度:31   发布时间:2024-02-10 10:33:00.0

在看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);