文檔半島外圍網上直營>>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交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線