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.
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.
public partial class MainPage : UserControl
private SaveFileDialog 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(“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.”;
bool? result = this.saveFileDialog.ShowDialog();
if (result.HasValue && result.Value)
using (Stream stream = this.saveFileDialog.OpenFile())
LEARN MORE about Spire.Doc for Silverlight
Freely DOWNLOAD Spire.Doc for Silverlight