How to: Open a Word Processing Document from a Stream

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

In this article
When to Open a Document from a Stream
Creating a WordprocessingDocument Object
Structure of a WordProcessingML Document
How the Sample Code Works
Sample Code

This topic shows how to use the classes in the Open XML SDK 2.0 for Microsoft Office to programmatically open a Word processing document from a stream.

The following assembly directives are required to compile the code in this topic.

using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

When to Open a Document from a Stream

If you have an application, such as a SharePoint application, that works with documents using stream input/output, and you want to employ the Open XML SDK 2.0 to work with one of the documents, this is designed to be easy to do. This is particularly true if the document exists and you can open it using the Open XML SDK 2.0. However, suppose the document is an open stream at the point in your code where you need to employ the SDK to work with it? That is the scenario for this topic. The sample method in the sample code accepts an open stream as a parameter and then adds text to the document behind the stream using the Open XML SDK 2.0.

Creating a WordprocessingDocument Object

In the Open XML SDK, the WordprocessingDocument class represents a Word document package. To work with a Word document, first create an instance of the WordprocessingDocument class from the document, and then work with that instance. When you create the instance from the document, you can then obtain access to the main document part that contains the text of the document. Every Open XML package contains some number of parts. At a minimum, a WordProcessingDocument must contain a main document part that acts as a container for the main text of the document. The package can also contain additional parts. Notice that in a Word document, the text in the main document part is represented in the package as XML using WordprocessingML markup.

To create the class instance from the document call the Open(Stream, Boolean) method. Several Open methods are provided, each with a different signature. The sample code in this topic uses the Open method with a signature that requires two parameters. The first parameter takes a handle to the stream from which you want to open the document. The second parameter is either true or false and represents whether the stream is opened for editing.

The following code example calls the Open method.

// Open a WordProcessingDocument based on a stream.
WordprocessingDocument wordprocessingDocument = 
    WordprocessingDocument.Open(stream, true);
' Open a WordProcessingDocument based on a stream.
Dim wordprocessingDocument As WordprocessingDocument = _
WordprocessingDocument.Open(stream, True)

Structure of a WordProcessingML Document

The basic document structure of a WordProcessingML document consists of the document and body elements, followed by one or more block level elements such as p, which represents a paragraph. A paragraph contains one or more r elements. The r stands for run, which is a region of text with a common set of properties, such as formatting. A run contains one or more t elements. The t element contains a range of text. For example, the WordprocessingML markup for a document that contains only the text "Example text." is shown in the following code example.

<w:document xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    <w:p>
      <w:r>
        <w:t>Example text.</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:document>

Using the Open XML SDK 2.0, you can create document structure and content using strongly-typed classes that correspond to WordprocessingML elements. You can find these classes in the DocumentFormat.OpenXml.Wordprocessing namespace. The following table lists the class names of the classes that correspond to the document, body, p, r, and t elements.

WordprocessingML Element

Open XML SDK 2.0 Class

Description

document

Document

The root element for the main document part.

body

Body

The container for the block-level structures such as paragraphs, tables, annotations, and others specified in the ISO/IEC 29500 specification.

p

Paragraph

A paragraph.

r

Run

A run.

t

Text

A range of text.

How the Sample Code Works

When you open the Word document package, you can add text to the main document part. To access the body of the main document part you assign a reference to the existing document body, as shown in the following code segment.

// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
' Assign a reference to the existing document body.
Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body

When you access to the body of the main document part, add text by adding instances of the Paragraph, Run, and Text classes. This generates the required WordprocessingML markup. The following lines from the sample code add the paragraph, run, and text.

// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
' Add new text.
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim run As Run = para.AppendChild(New Run())
run.AppendChild(New Text(txt))

Sample Code

The example OpenAndAddToWordprocessingStream method shown here can be used to open a Word document from an already open stream and append some text using the Open XML SDK. You can call it by passing a handle to an open stream as the first parameter and the text to add as the second. For example, the following code example opens the Word13.docx file in the Public Documents folder and adds text to it.

string strDoc = @"C:\Users\Public\Public Documents\Word13.docx";
string txt = "Append text in body - OpenAndAddToWordprocessingStream";
Stream stream = File.Open(strDoc, FileMode.Open);
OpenAndAddToWordprocessingStream(stream, txt);
stream.Close();
Dim strDoc As String = "C:\Users\Public\Documents\Word13.docx"
Dim txt As String = "Append text in body - OpenAndAddToWordprocessingStream"
Dim stream As Stream = File.Open(strDoc, FileMode.Open)
OpenAndAddToWordprocessingStream(stream, txt)
stream.Close()

Note

Notice that the OpenAddAddToWordprocessingStream method does not close the stream passed to it. The calling code must do that.

Following is the complete sample code in both C# and Visual Basic.

public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
{
    // Open a WordProcessingDocument based on a stream.
    WordprocessingDocument wordprocessingDocument = 
        WordprocessingDocument.Open(stream, true);
    
    // Assign a reference to the existing document body.
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

    // Add new text.
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text(txt));

    // Close the document handle.
    wordprocessingDocument.Close();
    
    // Caller must close the stream.
}
Public Sub OpenAndAddToWordprocessingStream(ByVal stream As Stream, ByVal txt As String)
    ' Open a WordProcessingDocument based on a stream.
    Dim wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(stream, true)

    ' Assign a reference to the existing document body.
    Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body

    ' Add new text.
    Dim para As Paragraph = body.AppendChild(New Paragraph)
    Dim run As Run = para.AppendChild(New Run)
    run.AppendChild(New Text(txt))

    ' Close the document handle.
    wordprocessingDocument.Close

    ' Caller must close the stream.
End Sub

See Also

Reference

Class Library Reference