首页 > 应用开发 > > C# 添加、获取Word内容控件
在Word中,借助内容控件,可设计具有特定功能的文档或模板。以下表格中简单罗列了几种常用的内容控件。
名称简介
下拉列表内容控件下拉列表包含了一个预先定义好的列表。和组合框不同的是下拉列表不允许用户编辑项
纯文本内容控件纯文本内容控件只能包含文本,不能包含其他项,例如表格、图片或其他内容控件
富文本内容控件与纯文本内容控件不同,富文本内容控件可以包含除文本以外的其他项,例如表格、图片或其他内容控件
日期选取器内容控件日期选取器内容控件包含一个日历控件,用于帮助用户输入日期
组合框内容控件组合框控件包含一个可以直接编辑的列表。它结合了文本框和下拉列表的属性,用户可以在框中键入值或者从下拉列表中选择值
图片内容控件图片内容控件用于显示图片。用户可以在制作模板时指定图片,也可以通过单击此控件来选择需要插入的图片。
本篇文章中介绍通过C# 来添加以上几种内容控件的添加方法。同时,针对Word文档中已有的控件,也可以读取控件名及控件中的内容。
使用工具:Spire.Doc for .NET
Dll文件获取及导入:
方法1:通过官网下载dll文件包。下载后,解压安装。完成安装后,注意在vs程序中添加引用Spire.Doc.dll程序集文件。如下图:

方法2:可通过Nuget网站下载。
C# 代码示例
【示例1】添加内容控件到Word
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;
namespace Addsdt
{
class Program
{
static void Main(string[] args)
{
//创建word文档
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//实例化StructureDocumentTagInline类的对象
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;
sdt.SDTProperties.ControlProperties = date;
//设置显示日期
rt = new TextRange(document);
rt.Text = "2019.08.22";
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 = false;
//添加图片内容控件
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 = 30f;
pic.HeightScale = 30f;
pic.LoadImage(Image.FromFile("nz.png"));
sdt.SDTContent.ChildObjects.Add(pic);
//保存文档
document.SaveToFile("Controls.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Controls.docx");
}
}
}控件添加效果:

点击相应内容,可查看控件添加情况。
【示例2】获取Word中的内容控件
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Collections.Generic;
using System.Text;
namespace GetSDT
{
class Program
{
static void Main(string[] args)
{
//加载含有内容控件的文档
Document document = new Document();
document.LoadFromFile("test.docx");
//调用StructureTags类获取内容控件列表
StructureTags structureTags = GetAllTags(document);
List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;
//获取内容控件属性并输出到控制台
for (int i = 0; i < tagInlines.Count; i++)
{
string alias = tagInlines[i].SDTProperties.Alias;
string tag = tagInlines[i].SDTProperties.Tag;
string value = tagInlines[i].SDTContent.Text;
Console.WriteLine(alias);
Console.WriteLine(tag);
Console.WriteLine(value);
Console.WriteLine("_____________________");
}
Console.ReadLine();
}
static StructureTags GetAllTags(Document document)
{
//遍历文档并获取所有的内容控件
StructureTags structureTags = new StructureTags();
foreach (Section section in document.Sections)
{
foreach (DocumentObject obj in section.Body.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
{
if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
{
structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
}
}
}
}
}
return structureTags;
}
public class StructureTags
{
List<StructureDocumentTagInline> m_tagInlines;
public List<StructureDocumentTagInline> tagInlines
{
get
{
if (m_tagInlines == null)
m_tagInlines = new List<StructureDocumentTagInline>();
return m_tagInlines;
}
set
{
m_tagInlines = value;
}
}
}
}
}
读取效果:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/835/viewspace-3134752/ ,如需转载,请注明出处,否则将追究法律责任。