How to Set Word Properties with Silverlight

As different people have different personalities, each Word document has own properties. We can take properties as a brief conclusion of document information. The conclusion includes document size, title, author, create data etc. Also, in order to have readers learned document more clearly, we can set Word properties by adding or modifying information. In this post, I want to introduce a method about how to set Word properties with Silverlight.

Although we can set properties for one document, some properties are generated automatically and cannot be changed, for example, location, created/modified date, size. So, in this example, I will set author, title, keywords, company and comments for document.

Note: I have used a component, Spire.Doc for Silverlight in my method so that please download and install it on your system if you want to use the following code. After that, add its dll file as reference in your project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml and double click it. At first, adjust UserControl size and set an image as its background. Then, add a label and change it contents as “Word Properties Setting with Silverlight”. Set font style, color, size and alignment for contents. At last, add a button to run. I set format for this button as well, including button background, border and contents format.

Step 2. Declare SaveFileDialog

Declare a SaveFileDialog for saving document. Then, set a filter for this SaveFileDialog. The filter format is set as .docx only.

Step 3. Load Document

Right click project to add existing item (the document which I want to set properties). After adding, change its Build Action as Embedded Resource.

Double click run button to write code. Declare document and assembly firstly. Then, use foreach sentence to get document name from assembly. If the name is the same as existing item name, load this document.

Step 4. Set Word Properties

Assign value for document. BuiltinDocumentProperties.Author/Category/Title/Company/Keywords/Comments. All the values are string.

Step 5. Save Document

Judge if the SaveFileDialog which has been declared in step 1 can pop up. If the result is ture, use document.SaveToStream() method to save document with SaveFileDialog.

Full Properties.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

using System.IO;

using Spire.Doc;

 

namespace PropertiesofWord

{

    public partial class MainPage : UserControl

    {

        private SaveFileDialog saveFileDialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFileDialog.Filter = “Word Document(*.docx)|*.docx”;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith(“Humor Them.docx”))

                {

                    using (Stream docStream = assembly.GetManifestResourceStream(name))

                    {

                        document = new Document(docStream, FileFormat.Docx);

                    }

                }

            }

 

            //Set Word Properties

            document.BuiltinDocumentProperties.Author = “Janewdaisy”;

            document.BuiltinDocumentProperties.Category = “Eassy”;

            document.BuiltinDocumentProperties.Title = “Humor Them”;

            document.BuiltinDocumentProperties.Company = “E-iceblue”;

            document.BuiltinDocumentProperties.Keywords = “Humor”;

            document.BuiltinDocumentProperties.Comments = “This is an example to show how to set Word properties with Silverlight.”;

 

            //Save Document

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

 

Result

 

___________________________________________________________________________________________

LEARN MORE about Spire.Doc for Silverlight

Freely DOWNLOAD Spire.Doc for Silverlight

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.

2 thoughts on “How to Set Word Properties with Silverlight”

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