C#向word文檔插入新段落及隱藏段落的方法
編輯Word文檔時,我們有時會突然想增加一段新內(nèi)容;而將word文檔給他人瀏覽時,有些信息我們是不想讓他人看到的。那么如何運(yùn)用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);
以下是程序運(yùn)行前后的對比圖:
運(yùn)行前

運(yùn)行后

隱藏段落的操作步驟
當(dāng)操作Word文檔時,我們可以通過Microsoft Word點(diǎn)擊字體對話框來隱藏所選擇的文本。請通過如下的屏幕截圖來查看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);
以下是程序運(yùn)行前后的對比圖:
運(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("向日葵的花語是——太陽、光輝、高傲、忠誠、愛慕、沉默的愛。向日葵又叫望日蓮,一個很美的名字");
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);
}
}
}
這是我本次要分享的全部內(nèi)容,感謝您的瀏覽。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#利用Session對象實(shí)現(xiàn)購物車的方法示例
這篇文章主要介紹了c#利用Session對象實(shí)現(xiàn)購物車的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
C#接口INotifyPropertyChanged使用方法
這篇文章介紹了C#接口INotifyPropertyChanged的使用方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
WPF使用Dragablz構(gòu)建可拖拽分離的Tab頁程序
這篇文章介紹了WPF使用Dragablz構(gòu)建可拖拽分離Tab頁的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實(shí)例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了單鏈表的原理、定義與C#具體實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
C#實(shí)現(xiàn)六大設(shè)計(jì)原則之依賴倒置原則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之依賴倒置原則的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02

