本文将介绍如何使用spire.doc组件给word文档添加内容控件。
内容控件简介
内容控件在word文档中充当着特定内容的容器,不同的内容控件可以指定不同的内容类型(例如日期、图片或文本等)以及能否编辑此内容。我们从word 2013中选取了以下几种常见的内容控件进行介绍:
名称 |
简介 |
下拉列表内容控件 |
下拉列表包含了一个预先定义好的列表。和组合框不同的是下拉列表不允许用户编辑项。 |
纯文本内容控件 |
纯文本内容控件只能包含文本,不能包含其他项,例如表格、图片或其他内容控件。 |
格式文本内容控件 |
与纯文本内容控件不同,格式文本内容控件可以包含除文本以外的其他项,例如表格、图片或其他内容控件。 |
日期选取器内容控件 |
日期选取器内容控件包含一个日历控件,用于帮助用户输入日期。 |
组合框内容控件 |
组合框控件包含一个可以直接编辑的列表。它结合了文本框和下拉列表的属性,用户可以在框中键入值或者从下拉列表中选择值。 |
图片内容控件 |
图片内容控件用于显示图片。用户可以在制作模板时指定图片,也可以通过单击此控件来选择需要插入的图片。 |
添加内容控件
spire.doc组件支持用户在word文档中添加多种内容控件。以下代码将展示如何使用spire.doc组件给word文档添加上述表格中所介绍的内容控件。
c#
//创建文档
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
//添加下拉列表控件
structuredocumenttaginline sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.dropdownlist;
sdt.sdtproperties.alias = "下拉列表";
sdt.sdtproperties.tag = "下拉列表";
sdtdropdownlist sddl = new sdtdropdownlist();
sddl.listitems.add(new sdtlistitem("男", "1"));
sddl.listitems.add(new sdtlistitem("女", "2"));
sdt.sdtproperties.controlproperties = sddl;
textrange rt = new textrange(document);
rt.text = sddl.listitems[0].displaytext;
spire.doc.formatting.characterformat format = new spire.doc.formatting.characterformat(document);
format.fontname = "宋体";
rt.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(rt);
//添加文本控件
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.text;
sdt.sdtproperties.alias = "纯文本";
sdt.sdtproperties.tag = "纯文本";
sdttext text = new sdttext(false);
text.ismultiline = true;
sdt.sdtproperties.controlproperties = text;
rt = new textrange(document);
rt.text = "此处只能输入文本,";
rt.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(rt);
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.richtext;
sdt.sdtproperties.alias = "格式文本";
sdt.sdtproperties.tag = "格式文本";
sdttext richtext = new sdttext(true);
richtext.ismultiline = true;
sdt.sdtproperties.controlproperties = richtext;
rt = new textrange(document);
rt.text = "输入格式化的文本、表格、图片等";
rt.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(rt);
//添加日期选取器
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.datepicker;
sdt.sdtproperties.alias = "日期选取器";
sdt.sdtproperties.tag = "日期选取器";
sdtdate date = new sdtdate();
date.calendartype = calendartype.default;
date.dateformat = "yyyy.mm.dd";
date.fulldate = datetime.now;
sdtrt = new textrange(document);
rt.text = "1990.02.08";
rt.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(rt);
//添加组合框
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.combobox;
sdt.sdtproperties.alias = "组合框";
sdt.sdtproperties.tag = "组合框";
sdtcombobox cb = new sdtcombobox();
cb.listitems.add(new sdtlistitem("英国", "1"));
cb.listitems.add(new sdtlistitem("日本", "2"));
cb.listitems.add(new sdtlistitem("意大利", "3"));
sdt.sdtproperties.controlproperties = cb;
rt = new textrange(document);
rt.text = cb.listitems[0].displaytext;
rt.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(rt);
//添加复选框
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.checkbox;
sdt.sdtproperties.alias = "复选框";
sdt.sdtproperties.tag = "复选框";
sdtcheckbox scb = new sdtcheckbox();
sdt.sdtproperties.controlproperties = scb;
rt = new textrange(document);
rt.applycharacterformat(format);
sdt.childobjects.add(rt);
scb.checked = true;
//添加图片内容控件
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.controlproperties = new sdtpicture();
sdt.sdtproperties.alias = "图片";
sdt.sdtproperties.tag = "图片";
docpicture pic = new docpicture(document);
pic.widthscale = 30;
pic.heightscale = 30;
pic.loadimage(image.fromfile("c:\\users\\administrator\\desktop\\pic\\logo.png"));
sdt.sdtcontent.childobjects.add(pic);
//保存文档
document.savetofile("controls.docx", fileformat.docx);
vb.net
dim document as document = new document
dim section as section = document.addsection
dim paragraph as paragraph = section.addparagraph
dim sdt as structuredocumenttaginline = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.dropdownlist
sdt.sdtproperties.alias = "下拉列表"
sdt.sdtproperties.tag = "下拉列表"
dim sddl as sdtdropdownlist = new sdtdropdownlist
sddl.listitems.add(new sdtlistitem("男", "1"))
sddl.listitems.add(new sdtlistitem("女", "2"))
sdt.sdtproperties.controlproperties = sddl
dim rt as textrange = new textrange(document)
rt.text = sddl.listitems(0).displaytext
dim format as spire.doc.formatting.characterformat = new spire.doc.formatting.characterformat(document)
format.fontname = "宋体"
rt.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(rt)
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.text
sdt.sdtproperties.alias = "纯文本"
sdt.sdtproperties.tag = "纯文本"
dim text as sdttext = new sdttext(false)
text.ismultiline = true
sdt.sdtproperties.controlproperties = text
rt = new textrange(document)
rt.text = "此处只能输入文本"
rt.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(rt)
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.richtext
sdt.sdtproperties.alias = "格式文本"
sdt.sdtproperties.tag = "格式文本"
dim richtext as sdttext = new sdttext(true)
richtext.ismultiline = true
sdt.sdtproperties.controlproperties = richtext
rt = new textrange(document)
rt.text = "输入格式化的文本、表格、图片等"
rt.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(rt)
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.datepicker
sdt.sdtproperties.alias = "日期选取器"
sdt.sdtproperties.tag = "日期选取器"
dim date as sdtdate = new sdtdate
date.calendartype = calendartype.default
date.dateformat = "yyyy.mm.dd"
date.fulldate = datetime.now
sdt.sdtproperties.controlproperties = date
rt = new textrange(document)
rt.text = "1990.02.08"
rt.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(rt)
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.combobox
sdt.sdtproperties.alias = "组合框"
sdt.sdtproperties.tag = "组合框"
dim cb as sdtcombobox = new sdtcombobox
cb.listitems.add(new sdtlistitem("英国", "1"))
cb.listitems.add(new sdtlistitem("日本", "2"))
cb.listitems.add(new sdtlistitem("意大利", "3"))
sdt.sdtproperties.controlproperties = cb
rt = new textrange(document)
rt.text = cb.listitems(0).displaytext
rt.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(rt)
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.checkbox
sdt.sdtproperties.alias = "复选框"
"
sdt.sdtproperties.tag = "复选框"
"
dim scb as sdtcheckbox = new sdtcheckbox
sdt.sdtproperties.controlproperties = scb
rt = new textrange(document)
rt.applycharacterformat(format)
sdt.childobjects.add(rt)
scb.checked = true
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.controlproperties = new sdtpicture
sdt.sdtproperties.alias = "图片"
sdt.sdtproperties.tag = "图片"
dim pic as docpicture = new docpicture(document)
pic.widthscale = 30!
pic.heightscale = 30!
pic.loadimage(image.fromfile("c:\users\administrator\desktop\pic\logo.png"))
sdt.sdtcontent.childobjects.add(pic)
document.savetofile("controls.docx", fileformat.docx)