Insert Comments in Word and Set Comments Format with C#/VB.NET

Word Comment is inserted in some specified place, where reviewers will give suggestions to use a more appropriate word or sentence in document. It is very useful to express our own ideas. Also, we can use comments to show references or notes. For example, if contents in one paragraph come from a book or website, we can add book name or website address as comment.

In this post, I want to show how to insert comment in Word with C#/VB.NET. In my example, I will add two comments in my document. The first one is URL. By clicking it, we can get details about cue words. The second is text comment, which presents a brief introduction about cue words.

Note: I use the .NET Word component, Spire.Doc for .NET for realizing this function more easily and quickly.

Detailed Step Shown as Following:

  1. Create a new document. Then, add section and paragraph.
  2. Define style 1, which will be applied for paragraph 1. The style includes font type, size, color, alignment and paragraph line spacing.
  3. Add URL comment. Firstly, add cue words and text which I want to give comment. Secondly, insert comment, which is url address. Thirdly, add comments item and set comment format. Apply style1.
  4. Define style 2 and add text comments as step 2 and step 3.
  5. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordComment
{
    class Comments
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();

            //Add Section and Paragraph
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Add Style1
            ParagraphStyle style1 = new ParagraphStyle(section.Document);
            style1.Name = “style1”;
            style1.CharacterFormat.Font = new Font(“Calibri”, 12f);
            style1.CharacterFormat.Bold = true;
            style1.CharacterFormat.TextColor = Color.DarkCyan;
            style1.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;
            style1.ParagraphFormat.LineSpacing = 3F * 12F;
            style1.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
            section.Document.Styles.Add(style1);

            //Add URL Comment
            paragraph.AppendText(“URL Comments: “);
            TextRange text = paragraph.AppendText(“Rolling in the Deep”);
            Comment comment1 = paragraph.AppendComment(http://en.wikipedia.org/wiki/Rolling_in_the_Deep”);
            comment1.AddItem(text);
            comment1.Format.Author = “MS”;
            comment1.Format.Initial = “CI”;
            paragraph.ApplyStyle(style1.Name);

            //Add Style2
            ParagraphStyle style2 = new ParagraphStyle(section.Document);
            style2.Name = “style2”;
            style2.ApplyBaseStyle(style1.Name);
            style2.CharacterFormat.Font = new Font(“Century Gothic”, 12f);
            style2.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;
            style2.CharacterFormat.Bold = true;
            style2.CharacterFormat.TextColor = Color.DarkOliveGreen;
            section.Document.Styles.Add(style2);

            //Add Text Comment
            Paragraph paragraph1 = section.AddParagraph();
            paragraph1.AppendText(“Text Comment: “);
            TextRange text2 = paragraph1.AppendText(“Poker Face”);
            string str = “An uptempo dance-pop song in the key of G# minor,”
            + “\”Just Dance\” but with a darker musical tone. “
            + “The main idea behind the song is bisexuality and was a tribute by Gaga to her rock n’ roll boyfriends.”
            + “Lyrically, \”Poker Face\” is about sex and gambling.”;
            Comment comment2 = paragraph1.AppendComment(str);
            comment2.AddItem(text2);
            comment2.Format.Author = “Harry Hu”;
            comment2.Format.Initial = “CII”;
            paragraph1.ApplyStyle(style2.Name);

            //Save and Luanch
            document.SaveToFile(“Comments.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Comments.docx”);
        }
    }
}

VB.NET

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace WordComment
    Friend Class Comments
        Shared Sub Main(ByVal args() As String)
            ‘Create Document
            Dim document As New Document()

            ‘Add Section and Paragraph
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Add Style1
            Dim style1 As New ParagraphStyle(section.Document)
            style1.Name = “style1”
            style1.CharacterFormat.Font = New Font(“Calibri”, 12.0F)
            style1.CharacterFormat.Bold = True
            style1.CharacterFormat.TextColor = Color.DarkCyan
            style1.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right
            style1.ParagraphFormat.LineSpacing = 3.0F * 12.0F
            style1.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple
            section.Document.Styles.Add(style1)

            ‘Add URL Comment
            paragraph.AppendText(“URL Comments: “)
            Dim text As TextRange = paragraph.AppendText(“Rolling in the Deep”)
            Dim comment1 As Comment = paragraph.AppendComment(http://en.wikipedia.org/wiki/Rolling_in_the_Deep”)
            comment1.AddItem(text)
            comment1.Format.Author = “MS”
            comment1.Format.Initial = “CI”
            paragraph.ApplyStyle(style1.Name)

            ‘Add Style2
            Dim style2 As New ParagraphStyle(section.Document)
            style2.Name = “style2”
            style2.ApplyBaseStyle(style1.Name)
            style2.CharacterFormat.Font = New Font(“Century Gothic”, 12.0F)
            style2.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right
            style2.CharacterFormat.Bold = True
            style2.CharacterFormat.TextColor = Color.DarkOliveGreen
            section.Document.Styles.Add(style2)

            ‘Add Text Comment
            Dim paragraph1 As Paragraph = section.AddParagraph()
            paragraph1.AppendText(“Text Comment: “)
            Dim text2 As TextRange = paragraph1.AppendText(“Poker Face”)
            Dim str As String = “An uptempo dance-pop song in the key of G# minor,” & “””Just Dance”” but with a darker musical tone. “ & “The main idea behind the song is bisexuality and was a tribute by Gaga to her rock n’ roll boyfriends.” & “Lyrically, “”Poker Face”” is about sex and gambling.”
            Dim comment2 As Comment = paragraph1.AppendComment(str)
            comment2.AddItem(text2)
            comment2.Format.Author = “Harry Hu”
            comment2.Format.Initial = “CII”
            paragraph1.ApplyStyle(style2.Name)

            ‘Save and Launch
            document.SaveToFile(“Comments.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Comments.docx”)
        End Sub
    End Class
End Namespace

Result:

Download Spire.Doc for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

8 thoughts on “Insert Comments in Word and Set Comments Format with C#/VB.NET”

  1. Somebody necessarily lend a hand to make severely articles I might state.
    This is the first time I frequented your website page and so far?

    I amazed with the analysis you made to make this particular post extraordinary.
    Excellent activity!

  2. I think this is among the most important info for me.

    Andd i am glad reading your article. But should remark on some
    general things, The website style is great, the adticles is really great : D.
    Good job, cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s