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

C# 給Word文檔添加內容控件html

在MS Word中,咱們能夠經過內容控件來向word文檔中插入預先定義好的模塊,指定模塊的內容格式(如圖片、日期、列表或格式化的文本等),從而建立一個結構化的word文檔。下面就來看看如何使用C#給word文檔添加組合框、文本、圖片、日期選取器及下拉列表等內容控件(這裏我藉助了一個word組件Spire.Doc)。spa

添加組合框內容控件設計

組合框用於顯示用戶能夠選擇的項目列表。和下拉列表不一樣的是組合框容許用戶編輯或添加項。3d

//給段落添加一個內容控件並指定它的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);

 

                   

添加文本內容控件code

純文本控件包含文本,但不能包含其餘項,例如表格、圖片或其餘內容控件。此外,純文本控件中的全部文本都具備相同的格式。orm

添加文本內容控件的步驟和添加組合框內容控件相似,核心代碼以下:htm

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);

 

 

添加圖片內容控件blog

圖片控件用於顯示圖像。咱們能夠在設計時或運行時指定圖像,用戶也能夠單擊此控件以選擇要插入文檔中的圖像。圖片

核心代碼:文檔

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);

 

 

添加日期選取器內容控件

日期選取器提供用於選擇日期的日曆 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);

 

 

 

添加下拉列表內容控件

下拉列表顯示了用戶能夠選擇的項目列表。和組合框不一樣的是,下拉列表不容許用戶添加或編輯項。

核心代碼:

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);

 

 

所有代碼:

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);           
        }
    }
}
相關文章
相關標籤/搜索