A comment in Word can contain rich elements such as text, image, OLE object, and etc. This article presents how we can insert a picture to a comment in Word using Spire.Doc.
Step 1: Initialize an instance of Document class and load a sample document.
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
Step 2: Get the third paragraph from section one.
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
Step 3: Append a comment to the paragraph.
Comment comment = paragraph.AppendComment("This is a comment."); comment.Format.Author = "E-iceblue";
Step 4: Insert an image to comment body.
DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png"); docPicture.LoadImage(img); comment.Body.AddParagraph().ChildObjects.Add(docPicture);
Step 5: Save the file.
doc.SaveToFile("result.docx",FileFormat.Docx2013);
Output:
Full Code:
[C#]
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertPicture { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx"); Paragraph paragraph = doc.Sections[0].Paragraphs[2]; Comment comment = paragraph.AppendComment("This is a comment."); comment.Format.Author = "E-iceblue"; DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png"); docPicture.LoadImage(img); comment.Body.AddParagraph().ChildObjects.Add(docPicture); doc.SaveToFile("result.docx", FileFormat.Docx2013); } } }
[VB.NET]
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertPicture Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx") Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2) Dim comment As Comment = paragraph.AppendComment("This is a comment.") comment.Format.Author = "E-iceblue" Dim docPicture As New DocPicture(doc) Dim img As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.png") docPicture.LoadImage(img) comment.Body.AddParagraph().ChildObjects.Add(docPicture) doc.SaveToFile("result.docx", FileFormat.Docx2013) End Sub End Class End Namespace