1. How to Use C# Insert Comments in Word
This article is mainly telling you how to use C# insert comments in Word.
Comments in MS Word like track changes which used to insert information without overwriting
on original text. Usually comments are for readers to ask author questions or indicate an area that
is unclear. Or comments are for authors to make it easier to understand his/her ideas. It’s very
useful to insert comments in Word.
Insert comments in Word with MS Word can be very easy if you have MS Office installed on your
system. This guide can be found here. Here I want to introduce another solution which is use C#
insert comments in Word. Through C#, there is possible to edit Word without MS Office installed.
Spire.Doc, a .NET Word component can help us use C# insert comments in Word and also can do
other editable jobs.
Download Spire.Doc (or Spire.Office which include Spire.Doc) with .NET Framework 2.0 (or
above) together.
Spire.Doc presents you an easy way to insert a comment into the word document. There are two
forms of the comment in Spire.Doc, simple and complex comments.
Insert Simple Comments
Suppose we have written text in the word document. Now we can specify a position to make a
comment. Also we can set the author and the initial of the comment in comment.Format.Author
and comment.Format.Initial methods. Use the code below to use C# insert comments in Word:
01 using System;
02 using System.Drawing;
03 using Spire.Doc;
04 using Spire.Doc.Documents;
05 using Spire.Doc.Fields;
06
07 namespace MailMerge
08 {
09 class Program
10 {
11 static void Main(string[] args)
12 {
13 //Create a new document
14 Document document = new Document();
15
16 //Create a section
17 Section section = document.AddSection();
18
2. 19 //Add a paragraph
20 Paragraph paragraph = section.AddParagraph();
21
22 //Style
23 ParagraphStyle style = new ParagraphStyle(section.Document);
24 style.Name = "style";
25 style.ApplyBaseStyle(style.Name);
26 style.CharacterFormat.Font = new Font("Arial", 10f);
27 section.Document.Styles.Add(style);
28
29 //Add a text
30 String str = "Science (from the Latin scientia, meaning "knowledge") ";
31 paragraph.AppendText(str);
32
33 //Simple comment
34 Comment comment = paragraph.AppendComment("Not given in this file.");
35 comment.Format.Author = "Harry Hu";
36 comment.Format.Initial = "HH";
37 paragraph.ApplyStyle(style.Name);
38
39 //Save doc file.
40 document.SaveToFile("Sample.doc", FileFormat.Doc);
41
42 //Launching the MS Word file.
43 System.Diagnostics.Process.Start("Sample.doc");
44 }
45 }
46 }
Insert Complex Comments:
To insert complex comments, we need use paragraph.AppendComment("comment text") method
to insert comment into the document. And then, use comment.AddItem(text) to specify the text
which we are making comment for. We can set the author and initial of the comment in
comment.Format.Author and comment.Format.Initial methods. The code below is for you to use
C# insert complex comments.
01 using System;
02 using System.Drawing;
03 using Spire.Doc;
04 using Spire.Doc.Documents;
05 using Spire.Doc.Fields;
06
07 namespace MailMerge
3. 08 {
09 class Program
10 {
11 static void Main(string[] args)
12 {
13 //Create a new document
14 Document document = new Document();
15
16 //Create a section
17 Section section = document.AddSection();
18
19 //Add a paragraph
20 Paragraph paragraph = section.AddParagraph();
21
22 //style
23 ParagraphStyle style = new ParagraphStyle(section.Document);
24 style.Name = "style";
25 style.CharacterFormat.FontName = "Arial";
26 style.CharacterFormat.FontSize = 9;
27 style.ParagraphFormat.LineSpacing = 1.5F * 12F;
28 style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
29 section.Document.Styles.Add(style);
30
31 //Add a paragraph
32 paragraph = section.AddParagraph();
33 paragraph.AppendText("(All text and pictures are from ");
34
35 //Append the text which you want to make comment for
36 TextRange text = paragraph.AppendText("Wikipedia");
37
38 //Make comment
39 Comment comment =
paragraph.AppendComment("http://en.wikipedia.org/wiki/Science");
40 comment.AddItem(text);
41 comment.Format.Author = "Harry Hu";
42 comment.Format.Initial = "HH";
43
44 //Continue the paragraph
45 paragraph.AppendText(", the free encyclopedia)");
46 paragraph.ApplyStyle(style.Name);
47
48 //Save doc file.
49 document.SaveToFile("Sample.doc", FileFormat.Doc);
50
4. 51 //Launching the MS Word file.
52 System.Diagnostics.Process.Start("Sample.doc");
53 }
54 }
55 }
Full Demo
More about Spire.Doc
Download Spire.Doc
Purchase Spire.Doc