结构化文档
结构化文档是这样一种文档,即可以控制内容在文档的何处显示,文档中可以显示的内容类型以及能否编辑此内容。
使用内容控件来构建文档
内容控件是microsoft word文档中的实体,充当特定内容的容器。单个内容控件可以包含格式化文本、日期、列表或段落等内容。内容控件帮助您创建丰富、结构化的内容基块,并旨在用于将明确定义的基块插入文档的模板中,从而创建结构化文档。
本文将以下拉列表内容控件为例,详细介绍如何使用spire.doc在word文档中创建下拉框。通过类似方法,我们可以创建文本控件、图片控件、组合框控件和日期选取器控件。
创建下拉框控件
c#
//创建文档
document document = new document();
//添加section
section section = document.addsection();
//添加段落
paragraph paragraph = section.addparagraph();
//添加下拉列表内容控件
structuredocumenttaginline sdt = new structuredocumenttaginline(document);
sdt.sdtproperties.sdttype = sdttype.dropdownlist;
sdt.sdtproperties.alias = "下拉框";
sdt.sdtproperties.tag = "下拉框";
paragraph.childobjects.add(sdt);
sdtdropdownlist sddl = new sdtdropdownlist();
sddl.listitems.add(new sdtlistitem("中国", "1"));
sddl.listitems.add(new sdtlistitem("德国", "2"));
sddl.listitems.add(new sdtlistitem("英国", "3"));
sddl.listitems.add(new sdtlistitem("美国", "4"));
sdt.sdtproperties.controlproperties = sddl;
textrange rt = new textrange(document);
rt.text = sddl.listitems[0].displaytext;
sdt.sdtcontent.childobjects.add(rt);
//保存文档
document.savetofile("下拉框控件", fileformat.docx2013);
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)
sdt.sdtproperties.sdttype = sdttype.dropdownlist
sdt.sdtproperties.alias = "
下拉框"
sdt.sdtproperties.tag = "
下拉框"
paragraph.childobjects.add(sdt)
dim sddl as sdtdropdownlist = new sdtdropdownlist
sddl.listitems.add(new sdtlistitem("中国", "1"))
sddl.listitems.add(new sdtlistitem("德国", "2"))
sddl.listitems.add(new sdtlistitem("英国", "3"))
sddl.listitems.add(new sdtlistitem("美国", "4"))
sdt.sdtproperties.controlproperties = sddl
dim rt as textrange = new textrange(document)
rt.text = sddl.listitems(0).displaytext
sdt.sdtcontent.childobjects.add(rt)
document.savetofile("
下拉框控件.docx", fileformat.docx2013)
创建文本控件、图片控件、日期选取器控件等
c#
//创建文档
document document = new document();
//添加section
section section = document.addsection();
//添加段落
paragraph paragraph = section.addparagraph();
//添加文本内容控件
structuredocumenttaginline sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.text;
sdt.sdtproperties.alias = "纯文本";
sdt.sdtproperties.tag = "纯文本";
sdttext text = new sdttext(true);
text.ismultiline = true;
sdt.sdtproperties.controlproperties = text;
textrange tr = new textrange(document);
tr.text = "请输入纯文本。_";
spire.doc.formatting.characterformat format = new spire.doc.formatting.characterformat(document);
format.fontname = "宋体";
tr.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(tr);
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;
tr = new textrange(document);
tr.text = "可以输入表格,图片,格式化的文本到这里。_";
tr.applycharacterformat(format);
sdt.sdtcontent.childobjects.add(tr);
//添加组合框内容控件
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("u.k."));
cb.listitems.add(new sdtlistitem("japan"));
cb.listitems.add(new sdtlistitem("italy"));
sdt.sdtproperties.controlproperties = cb;
tr = new textrange(document);
tr.text = cb.listitems[0].displaytext;
sdt.sdtcontent.childobjects.add(tr);
//添加图片内容控件
paragraph = section.addparagraph();
sdt = new structuredocumenttaginline(document);
paragraph.childobjects.add(sdt);
sdt.sdtproperties.sdttype = sdttype.picture;
sdt.sdtproperties.alias = "图片";
sdt.sdtproperties.tag = "图片";
docpicture pic = new docpicture(document);
pic.loadimage(image.fromfile("logo.png"));
sdt.sdtcontent.childobjects.add(pic);
//添加日期选取器控件
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";
sdttr = new textrange(document);
tr.text = "2017.02.08";
sdt.sdtcontent.childobjects.add(tr);
//保存文档
document.savetofile("更多内容控件.docx", fileformat.docx2013);
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.text
sdt.sdtproperties.alias = "纯文本"
sdt.sdtproperties.tag = "纯文本"
dim text as sdttext = new sdttext(false)
text.ismultiline = true
sdt.sdtproperties.controlproperties = text
dim tr as textrange = new textrange(document)
tr.text = "请输入纯文本。"
dim format as spire.doc.formatting.characterformat = new spire.doc.formatting.characterformat(document)
format.fontname = "宋体"
tr.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(tr)
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
tr = new textrange(document)
tr.text = "可以输入表格,图片,格式化的文本到这里。"
tr.applycharacterformat(format)
sdt.sdtcontent.childobjects.add(tr)
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("u.k."))
cb.listitems.add(new sdtlistitem("japan"))
cb.listitems.add(new sdtlistitem("italy"))
sdt.sdtproperties.controlproperties = cb
tr = new textrange(document)
tr.text = cb.listitems(0).displaytext
sdt.sdtcontent.childobjects.add(tr)
paragraph = section.addparagraph
sdt = new structuredocumenttaginline(document)
paragraph.childobjects.add(sdt)
sdt.sdtproperties.sdttype = sdttype.picture
sdt.sdtproperties.alias = "图片"
sdt.sdtproperties.tag = "图片"
dim pic as docpicture = new docpicture(document)
pic.loadimage(image.fromfile("logo.png"))
sdt.sdtcontent.childobjects.add(pic)
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"
sdt.sdtproperties.controlproperties = date
tr = new textrange(document)
tr.text = "2017.02.08"
sdt.sdtcontent.childobjects.add(tr)
document.savetofile("更多内容控件.docx", fileformat.docx2013)