最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#向Word文檔中添加內(nèi)容控件的方法示例

 更新時間:2017年01月05日 16:49:12   作者:Yesi  
這篇文章主要給大家介紹了C#向Word文檔中添加內(nèi)容控件的方法,文中對各種不同控件的添加方法分別進(jìn)行了介紹,如組合框、文本、圖片、日期選取器及下拉列表等內(nèi)容控件,都給出了詳細(xì)的示例代碼,有需要的朋友可以參考借鑒,下面來一起看看吧。

前言

大家應(yīng)該都知道在MS Word中,我們可以通過內(nèi)容控件來向word文檔中插入預(yù)先定義好的模塊,指定模塊的內(nèi)容格式(如圖片、日期、列表或格式化的文本等),從而創(chuàng)建一個結(jié)構(gòu)化的word文檔。

下面就來看看如何使用C#給word文檔添加組合框、文本、圖片、日期選取器及下拉列表等內(nèi)容控件(這里我借助了一個word組件Spire.Doc)。

添加組合框內(nèi)容控件

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

核心代碼如下:

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

//創(chuàng)建一個Combo Box, 添加選項并把它賦值給內(nèi)容控件
SdtComboBox cb = new SdtComboBox();
cb.ListItems.Add(new SdtListItem("Cat"));
cb.ListItems.Add(new SdtListItem("Dog"));
sd.SDTProperties.ControlProperties = cb;
 
//設(shè)置顯示文本
TextRange rt = new TextRange(document);
rt.Text = cb.ListItems[0].DisplayText;
sd.SDTContent.ChildObjects.Add(rt);

添加文本內(nèi)容控件

純文本控件包含文本,但不能包含其他項,例如表格、圖片或其他內(nèi)容控件。此外,純文本控件中的所有文本都具有相同的格式。

添加文本內(nèi)容控件的步驟和添加組合框內(nèi)容控件類似

核心代碼如下:

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

添加圖片內(nèi)容控件

圖片控件用于顯示圖像。我們可以在設(shè)計時或運行時指定圖像,用戶也可以單擊此控件以選擇要插入文檔中的圖像。

核心代碼:

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

添加日期選取器內(nèi)容控件

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

添加下拉列表內(nèi)容控件

下拉列表顯示了用戶可以選擇的項目列表。和組合框不同的是,下拉列表不允許用戶添加或編輯項。

核心代碼:

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)
  {
   //創(chuàng)建一個新的Word文檔
   Document document = new Document();
   Section section = document.AddSection();
   Paragraph paragraph = section.AddParagraph();

   //添加組合框內(nèi)容控件
   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);

   //添加文本內(nèi)容控件
   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);

   //添加圖片內(nèi)容控件
   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);
 
   //添加日期選取器內(nèi)容控件
   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);
 
   //添加下拉列表內(nèi)容控件
   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);   
  }
 }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法

    C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法

    這篇文章主要介紹了C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法,本文總結(jié)了Convert.ToDateTime(string)、Convert.ToDateTime(string, IFormatProvider)、DateTime.ParseExact()三種方法,需要的朋友可以參考下
    2015-07-07
  • C#獲取兩個數(shù)的最大公約數(shù)和最小公倍數(shù)示例

    C#獲取兩個數(shù)的最大公約數(shù)和最小公倍數(shù)示例

    本文介紹了使用C#獲取兩個數(shù)的最大公約數(shù)和最小公倍數(shù)的示例,大家參考使用吧
    2014-01-01
  • C#探秘系列(三)——StackTrace,Trim

    C#探秘系列(三)——StackTrace,Trim

    這個系列我們看看C#中有哪些我們知道,但是又不知道怎么用,又或者懶得去了解的東西,比如這篇我們要介紹的StackTrace,Trim
    2014-05-05
  • 最新評論

    宜兴市| 晋中市| 乌海市| 仙游县| 雷波县| 昔阳县| 石阡县| 凤凰县| 北安市| 龙门县| 屏东县| 迭部县| 乌审旗| 会理县| 达拉特旗| 十堰市| 乌什县| 乌什县| 鸡西市| 玉溪市| 白沙| 和林格尔县| 邯郸市| 大丰市| 邳州市| 定襄县| 辽宁省| 松江区| 宜兰县| 航空| 广灵县| 昭苏县| 砀山县| 手机| 杭锦旗| 石家庄市| 淮安市| 张家界市| 杨浦区| 潞西市| 枝江市|