Order
In the previous article, how to insert, modify, and delete word annotations in C # describes some ways to manipulate word annotations, and in this article you will continue to explain how to manipulate word annotations. The following three scenarios are described:
- Insert a picture into a word annotation
- Reading word annotations
- Reply to Word annotations
The tools you need
- Spire.doc for. NET 6.3 (Community Edition)
- Visual Stuido
Example code 1. Insert a picture into a word annotation
"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) {//Instantiate document class, loading Document Documents DOC = new document (); Doc. LoadFromFile ("Testfile.docx"); Gets the paragraph that needs to be annotated Paragraph Paragraph = doc. Sections[0]. PARAGRAPHS[2]; Add text annotation content, annotation author Comment Comment = paragraph. Appendcomment ("Explore black technology, millet for a fever born!"); Comment. Format.author = "Administor"; Instantiate the Docpicture class, load the picture docpicture docpicture = new Docpicture (DOC); Image img = image.fromfile ("Mi.png"); Docpicture.loadimage (IMG); Inserts a picture into an annotation comment. Body.addparagraph (). Childobjects.add (docpicture); Save the file and open the document Doc. SaveToFile ("Result.docx", fileformat.docx2013); System.Diagnostics.Process.Start ("Result.docx"); } }}
"VB.net"
Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingNamespace 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 ClassEnd Namespace
Test the effect:
2. Read Word annotation content
"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"); } }}
"VB.net"
Imports System.TextImports System.IOImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace 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 ClassEnd Namespace
Read Result:
3. Reply to Word annotations
"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"); } }}
"VB.net"
Imports Spire.DocImports Spire.Doc.FieldsNamespace 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 ClassEnd Namespace
Test results:
Note that in reply Word comment this function, the free version of Spire.doc temporarily does not support, must use the commercial edition.
The above is all about manipulating word annotations.
(End of this article)
If you want to reprint, please indicate the source.
C#/vb.net Working with Word annotations (ii)--How to insert a picture, read, reply to Word annotation content