本文将介绍如何使用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)