C#如何向word文檔插入一個新段落及隱藏段落

來源:互聯網
上載者:User

標籤:document   desktop   向日葵   users   如何   

  • 向Word文檔插入一個新段落的操作步驟

步驟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文檔插入一個新段落及隱藏段落

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.