序 在前面的文章C# 如何插入、修改、刪除Word批註一文中介紹瞭如何操作Word批註的一些方法,在本篇文章中繼續介紹操作Word批註的方法。分以下三種情況來介紹: 1. 插入圖片到Word批註 2. 讀取Word批註 3. 回覆Word批註 所需工具 Free Spire.Doc for .NET ...
序
在前面的文章C# 如何插入、修改、刪除Word批註一文中介紹瞭如何操作Word批註的一些方法,在本篇文章中繼續介紹操作Word批註的方法。分以下三種情況來介紹:
1. 插入圖片到Word批註
2. 讀取Word批註
3. 回覆Word批註
所需工具
- Free Spire.Doc for .NET 6.3(社區版)
- Visual Studio
PS:下載安裝Free Spire.Doc 後,註意在你的程式中添加引用Spire.Doc.dll(dll文件可以在安裝路徑下的Bin文件夾中獲取)
示例代碼
1. 插入圖片到Word批註
步驟 1:添加using指令
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing;
步驟 2:創建文檔,載入測試文件
Document doc = new Document(); doc.LoadFromFile("testfile.docx");
步驟 3 :獲取段落
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
步驟 4 :添加文本、圖片到批註
Comment comment = paragraph.AppendComment("探索黑科技,小米為發燒而生!");
comment.Format.Author = "Administor";
DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile("mi.png"); docPicture.LoadImage(img); //插入圖片到批註 comment.Body.AddParagraph().ChildObjects.Add(docPicture);
步驟 5 :保存文件
doc.SaveToFile("result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result.docx");
測試結果:
C#全部代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertImgToComment_Doc { class Program { static void Main(string[] args) { //實例化Document類,載入文檔 Document doc = new Document(); doc.LoadFromFile("testfile.docx"); //獲取需要添加批註的段落 Paragraph paragraph = doc.Sections[0].Paragraphs[2]; //添加文本批註內容、批註作者 Comment comment = paragraph.AppendComment("探索黑科技,小米為發燒而生!"); comment.Format.Author = "Administor"; //實例化DocPicture類,載入圖片 DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile("mi.png"); docPicture.LoadImage(img); //插入圖片到批註 comment.Body.AddParagraph().ChildObjects.Add(docPicture); //保存文件並打開文檔 doc.SaveToFile("result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result.docx"); } } }View Code
VB.NET代碼:
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertImgToComment_Doc Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As Document = New Document() doc.LoadFromFile("testfile.docx") Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2) Dim comment As Comment = paragraph.AppendComment("探索黑科技,小米為發燒而生!") comment.Format.Author = "Administor" Dim docPicture As DocPicture = New DocPicture(doc) Dim img As Image = Image.FromFile("mi.png") docPicture.LoadImage(img) comment.Body.AddParagraph().ChildObjects.Add(docPicture) doc.SaveToFile("result.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("result.docx") End Sub End Class End NamespaceView Code
2.讀取Word批註
步驟 1 :添加using指令
using System.Text; using System.IO; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields;
步驟 2 :創建實例,載入文檔
Document doc = new Document(); doc.LoadFromFile("test.docx");
步驟 3 :將批註內容寫入Txt文檔
//實例化StringBuilder類 StringBuilder SB = new StringBuilder(); //遍歷所有word批註,將批註內容寫入Txt文檔 foreach (Comment comment in doc.Comments) { foreach (Paragraph p in comment.Body.Paragraphs) { SB.AppendLine(p.Text); } } File.WriteAllText("CommentExtraction.txt", SB.ToString()); System.Diagnostics.Process.Start("CommentExtraction.txt");
C# 全部代碼:
using System.Text; using System.IO; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace ExtractComments { class Program { static void Main(string[] args) { //創建實例,載入文檔 Document doc = new Document(); doc.LoadFromFile("test.docx"); //實例化StringBuilder類 StringBuilder SB = new StringBuilder(); //遍歷所有word批註,將批註內容寫入Txt文檔 foreach (Comment comment in doc.Comments) { foreach (Paragraph p in comment.Body.Paragraphs) { SB.AppendLine(p.Text); } } File.WriteAllText("CommentExtraction.txt", SB.ToString()); System.Diagnostics.Process.Start("CommentExtraction.txt"); } } }View Code
VB.NET 代碼
Imports System.Text Imports System.IO Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace ExtractComments Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As Document = New Document() doc.LoadFromFile("test.docx") Dim SB As StringBuilder = New StringBuilder() For Each comment As Comment In doc.Comments For Each p As Paragraph In comment.Body.Paragraphs SB.AppendLine(p.Text) Next Next File.WriteAllText("CommentExtraction.txt", SB.ToString()) System.Diagnostics.Process.Start("CommentExtraction.txt") End Sub End Class End NamespaceView Code
3. 回覆Word批註內容
步驟 1 :添加using指令
using Spire.Doc; using Spire.Doc.Fields;
步驟 2 :創建實例
Document doc = new Document(); doc.LoadFromFile("test.docx");
步驟 3 :獲取批註
Comment comment = doc.Comments[0];
步驟 4 :回覆批註
Comment replyComment = new Comment(doc); replyComment.Format.Author = "Adam"; replyComment.Body.AddParagraph().AppendText("這條批註內容請再豐富一下,內容有些單調"); comment.ReplyToComment(replyComment);
步驟 5 :保存文件
doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ReplyToComment.docx");
測試結果:
C# 全部代碼:
using Spire.Doc; using Spire.Doc.Fields; namespace ReplyComment_Doc { class Program { static void Main(string[] args) { //實例化Document類,載入文件 Document doc = new Document(); doc.LoadFromFile("test.docx"); //獲取第一個批註 Comment comment = doc.Comments[0]; //實例化Comment類,添加批註回覆作者以及回覆內容 Comment replyComment = new Comment(doc); replyComment.Format.Author = "Adam"; replyComment.Body.AddParagraph().AppendText("這條批註內容請再豐富一下,內容有些單調"); comment.ReplyToComment(replyComment); //保存文件並打開 doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ReplyToComment.docx"); } } }View Code
VB.NET 代碼:
Imports Spire.Doc Imports Spire.Doc.Fields Namespace ReplyComment_Doc Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As Document = New Document() doc.LoadFromFile("test.docx") Dim comment As Comment = doc.Comments(0) Dim replyComment As Comment = New Comment(doc) replyComment.Format.Author = "Adam" replyComment.Body.AddParagraph().AppendText("這條批註內容請再豐富一下,內容有些單調") comment.ReplyToComment(replyComment) doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("ReplyToComment.docx") End Sub End Class End NamespaceView Code
以上為本次關於操作Word批註的全部內容。
(本文完)
如需轉載,請註明出處。