最近使用Java Apache POI对word进行编辑,需要实现一段落两种字体样式,但是无论怎么修改就只能出现一种字体,有没有知道怎么修改的
public static void createContentLine2(XWPFRun run, List<String> textList){
run.setFontSize(16);
for(int i=0; i<textList.size(); i++){
String val = textList.get(i);
if(val.contains(":")){
// 新建 字体配置对象
//中文
int index = val.indexOf(":")+1;
if(index==val.length()){
run.setText(val);
run.setFontSize(16);
run.setFontFamily("黑体");
}else {
String str1 = val.substring(0, index);
run.setText(str1);
CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
fonts.setAscii("黑体");
fonts.setEastAsia("黑体");
fonts.setHAnsi("黑体");
//run.setFontFamily("黑体", XWPFRun.FontCharRange.eastAsia);
String str2 = val.substring(index);
//run.setText(mapkey+String.valueOf(i));
run.setText(str2);
rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();
fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
fonts.setAscii("仿宋");
fonts.setEastAsia("仿宋");
//fonts.setHAnsi("仿宋");
//run.setFontFamily("仿宋", XWPFRun.FontCharRange.eastAsia);
run.setFontFamily("仿宋", XWPFRun.FontCharRange.cs);
}
run.addBreak();
run.addTab();
}
}
}
** 想要实现的效果是替换word文本中的匹配字段放入List中的数据
例如:冒号前面是黑体后面的文字是仿宋的效果
张三:今天工作
李四:今天回家
王五:今天劳务**
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class SetCharacterFormat {
public static void main(String[] args) {
//创建Word文档
Document document = new Document();
Section sec = document.addSection();
//添加段落
Paragraph paragraph = sec.addParagraph();
paragraph.appendText("这是一段样式繁多的文字,我是");
//在段落中添加文字,并返回TextRange对象
TextRange tr = paragraph.appendText("带删除线的文字");
//通过TextRange对象设置文字带删除线
tr.getCharacterFormat().isStrikeout(true);
//设置文字阴影效果
paragraph.appendText(",我是");
tr = paragraph.appendText("带阴影的文字");
tr.getCharacterFormat().isShadow(true);
//设置文字字号
paragraph.appendText(",我是");
tr = paragraph.appendText("加大的文字");
tr.getCharacterFormat().setFontSize(20);
//设置文字颜色
paragraph.appendText(",我是");
tr = paragraph.appendText("红色的文字");
tr.getCharacterFormat().setTextColor(Color.red);
//设置文字斜体加粗
paragraph.appendText(",我是");
tr = paragraph.appendText("斜体加粗的文字");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setItalic(true);
//设置文字带下划线
paragraph.appendText(",我是");
tr = paragraph.appendText("带下划线的文字");
tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single);
//设置文字背景色
paragraph.appendText(",我是");
tr = paragraph.appendText("带背景色的文字");
tr.getCharacterFormat().setTextBackgroundColor(Color.GREEN);
//为文字添加上标
paragraph.appendText(",这里还有一个公式:a");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(" + b");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(" = c");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText("。");
//对整个段落设置为宋体(默认为英文字体)
ParagraphStyle style1 = new ParagraphStyle(document);
style1.setName("style");
style1.getCharacterFormat().setFontName("宋体");
document.getStyles().add(style1);
paragraph.applyStyle(style1.getName());
//保存文档
document.saveToFile("SetCharacterFormat.docx", FileFormat.Docx);
}
}