新聞中心
小編給大家分享一下如何使用C#向word文檔插入和隱藏段落,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在民豐等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作定制網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站建設(shè),民豐網(wǎng)站建設(shè)費用合理。
編輯Word文檔時,我們有時會突然想增加一段新內(nèi)容;而將word文檔給他人瀏覽時,有些信息我們是不想讓他人看到的。那么如何運用C#編程的方式巧妙地插入或隱藏段落呢?本文將與大家分享一種向Word文檔插入新段落及隱藏段落的好方法。
這里使用的是Free Spire.Doc for .NET組件,該組件允許開發(fā)人員輕松并靈活地操作Word文檔。
向Word文檔插入一個新段落的操作步驟
步驟1:新建一個文檔并加載現(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("向日葵的花語是——太陽、光輝、高傲、忠誠、愛慕、沉默的愛。向日葵又叫望日蓮,一個很美的名字"); textRange1.CharacterFormat.TextColor = Color.Blue; textRange1.CharacterFormat.FontSize = 15; textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
步驟3:保存文檔
document.SaveToFile("result.docx", FileFormat.Docx);
以下是程序運行前后的對比圖:
運行前
運行后
隱藏段落的操作步驟
當(dāng)操作Word文檔時,我們可以通過Microsoft Word點擊字體對話框來隱藏所選擇的文本。請通過如下的屏幕截圖來查看Microsoft是如何隱藏文本的:
然而,F(xiàn)ree Spire.Doc for .NET可以通過設(shè)置CharacterFormat.Hidden的屬性來隱藏指定文本或整個段落,下面將為大家介紹詳細(xì)步驟:
步驟1:新建一個文檔并加載現(xiàn)有文檔
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\雛菊.docx", FileFormat.Docx);
步驟2:獲取Word文檔的第一個section和最后一段
Section sec = doc.Sections[0]; Paragraph para = sec.Paragraphs[sec.Paragraphs.Count - 1];
步驟3:調(diào)用for循環(huán)語句來獲取最后一段的所有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);
以下是程序運行前后的對比圖:
運行前
運行后
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("向日葵的花語是——太陽、光輝、高傲、忠誠、愛慕、沉默的愛。向日葵又叫望日蓮,一個很美的名字"); 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)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:如何使用C#向word文檔插入和隱藏段落
本文地址:http://www.dlmjj.cn/article/igpgcc.html