天天看點

C# 給Word文檔添加内容控件

C# 給Word文檔添加内容控件

在MS Word中,我們可以通過内容控件來向word文檔中插入預先定義好的子產品,指定子產品的内容格式(如圖檔、日期、清單或格式化的文本等),進而建立一個結構化的word文檔。下面就來看看如何使用C#給word文檔添加組合框、文本、圖檔、日期選取器及下拉清單等内容控件(這裡我借助了一個word元件

Spire.Doc

)。

添加組合框内容控件

組合框用于顯示使用者可以選擇的項目清單。和下拉清單不同的是組合框允許使用者編輯或添加項。

//給段落添加一個内容控件并指定它的SDT type為Combo Box
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.ComboBox;

//建立一個Combo Box, 添加選項并把它指派給内容控件
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);      
C# 給Word文檔添加内容控件

添加文本内容控件

純文字控件包含文本,但不能包含其他項,例如表格、圖檔或其他内容控件。此外,純文字控件中的所有文本都具有相同的格式。

添加文本内容控件的步驟和添加組合框内容控件類似,核心代碼如下:

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);      
C# 給Word文檔添加内容控件

添加圖檔内容控件

圖檔控件用于顯示圖像。我們可以在設計時或運作時指定圖像,使用者也可以單擊此控件以選擇要插入文檔中的圖像。

核心代碼:

paragraph = section.AddParagraph();
sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.Picture;
DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };
pic.LoadImage(Image.FromFile("C:\\Icon.jpg"));
sd.SDTContent.ChildObjects.Add(pic);      
C# 給Word文檔添加内容控件

添加日期選取器内容控件

日期選取器提供用于選擇日期的月曆 UI。最終使用者單擊控件中的下拉箭頭時,就會顯示月曆。

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);      
C# 給Word文檔添加内容控件

添加下拉清單内容控件

下拉清單顯示了使用者可以選擇的項目清單。群組合框不同的是,下拉清單不允許使用者添加或編輯項。

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);      
C# 給Word文檔添加内容控件

全部代碼:

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields; 

namespace Insert_content_control_in_word_document
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立一個新的Word文檔
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph(); 

            //添加組合框内容控件
            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);

            //添加文本内容控件
            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); 

            //添加圖檔内容控件
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Picture;
            DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };
            pic.LoadImage(Image.FromFile("C:\\Icon.jpg"));
            sd.SDTContent.ChildObjects.Add(pic);
 
            //添加日期選取器内容控件
            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);
 
            //添加下拉清單内容控件
            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); 

            //儲存并重新開機檔案
            string resultfile = "sample.docx";
            document.SaveToFile(resultfile, FileFormat.Docx);
            System.Diagnostics.Process.Start(resultfile);           
        }
    }
}