该文将介绍如何使用象征符号绘制复选框到word文档。
import com.spire.doc.*;
import com.spire.doc.documents.paragraph;
import com.spire.doc.documents.paragraphstyle;
import com.spire.doc.documents.textselection;
import com.spire.doc.fields.textrange;
public class drawcombox {
public static void main(string[] args) {
//新建word 文档
document doc = new document();
section section = doc.addsection();
paragraph para = section.addparagraph();
para.settext("指定字符替换成复选框 symbol1, symbol2, symbol3.");
//设置段落中字体样式
paragraphstyle style= new paragraphstyle(doc);
style.setname("parastyle");
style.getcharacterformat().setfontname("宋体");
style.getcharacterformat().setfontsize(11f);
doc.getstyles().add(style);
para.applystyle("parastyle");
//复选框打勾
textselection selection1 = doc.findstring("symbol1",true,true);
textrange tr1 = selection1.getasonerange();
tr1.getcharacterformat().setfontname("wingdings 2");
//除了16进制,也可以用10进制来表示这个符号,复选框打勾是82
doc.replace(selection1.getselectedtext(), "\u0052", true, true);
//doc.replace(selection1.getselectedtext(),string.valueof(((char)82)), true, true);
//复选框打叉
textselection selection2 = doc.findstring("symbol2",true,true);
textrange tr2 = selection2.getasonerange();
tr2.getcharacterformat().setfontname("wingdings 2");
//16进制复选框打叉是0053,10进制是83
doc.replace(selection2.getselectedtext(), "\u0053", true, true);
//复选框不勾选
textselection selection3 = doc.findstring("symbol3", true, true);
textrange tr3 = selection3.getasonerange();
tr3.getcharacterformat().setfontname("wingdings 2");
//16进制复选框不勾选是00a3,10进制是163
doc.replace(selection3.getselectedtext(), "\u00a3", true, true);
doc.savetofile("output/symboltest.docx",fileformat.docx_2013);
}
}
效果图: