Version

Creating a Word Document

This topic illustrates how to create a Word document using the forward-only WordDocumentWriter streamer object. The static Create method of the WordDocumentWriter object creates a new Word document.

The following screenshot shows a Word document created with text and hyper link:

Word Create a Word Document 01.png

The various properties of the document such as Author, Title, Subject etc., can be set using the DocumentProperties property of the WordDocumentWriter object. These information can be accessed in Word 2007 by clicking the Office Button and navigating to Prepare > Properties section. Similarly in Word 2010 by clicking the File tab the document properties can be accessed from the right side of the backstage view.

In order to start writing into the Word document use the StartDocument method that must be balanced with a corresponding call to the EndDocument method.

A Paragraph provides the ability to display a block of text which can be aligned or indented. Use the StartParagraph method to begin a paragraph. The AddTextRun method provides a way to add content to the paragraph. Once content is added the paragraph must be closed using the EndParagraph method.

Note
Note

A reference to the Infragistics3.Documents.IO assembly is required for the following code.

Note
Note

When using the WordDocumentWriter object for creating Word documents the streamer object must be disposed or closed using either the Dispose or Close method.

In C#:

using Infragistics.Documents.Word;
// Create a new instance of the WordDocumentWriter class
// using the static 'Create' method.
WordDocumentWriter docWriter = WordDocumentWriter.Create(@"C:\TestWordDoc.docx");
// Use inches as the unit of measure
docWriter.Unit = UnitOfMeasurement.Inch;
// Set the document properties, such as title, author, etc.
docWriter.DocumentProperties.Title = "Sample Document";
docWriter.DocumentProperties.Author = string.Format("Infragistics.{0}", SystemInformation.UserName);
// Start the document...note that each call to
// StartDocument must be balanced with a corresponding call to EndDocument.
docWriter.StartDocument();
//  Start a paragraph
docWriter.StartParagraph();
//  Add a text run for the title
docWriter.AddTextRun("Paragraphs and Topic Sentences");
// Add a new line
docWriter.AddNewLine();
//  Add a Hyperlink
docWriter.AddHyperlink("http://ko.infragistics.com", "Infragistics Inc.");
//  End the paragraph
docWriter.EndParagraph();
//  End Document
docWriter.EndDocument();
// Close the writer
docWriter.Close();

In Visual Basic:

Imports Infragistics.Documents.Word
'  Create a new instance of the WordDocumentWriter class using the
'  static 'Create' method.
Dim docWriter As WordDocumentWriter = WordDocumentWriter.Create("C:\TestWordDoc.docx")
'  Use inches as the unit of measure
docWriter.Unit = UnitOfMeasurement.Inch
'  Set the document properties, such as title, author, etc.
docWriter.DocumentProperties.Title = "Sample Document"
docWriter.DocumentProperties.Author = String.Format("Infragistics.{0}", SystemInformation.UserName)
'  Start the document...note that each call to StartDocument must
'  be balanced with a corresponding call to EndDocument.
docWriter.StartDocument()
'  Start a paragraph
docWriter.StartParagraph()
'  Add a text run for the title
docWriter.AddTextRun("Paragraphs and Topic Sentences")
' Add a new line
docWriter.AddNewLine()
' Add a Hyperlink
docWriter.AddHyperlink("http://ko.infragistics.com", "Infragistics Inc.")
'End the paragraph
docWriter.EndParagraph()
docWriter.EndDocument()
' Close the writer
docWriter.Close()