How to Insert Picture into Comment in Word in C#, VB.NET

2017-03-31 06:27:20 Written by  support iceblue
Rate this item
(0 votes)

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:

How to Insert Picture into Comment in Word in C#, VB.NET

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

Additional Info

  • tutorial_title: Insert Picture into Comment in Word in C#, VB.NET
Last modified on Friday, 03 September 2021 03:50