Introduction to the Infragistics Word Library

Atanas Dyulgerov / Friday, February 3, 2012

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:

  • InfragisticsSL4.v11.2
  • InfragisticsSL4.Documents.IO.v11.2

You would normally use the library in code behind so you need to add the following namespace reference you your code:

  1. 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:

  1. private void CreateBasicDoc(object sender, RoutedEventArgs e)
  2. {
  3.     try
  4.     {
  5.         MemoryStream documentStream = new MemoryStream();
  6.         WordDocumentWriter docWriter =
  7.             WordDocumentWriter.Create(documentStream);
  9.         docWriter.DocumentProperties.Title =
  10.             "My first word doc";
  11.         docWriter.DocumentProperties.Author =
  12.             "Infragistics WordDocumentWriter";
  14.         docWriter.StartDocument();
  16.         docWriter.StartParagraph();
  17.         docWriter.AddTextRun("Hello world!");
  18.         docWriter.EndParagraph();
  20.         docWriter.EndDocument();
  22.         docWriter.Close();
  24.         SaveDocument(documentStream);
  25.     }
  26.     catch (Exception errmsg)
  27.     {
  28.         MessageBox.Show(errmsg.Message);
  29.     }
  30. }
  32. private void SaveDocument(MemoryStream documentStream)
  33. {
  34.     SaveFileDialog dialog = new SaveFileDialog
  35.     {
  36.         Filter = "Word files|*.docx",
  37.         DefaultExt = "docx"
  38.     };
  40.     bool? showDialog = dialog.ShowDialog();
  41.     if (showDialog == true)
  42.     {
  43.         using (Stream exportStream = dialog.OpenFile())
  44.         {
  45.             byte[] data = documentStream.ToArray();
  46.             exportStream.Write(data, 0, data.Length);
  47.             exportStream.Close();
  48.         }
  49.     }
  50. }

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.

  1. Infragistics.Documents.Word.Font headingFont =
  2.     docWriter.CreateFont();
  3. headingFont.Bold = true;
  4. headingFont.Size = 15;
  5. headingFont.Underline =
  6.     Infragistics.Documents.Word.Underline.Double;
  8. Infragistics.Documents.Word.ParagraphProperties paraProps =
  9.     Infragistics.Documents.Word.ParagraphProperties.Create(docWriter);
  10. paraProps.SpacingAfter = (float)12;
  11. paraProps.Alignment =
  12.     Infragistics.Documents.Word.ParagraphAlignment.Center;

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:

  1. docWriter.StartParagraph(paraProps);
  2. 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!