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)容控件類(lè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)大家可以留言交流。
相關(guān)文章
C#使用OleDb操作Excel和數(shù)據(jù)庫(kù)的策略
在C#編程中,使用OleDb可以方便地實(shí)現(xiàn)對(duì)Excel文件和數(shù)據(jù)庫(kù)的操作,本文探討了在C#中使用OleDb技術(shù)操作Excel和數(shù)據(jù)庫(kù)的策略,文章詳述了OleDb的定義、配置環(huán)境的步驟,并通過(guò)實(shí)際代碼示例演示了如何高效讀寫(xiě)Excel文件和交互數(shù)據(jù)庫(kù),需要的朋友可以參考下2024-05-05C#實(shí)現(xiàn)將HTML網(wǎng)頁(yè)或HTML字符串轉(zhuǎn)換為PDF
將HTML轉(zhuǎn)換為PDF可實(shí)現(xiàn)格式保留、可靠打印、文檔歸檔等多種用途,滿足不同領(lǐng)域和情境下的需求,所以本文就來(lái)介紹一下如何使用C#實(shí)現(xiàn)將HTML網(wǎng)頁(yè)或HTML字符串轉(zhuǎn)換為PDF,有需要的可以參考下2024-01-01c#通過(guò)進(jìn)程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
最近自己開(kāi)發(fā)軟件需要讀取本地配置文件,因?yàn)榈卿浻脩舻臋?quán)限不夠會(huì)導(dǎo)致無(wú)法讀取文件進(jìn)而導(dǎo)致程序崩潰,查了一些解決方法,代碼分享如下2013-12-12C#實(shí)現(xiàn)二維數(shù)據(jù)數(shù)組導(dǎo)出到Excel的詳細(xì)過(guò)程
將數(shù)據(jù)庫(kù)查詢出來(lái)的數(shù)據(jù)導(dǎo)出并生成?Excel?文件,是項(xiàng)目中經(jīng)常使用的一項(xiàng)功能,本文將介紹通過(guò)數(shù)據(jù)集生成二維數(shù)據(jù)數(shù)組并導(dǎo)出到?Excel,文中有詳細(xì)的代碼供大家參考,需要的朋友可以參考下2024-09-09C#固定大小緩沖區(qū)及使用指針復(fù)制數(shù)據(jù)詳解
這篇文章主要為大家介紹了C#固定大小緩沖區(qū)及使用指針復(fù)制數(shù)據(jù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12