C#向Word文檔中添加內(nèi)容控件的方法示例
前言
大家應(yīng)該都知道在MS Word中,我們可以通過(guò)內(nèi)容控件來(lái)向word文檔中插入預(yù)先定義好的模塊,指定模塊的內(nèi)容格式(如圖片、日期、列表或格式化的文本等),從而創(chuàng)建一個(gè)結(jié)構(gòu)化的word文檔。
下面就來(lái)看看如何使用C#給word文檔添加組合框、文本、圖片、日期選取器及下拉列表等內(nèi)容控件(這里我借助了一個(gè)word組件Spire.Doc)。
添加組合框內(nèi)容控件
組合框用于顯示用戶可以選擇的項(xiàng)目列表。和下拉列表不同的是組合框允許用戶編輯或添加項(xiàng)。
核心代碼如下:
//給段落添加一個(gè)內(nèi)容控件并指定它的SDT type為Combo Box
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.ComboBox;
//創(chuàng)建一個(gè)Combo Box, 添加選項(xiàng)并把它賦值給內(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)容控件
純文本控件包含文本,但不能包含其他項(xiàng),例如表格、圖片或其他內(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è)計(jì)時(shí)或運(yùn)行時(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。最終用戶單擊控件中的下拉箭頭時(shí),就會(huì)顯示日歷。
核心代碼:
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)容控件
下拉列表顯示了用戶可以選擇的項(xiàng)目列表。和組合框不同的是,下拉列表不允許用戶添加或編輯項(xiàng)。
核心代碼:
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)建一個(gè)新的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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
上一篇:C# #define條件編譯詳解
欄 目:C#教程
本文標(biāo)題:C#向Word文檔中添加內(nèi)容控件的方法示例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6041.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
- 01-10C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
- 01-10C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- 01-10C#代碼實(shí)現(xiàn)PDF文檔操作類
- 01-10C#實(shí)現(xiàn)向多線程傳參的三種方式實(shí)例分析
- 01-10C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法
- 01-10C#數(shù)據(jù)結(jié)構(gòu)之雙向鏈表(DbLinkList)實(shí)例詳解
- 01-10C#獲取遠(yuǎn)程XML文檔的方法
- 01-10解析C#面向?qū)ο缶幊讨蟹椒ǎ╩ethod)的使用


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?


