小編給大家分享一下如何使用C#向word文檔插入和隱藏段落,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
編輯Word文檔時(shí),我們有時(shí)會(huì)突然想增加一段新內(nèi)容;而將word文檔給他人瀏覽時(shí),有些信息我們是不想讓他人看到的。那么如何運(yùn)用C#編程的方式巧妙地插入或隱藏段落呢?本文將與大家分享一種向Word文檔插入新段落及隱藏段落的好方法。
這里使用的是Free Spire.Doc for .NET組件,該組件允許開(kāi)發(fā)人員輕松并靈活地操作Word文檔。
向Word文檔插入一個(gè)新段落的操作步驟
步驟1:新建一個(gè)文檔并加載現(xiàn)有文檔
Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx);
步驟2:插入新段落并設(shè)置字體格式
Paragraph paraInserted = document.Sections[0].AddParagraph(); TextRange textRange1 = paraInserted.AppendText("向日葵的花語(yǔ)是——太陽(yáng)、光輝、高傲、忠誠(chéng)、愛(ài)慕、沉默的愛(ài)。向日葵又叫望日蓮,一個(gè)很美的名字"); textRange1.CharacterFormat.TextColor = Color.Blue; textRange1.CharacterFormat.FontSize = 15; textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
步驟3:保存文檔
document.SaveToFile("result.docx", FileFormat.Docx);
以下是程序運(yùn)行前后的對(duì)比圖:
運(yùn)行前
運(yùn)行后
隱藏段落的操作步驟
當(dāng)操作Word文檔時(shí),我們可以通過(guò)Microsoft Word點(diǎn)擊字體對(duì)話框來(lái)隱藏所選擇的文本。請(qǐng)通過(guò)如下的屏幕截圖來(lái)查看Microsoft是如何隱藏文本的:
然而,F(xiàn)ree Spire.Doc for .NET可以通過(guò)設(shè)置CharacterFormat.Hidden的屬性來(lái)隱藏指定文本或整個(gè)段落,下面將為大家介紹詳細(xì)步驟:
步驟1:新建一個(gè)文檔并加載現(xiàn)有文檔
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\雛菊.docx", FileFormat.Docx);
步驟2:獲取Word文檔的第一個(gè)section和最后一段
Section sec = doc.Sections[0]; Paragraph para = sec.Paragraphs[sec.Paragraphs.Count - 1];
步驟3:調(diào)用for循環(huán)語(yǔ)句來(lái)獲取最后一段的所有TextRange并將CharacterFormat.Hidden的屬性設(shè)置為true
for (int i = 0; i < para.ChildObjects.Count;i++) { (para.ChildObjects[i] as TextRange).CharacterFormat.Hidden = true; }
步驟4:保存文檔
doc.SaveToFile("result1.docx", FileFormat.Docx);
以下是程序運(yùn)行前后的對(duì)比圖:
運(yùn)行前
運(yùn)行后
C#完整代碼
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; namespace insert_new_paragraph_and_hide { class Program { static void Main(string[] args) { //該部分為插入新段落的代碼 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx); Paragraph paraInserted = document.Sections[0].AddParagraph(); TextRange textRange1 = paraInserted.AppendText("向日葵的花語(yǔ)是——太陽(yáng)、光輝、高傲、忠誠(chéng)、愛(ài)慕、沉默的愛(ài)。向日葵又叫望日蓮,一個(gè)很美的名字"); textRange1.CharacterFormat.TextColor = Color.Blue; textRange1.CharacterFormat.FontSize = 15; textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash; document.SaveToFile("result.docx", FileFormat.Docx); //該部分為隱藏段落的代碼 Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\雛菊.docx", FileFormat.Docx); Section sec = doc.Sections[0]; Paragraph para = sec.Paragraphs[sec.Paragraphs.Count - 1]; for (int i = 0; i < para.ChildObjects.Count;i++) { (para.ChildObjects[i] as TextRange).CharacterFormat.Hidden = true; } doc.SaveToFile("result1.docx", FileFormat.Docx); } } }
以上是“如何使用C#向word文檔插入和隱藏段落”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標(biāo)題名稱:如何使用C#向word文檔插入和隱藏段落-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.chinadenli.net/article2/iggic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、用戶體驗(yàn)、手機(jī)網(wǎng)站建設(shè)、域名注冊(cè)、關(guān)鍵詞優(yōu)化、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容