C# /VB.NET 操作Word (一)——插入、修改、刪除Word批註

批註內容可以是對某段文字或內容的註釋,也可以是對文段中心思想的概括提要,或者是對文章內容的評判、疑問,以及在閱讀時給自己或他人起到提示作用。本篇文章中將介紹如何在C#/VB中操作Word批註,主要包含以下要點:

  • 插入Word批註
  • 修改Word批註
  • 刪除Word批註
    使用工具:Free Spire.Doc for .NET 6.3(最新社區版)
    :編輯代碼前注意添加引用Sprie.Doc.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)

C# /VB.NET 操作Word (一)——插入、修改、刪除Word批註

1.插入Word批註

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertComment_Word
{
    class Program
    {
        static void Main(string[] args)
        { 
            //實例化一個Document類對象,並加載Word文檔
            Document document = new Document();
            document.LoadFromFile("sample.docx");

            //獲取第一段第一節
            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[0];

            //添加文本到批註
            string str = "This paragraph describes the origin and the purpose of WEF";
            Comment comment = paragraph.AppendComment(str);
            //添加批註作者
            comment.Format.Author = "E-iceblue";

            //保存並打開文檔
            document.SaveToFile("Comments.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("Comments.docx");
        }
    }
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertComment_Word

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            Dim document As Document = New Document
            document.LoadFromFile("sample.docx")
            Dim section As Section = document.Sections(0)
            Dim paragraph As Paragraph = section.Paragraphs(0)
            Dim str As String = "This paragraph describes the origin and the purpose of WEF"
            Dim comment As Comment = paragraph.AppendComment(str)
            comment.Format.Author = "E-iceblue"
            document.SaveToFile("Comments.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("Comments.docx")
        End Sub
    End Class
End Namespace

測試結果:
C# /VB.NET 操作Word (一)——插入、修改、刪除Word批註

2.修改、刪除Word批註

測試文檔:
C# /VB.NET 操作Word (一)——插入、修改、刪除Word批註
C#

using Spire.Doc;

namespace ReplaceAndRemoveComment_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Document類實例,加載帶有批註的Word文檔
            Document document = new Document();
            document.LoadFromFile("test.docx");

            //修改第一個批註內容
            document.Comments[0].Body.Paragraphs[0].Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false);

            //移除第二個批註
            document.Comments.RemoveAt(1);

            //保存並打開文檔
            document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("RemoveAndReplace.docx");
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace ReplaceAndRemoveComment_Word

    Class Program

        Private Shared Sub Main(ByVal args() As String)         
            Dim document As Document = New Document
            document.LoadFromFile("test.docx")
            document.Comments(0).Body.Paragraphs(0).Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false)
            document.Comments.RemoveAt(1)
            document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("RemoveAndReplace.docx")
        End Sub
    End Class
End Namespace

測試結果:
C# /VB.NET 操作Word (一)——插入、修改、刪除Word批註

以上是本次關於C#、VB.NET操作word批註的全部內容,感謝閱讀!
歡迎轉載,轉載請註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章