本文将介绍如何使用spire.doc for java给word文档添加组合框、复选框、文本、图片、日期选取器和下拉列表内容控件。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.date;
public class contentcontrols {
public static void main(string[] args){
//创建word文档
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
textrange txtrange = paragraph.appendtext("以下示例展示了如何给word文档添加内容控件");
section.addparagraph();
//添加组合框内容控件
paragraph = section.addparagraph();
txtrange = paragraph.appendtext("组合框内容控件: ");
txtrange.getcharacterformat().setitalic(true);
structuredocumenttaginline sd = new structuredocumenttaginline(document);
paragraph.getchildobjects().add(sd);
sd.getsdtproperties().setsdttype(sdttype.combo_box);
sd.getsdtproperties().setalias("组合框");
sd.getsdtproperties().settag("组合框");
sdtcombobox cb = new sdtcombobox();
cb.getlistitems().add(new sdtlistitem("spire.doc"));
cb.getlistitems().add(new sdtlistitem("spire.xls"));
cb.getlistitems().add(new sdtlistitem("spire.pdf"));
sd.getsdtproperties().setcontrolproperties(cb);
textrange rt = new textrange(document);
rt.settext(cb.getlistitems().get(0).getdisplaytext());
sd.getsdtcontent().getchildobjects().add(rt);
section.addparagraph();
//添加复选框内容控件
paragraph = section.addparagraph();
txtrange = paragraph.appendtext("复选框内容控件: ");
txtrange.getcharacterformat().setitalic(true);
sd = new structuredocumenttaginline(document);
paragraph.getchildobjects().add(sd);
sd.getsdtproperties().setsdttype(sdttype.check_box);
sd.getsdtproperties().setalias("复选框");
sd.getsdtproperties().settag("复选框");
sdtcheckbox scb = new sdtcheckbox();
sd.getsdtproperties().setcontrolproperties(scb);
rt = new textrange(document);
sd.getchildobjects().add(rt);
scb.setchecked(true);
section.addparagraph();
//添加文本内容控件
paragraph = section.addparagraph();
txtrange = paragraph.appendtext("文本内容控件: ");
txtrange.getcharacterformat().setitalic(true);
sd = new structuredocumenttaginline(document);
paragraph.getchildobjects().add(sd);
sd.getsdtproperties().setsdttype(sdttype.text);
sd.getsdtproperties().setalias("文本");
sd.getsdtproperties().settag("文本");
sdttext text = new sdttext(true);
text.ismultiline(true);
sd.getsdtproperties().setcontrolproperties(text);
rt = new textrange(document);
rt.settext("文本");
sd.getsdtcontent().getchildobjects().add(rt);
section.addparagraph();
//添加图片内容控件
paragraph = section.addparagraph();
txtrange = paragraph.appendtext("图片内容控件: ");
txtrange.getcharacterformat().setitalic(true);
sd = new structuredocumenttaginline(document);
paragraph.getchildobjects().add(sd);
sd.getsdtproperties().setcontrolproperties(new sdtpicture());
sd.getsdtproperties().setalias("图片");
sd.getsdtproperties().settag("图片");
docpicture pic = new docpicture(document);
pic.setwidth(10f);
pic.setheight(10f);
pic.loadimage("logo.png");
sd.getsdtcontent().getchildobjects().add(pic);
section.addparagraph();
//添加日期选取器内容控件
paragraph = section.addparagraph();
txtrange = paragraph.appendtext("日期选取器内容控件: ");
txtrange.getcharacterformat().setitalic(true);
sd = new structuredocumenttaginline(document);
paragraph.getchildobjects().add(sd);
sd.getsdtproperties().setsdttype(sdttype.date_picker);
sd.getsdtproperties().setalias("日期");
sd.getsdtproperties().settag("日期");
sdtdate date = new sdtdate();
date.setcalendartype(calendartype.default);
date.setdateformat("yyyy.mm.dd");
date.setfulldate(new date());
sd.getsdtproperties().setcontrolproperties(date);
rt = new textrange(document);
rt.settext("2018.12.25");
sd.getsdtcontent().getchildobjects().add(rt);
section.addparagraph();
//添加下拉列表内容控件
paragraph = section.addparagraph();
txtrange = paragraph.appendtext("下拉列表内容控件: ");
txtrange.getcharacterformat().setitalic(true);
sd = new structuredocumenttaginline(document);
paragraph.getchildobjects().add(sd);
sd.getsdtproperties().setsdttype(sdttype.drop_down_list);
sd.getsdtproperties().setalias("下拉列表");
sd.getsdtproperties().settag("下拉列表");
sdtdropdownlist sddl = new sdtdropdownlist();
sddl.getlistitems().add(new sdtlistitem("选项1"));
sddl.getlistitems().add(new sdtlistitem("选项2"));
sd.getsdtproperties().setcontrolproperties(sddl);
rt = new textrange(document);
rt.settext(sddl.getlistitems().get(0).getdisplaytext());
sd.getsdtcontent().getchildobjects().add(rt);
//保存结果文档
document.savetofile("addcontentcontrols1.docx", fileformat.docx_2013);
}
}
生成文档: