文档靠谱朗驰娱乐体育>>E-iceblue中文文档>>向 Word 文档添加内容控件
向 Word 文档添加内容控件
内容控件是 Microsoft Word 实体,它们充当特定内容(例如日期、列表或格式化文本段落)的容器。借助内容控件,我们可以通过在 word 文档中指定内容类型(例如日期或文本)、限制或允许编辑内容等,轻松创建结构化模板、表单或文档。
在本文中,我们将介绍如何使用 Spire.Doc for .NET 在 C# 中将 Combo Box、Text、Date Picker 和 Drop-Down List 内容控件添加到 Word 文档中。
这里只详细讲解了如何在word文档中添加Combo Box内容控件,完整代码在文末展示。
添加组合框内容控件
详细步骤和代码片段:
第 1步:初始化 Document 类的新对象并加载源文档。
Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph();
第 2步:初始化另一个对象以加载目标文档。
StructureDocumentTagInline sd = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sd); sd.SDTProperties.SDTType = SdtType.ComboBox;
第 3 步:从源文件中复制内容并将其插入到目标文件中。
SdtComboBox cb = new SdtComboBox();
cb.ListItems.Add(new SdtListItem("Cat"));
cb.ListItems.Add(new SdtListItem("Dog"));
sd.SDTProperties.ControlProperties = cb;
TextRange rt = new TextRange(document);
rt.Text = cb.ListItems[0].DisplayText;
sd.SDTContent.ChildObjects.Add(rt);
第 4 步:保存更改
string resultfile = "sample.docx"; document.SaveToFile(resultfile, FileFormat.Docx); System.Diagnostics.Process.Start(resultfile);
以下是添加 Combo Box Content Control 后的结果word 文档:
完整代码:
using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Add_content_controls_to_word_documents
{
class Program
{
static void Main(string[] args)
{
//Creat a new word document
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//Add Combo Box Content Control
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.ComboBox;
SdtComboBox cb = new SdtComboBox();
cb.ListItems.Add(new SdtListItem("Cat"));
cb.ListItems.Add(new SdtListItem("Dog"));
sd.SDTProperties.ControlProperties = cb;
TextRange rt = new TextRange(document);
rt.Text = cb.ListItems[0].DisplayText;
sd.SDTContent.ChildObjects.Add(rt);
//Add Text Content Control
paragraph = section.AddParagraph();
sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.Text;
SdtText text = new SdtText(true);
text.IsMultiline = true;
sd.SDTProperties.ControlProperties = text;
rt = new TextRange(document);
rt.Text = "Text";
sd.SDTContent.ChildObjects.Add(rt);
//Add Date Picker Content Control
paragraph = section.AddParagraph();
sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.DatePicker;
SdtDate date = new SdtDate();
date.CalendarType = CalendarType.Default;
date.DateFormat = "yyyy.MM.dd";
date.FullDate = DateTime.Now;
sd.SDTProperties.ControlProperties = date;
rt = new TextRange(document);
rt.Text = "1990.02.08";
sd.SDTContent.ChildObjects.Add(rt);
//Add Drop-Down List Content Control
paragraph = section.AddParagraph();
sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.DropDownList;
SdtDropDownList sddl = new SdtDropDownList();
sddl.ListItems.Add(new SdtListItem("Harry"));
sddl.ListItems.Add(new SdtListItem("Jerry"));
sd.SDTProperties.ControlProperties = sddl;
rt = new TextRange(document);
rt.Text = sddl.ListItems[0].DisplayText;
sd.SDTContent.ChildObjects.Add(rt);
//Save and launch the file
string resultfile = "sample.docx";
document.SaveToFile(resultfile, FileFormat.Docx);
System.Diagnostics.Process.Start(resultfile);
}
}
}
欢迎下载|体验更多E-iceblue产品
如需获取更多产品相关信息请咨询


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线