One of the hot new additions to the NetAdvantage for Silverlight Line of Business 11.2 release in the Infragistics Word library. It enables you to create high quality Microsoft® Word® documents in the widely-interoperable DOC/DOCX/WordML file format. It is completely independent from the Microsoft office software and allows you to run your application without requiring to install word alongside it. It supports most of the word document features that you might need, like text formatting, tables, hyperlinks, images, headers, footers, page numbers and shapes. This article will show you how to take advantage of this new library.
The assemblies you need to add to your project are:
You would normally use the library in code behind so you need to add the following namespace reference you your code:
- using Infragistics.Documents.Word;
Lets imagine we have a button that triggers the creation of the word document. We are going to create the doc file in a MemoryStream and then prompt the user for a location to save it and wrtie the stream there. Here is how its Click event handler and the method that saves to file would look like:
- private void CreateBasicDoc(object sender, RoutedEventArgs e)
- MemoryStream documentStream = new MemoryStream();
- WordDocumentWriter docWriter =
- docWriter.DocumentProperties.Title =
- "My first word doc";
- docWriter.DocumentProperties.Author =
- "Infragistics WordDocumentWriter";
- docWriter.AddTextRun("Hello world!");
- catch (Exception errmsg)
- private void SaveDocument(MemoryStream documentStream)
- SaveFileDialog dialog = new SaveFileDialog
- Filter = "Word files|*.docx",
- DefaultExt = "docx"
- bool? showDialog = dialog.ShowDialog();
- if (showDialog == true)
- using (Stream exportStream = dialog.OpenFile())
- byte data = documentStream.ToArray();
- exportStream.Write(data, 0, data.Length);
Lets go line by line in the code that we just saw. First we care a WordDocumentWriter object using the Create method of the class. As parameters it takes either a stream or a string path to a file. Then we could set some properties of the document, like its title, author, company, comments, etc.
Once we have started the document we can have two types of containers of sorts - paragraphs and tables. They are added with StartPharagraph and StartTable methods. All of them and the document that are “Started” have to be ended at some point. And then the main document writer Closed. The simple sample above shows that.
A paragraph can contain a number of items such as text runs, hyperlinks, new lines, anchored or inline pictures and shapes,etc. All the methods that you might need to do that start with Add – like AddTextRun() in the sample above. The table is similar, but a little bit more complicated. You need to define rows and columns and add cells to the table. Each cell then can be used like the paragraph to add other items in it. The will be some code that shows how to add a table later in this article.
Once the document is started along with a paragraph and some textruns in it and then closed we can use the stream to save the info in a file. The code for that is pretty standard, we use the SaveFileDialog and a filestream to write the binary data from our document stream.
Now that I’ve explained how to create a really basic document I want to show you how to spice it up a bit. Lets say we want a paragraph to have some line spacing and alignment and a the font to have size, color and bold style. The way to go is first define Font properties and ParagraphProperties objects that hold the information. The following code will show you how to do that. After you have those objects you can assign them to various elements as you add them in the document.
- Infragistics.Documents.Word.Font headingFont =
- headingFont.Bold = true;
- headingFont.Size = 15;
- headingFont.Underline =
- Infragistics.Documents.Word.ParagraphProperties paraProps =
- paraProps.SpacingAfter = (float)12;
- paraProps.Alignment =
As you see the WordDocumentWriter class has CreateFont and CreateParagraphProperties methods that create instances that expose the configurable properties. Once you have those objects you can use them like this:
- docWriter.AddTextRun("Hello world!", headingFont);
They can be reused in the next items you want to assign them.
As I mentioned before you can use the library to create much more complicated documents that include formatted text, tables, images, hyperlinks, shapes, headers, footers. You use this link to our samples page to see how to add these to your doc easily. Just go to the code behind file in the code viewer and you will have all you need to do it. It also has lots of inline comments.
I hope this has been interesting to you.
Have a great day!