標籤:document desktop 向日葵 users 如何
步驟1:建立一個文檔並載入現有文檔
Document document = new Document();document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx);
步驟2:插入新段落並設定字型格式
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);
以下是程式運行前後的對比圖:
運行前
650) this.width=650;" src="http://images2015.cnblogs.com/blog/706090/201704/706090-20170425124601162-736112789.png" style="margin:0px;padding:0px;border:0px;" />
運行後
650) this.width=650;" src="http://images2015.cnblogs.com/blog/706090/201704/706090-20170425125840350-682899657.png" style="margin:0px;padding:0px;border:0px;" />
當操作Word文檔時,我們可以通過Microsoft Word點擊字型對話方塊來隱藏所選擇的文本。請通過如下的螢幕來查看Microsoft是如何隱藏文本的:
650) this.width=650;" src="http://images2015.cnblogs.com/blog/706090/201704/706090-20170425114608490-1964015583.png" style="margin:0px;padding:0px;border:0px;" />
然而,Free Spire.Doc for .NET可以通過設定CharacterFormat.Hidden的屬性來隱藏指定文本或整個段落,下面將為大家介紹詳細步驟:
步驟1:建立一個文檔並載入現有文檔
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:調用for迴圈語句來擷取最後一段的所有TextRange並將CharacterFormat.Hidden的屬性設定為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);
以下是程式運行前後的對比圖:
運行前
650) this.width=650;" src="http://images2015.cnblogs.com/blog/706090/201704/706090-20170425125612350-889930811.png" style="margin:0px;padding:0px;border:0px;" />
運行後
650) this.width=650;" src="http://images2015.cnblogs.com/blog/706090/201704/706090-20170425125439397-1599690931.png" style="margin:0px;padding:0px;border:0px;" />
C#完整代碼
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
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); } }}
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
C#如何向word文檔插入一個新段落及隱藏段落