WordprocessingDocument Class

Defines WordprocessingDocument - an OpenXmlPackage represents a Word document.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer
    DocumentFormat.OpenXml.Packaging.OpenXmlPackage
      DocumentFormat.OpenXml.Packaging.WordprocessingDocument

Namespace:  DocumentFormat.OpenXml.Packaging
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

Syntax

'Declaration
Public Class WordprocessingDocument _
    Inherits OpenXmlPackage
'Usage
Dim instance As WordprocessingDocument
public class WordprocessingDocument : OpenXmlPackage

Examples

The following example shows how to apply the "Heading3" style to the first paragraph in an existing word processing document. To run the code example, create a word-processing file and write some text in it. After you run the code example, examine the text in the file. You would notice that the style of the first paragraph is changed to “Heading3.”

using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace WordProcessingEx
{
    class Program
    {
        static void Main(string[] args)
        {
            // Apply the Heading 3 style to a paragraph. 
            string fileName = @"C:\Users\Public\Documents\WordProcessingEx.docx";
            using ( WordprocessingDocument myDocument = WordprocessingDocument.Open(fileName, true))
            {
                // Get the first paragraph.
                Paragraph p = myDocument.MainDocumentPart.Document.Body.Elements<Paragraph>().First();

                // If the paragraph has no ParagraphProperties object, create a new one.
                if ( p.Elements<ParagraphProperties>().Count() == 0 )
                    p.PrependChild<ParagraphProperties>(new ParagraphProperties());

                // Get the ParagraphProperties element of the paragraph.
                ParagraphProperties pPr = p.Elements<ParagraphProperties>().First();

                // Set the value of ParagraphStyleId to "Heading3".
                pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading3" };
            }
            Console.WriteLine("All done. Press a key.");
            Console.ReadKey();
        }
   }
}
Imports System
Imports System.Linq
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Module Module1
    Sub Main()
        ' Apply the Heading 3 style to a paragraph. 
        Dim fileName As String = "C:\Users\Public\Documents\WordProcessingEx.docx"
        Using myDocument As WordprocessingDocument = WordprocessingDocument.Open(fileName, True)
            ' Get the first paragraph.
            Dim p As Paragraph = myDocument.MainDocumentPart.Document.Body.Elements(Of Paragraph)().First()

            ' If the paragraph has no ParagraphProperties object, create a new one.
            If p.Elements(Of ParagraphProperties)().Count() = 0 Then
                p.PrependChild(Of ParagraphProperties)(New ParagraphProperties())
            End If

            ' Get the ParagraphProperties element of the paragraph.
            Dim pPr As ParagraphProperties = p.Elements(Of ParagraphProperties)().First()

            ' Set the value of ParagraphStyleId to "Heading3".
            pPr.ParagraphStyleId = New ParagraphStyleId() With {.Val = "Heading3"}
        End Using
        Console.WriteLine("All done. Press a key.")
        Console.ReadKey()
    End Sub
End Module

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

WordprocessingDocument Members

DocumentFormat.OpenXml.Packaging Namespace