本篇文章主要介绍了如何使用Spire.Doc 在 C# 中向 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 documentDocument document = new Document();Section section = document.AddSection();Paragraph paragraph = section.AddParagraph();//Add Combo Box Content ControlStructureDocumentTagInline 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 Controlparagraph = 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 Controlparagraph = 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 Controlparagraph = 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 filestring resultfile = "sample.docx";document.SaveToFile(resultfile, FileFormat.Docx);System.Diagnostics.Process.Start(resultfile);}}}
欢迎下载|体验更多E-iceblue产品
如需获取更多产品相关信息请咨询在线客服
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!