批註內容可以是對某段文字或內容的註釋,也可以是對文段中心思想的概括提要,或者是對文章內容的評判、疑問,以及在閱讀時給自己或他人起到提示作用。本篇文章中將介紹如何在C#中操作Word批註,主要包含以下要點: 插入Word批註 修改Word批註 刪除Word批註 使用工具:Free Spire.Doc ...
批註內容可以是對某段文字或內容的註釋,也可以是對文段中心思想的概括提要,或者是對文章內容的評判、疑問,以及在閱讀時給自己或他人起到提示作用。本篇文章中將介紹如何在C#中操作Word批註,主要包含以下要點:
- 插入Word批註
- 修改Word批註
- 刪除Word批註
使用工具:Free Spire.Doc for .NET 6.3(最新社區版)
註:編輯代碼前註意添加引用Sprie.Doc.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)
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) '實例化一個Document類對象,並載入Word文檔 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
測試結果:
2.修改、刪除批註
測試文檔:
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) '初始化Document類實例,載入帶有批註的Word文檔 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
測試結果:
以上是本次關於操作Word批註的全部內容。感謝瀏覽!