How to Add Comment in Word Document with Silverlight

Comments can be taken as reviews or additional information for an article. On one hand, comments are used to present readers’ thoughts about article and give suggestions to readers. On the other hand, comments show some information which is not the main content in article but very important. Microsoft Word offers one function to users to insert comment for some special sentences or text. And in this post, I will introduce the method about how to add comment in Word with Silverlight.

I prepare a Word document which is about Antarctic and I will insert a comment for the first paragraph. This comment presents the original source of contents in this document.

I use a component, Spire.Doc for Silverlight in this example. If you want to use the following code, please make sure that you have installed Silverlight 4 and Spire.Doc for Silverlight. What’s more, Spire.Doc for Silverlight dll file must be added as reference in project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as InsertComment.xaml. Double click it to design UserControl. I set its background as an image at the beginning. Then, I add a label to and change the contents as what I will do next and set format for label contents. Finally, add a button to run.

Step 2. Declare SaveFileDialog

Declare a saveFileDialog for saving document. Also, set filter for this saveFileDialog, which is used to choose format. And I set the format as .docx.

        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            this.saveFileDialog.Filter = “Word Document(*.docx)|*.docx”;
        }

Step 3. Load Document

Right click project to add existed item which is the document which I want to add comment. After adding, click this document to change its Build Action as Embedded Resource.

Double Click RUN button to write code. Declare a new document at the beginning. Next, use foreach sentence to get document name from assembly. If the name is the same with name of embedded resource, load the document.

            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“Antarctic.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

Step 4. Add Comment

Get section in document and get paragraph which I want to add comment in this section. Then, declare a string which is the comment contents, and use paragraph.AppendComment() method to add comment. The parameter passed to this method is string. Then, set comment author and initial.

            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];

            string str = “All the contents in this document is from Wikipedia. If you want to learn more information about Antarctic, please visit http://en.wikipedia.org/wiki/Antarctic.”;
            Comment comment = paragraph.AppendComment(str);
            comment.Format.Author = “Wikipedia”;
            comment.Format.Initial = “C”;

Step 5. Save Document

Judge if the saveFileDialog which I declare at step 1 can pop up. If yes, save the encrypted document through it.

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }

Full WordTable.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordComment
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            this.saveFileDialog.Filter = “Word Document(*.docx)|*.docx”;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“Antarctic.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];

            string str = “All the contents in this document is from Wikipedia. If you want to learn more information about Antarctic, please visit http://en.wikipedia.org/wiki/Antarctic.”;
            Comment comment = paragraph.AppendComment(str);
            comment.Format.Author = “Wikipedia”;
            comment.Format.Initial = “C”;

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }
        }
    }
}

RESULT

______________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

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.

16 thoughts on “How to Add Comment in Word Document with Silverlight”

  1. Greetings! Very useful advice in this particular post!
    It is the little changes which will make the most significant changes.
    Many thanks for sharing!

  2. The other day, while I was at work, my cousin stole my iPad and tested to see if it can survive a forty
    foot drop, just so she can be a youtube sensation. My apple
    ipad is now broken and she has 83 views. I know this is completely off topic
    but I had to share it with someone!

  3. I’m really enjoying the design and layout of your website. It’s a very easy
    on the eyes which makes it much more pleasant for me to come here and
    visit more often. Did you hire out a designer to create
    your theme? Great work!

  4. The sales action uses exactly what the marketing action has produced in order
    to attain profit to the organization. In some ballpoint pens the
    end and the reservoir could be retracted to the body on the pen once
    the pen will not be in use, which can help prevent any longer drying out from the tip, and causes it to become safe to place away in to a jacket or
    shirt pocket etc. Marketing can be so much broader in scope, and promotion is tool
    in the region of marketing, usedin the top picture of selling strategy for businesses.

  5. Hey just stumbled upon your blog via Yahoo after I typed in,
    “How to Add Comment in Word Document with Silverlight | janewdaisy” or something similar (can’t quite remember exactly). Anyways, I’m delighted I found it simply because your subject material is exactly what I’m searching for (writing a university paper) and I hope you don’t mind if I collect some material from here
    and I will of course credit you as the source. Thank you so much.

  6. Fantastic beat ! I would like to apprentice while you amend your web site,
    how could i subscribe for a blog site? The account helped me a
    acceptable deal. I had been a little bit acquainted of this your broadcast
    provided bright clear concept

  7. An intriguing discussion is definitely worth comment.
    I think that you should publish more about this issue,
    it might not be a taboo subject but usually people don’t discuss such issues.

    To the next! Kind regards!!

  8. Oh my goodness! Incredible article dude!
    Thank you, However I am going through troubles with your RSS.
    I don’t know the reason why I am unable to join it.
    Is there anyone else having identical RSS issues?
    Anyone who knows the answer will you kindly respond?
    Thanks!!

  9. I really love your blog.. Very nice colors & theme. Did you make this
    web site yourself? Please reply back as I’m looking to
    create my very own blog and would like to find out where you got
    this from or exactly what the theme is named.
    Cheers!

  10. This design is wicked! You obviously know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job.
    I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

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