How to Write Comments in Word by Using C#
When reading an article, we may write comments including our idea or questions in the blank. Also, author may write comments to explain some difficult words or show the original source of the word. Comments can be written at the head or end of an article, beside words, sentences. Comments are useful for pointing out errors in one sentence or key words in one paragraph. Therefore, we may need to add comments in electronic documents such as MS Word.
It is easy to add comments in MS Word. We just open one document and find the words or sentences we want to add comments. Then select and click Create New Comment in Review tab (Word 2007). And write your words in the comment box. After that, the comment has been added.
Now, I want to introduce another method to write comments in Word with C#. In this method, a component: Spire.Doc is used. If you want to use the following code, please download and install this component. After that, add DLL file in your project.
private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertComments(document.AddSection());
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertComments(Section section)
{
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a comment into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendText("Spire.Doc can generate, modify, convert, render and print documents without utilizing Microsoft Word.");
paragraph.AppendComment("This is a comment");
paragraph = section.AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
|