Text Class

Text.When the object is serialized out as xml, its qualified name is w:t.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlLeafElement
      DocumentFormat.OpenXml.OpenXmlLeafTextElement
        DocumentFormat.OpenXml.Wordprocessing.TextType
          DocumentFormat.OpenXml.Wordprocessing.Text

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

Syntax

'Declaration
Public Class Text _
    Inherits TextType
'Usage
Dim instance As Text
public class Text : TextType

Remarks

[ISO/IEC 29500-1 1st Edition]

17.3.3.31 t (Text)

This element specifies that this run contains literal text which shall be displayed in the document. The t element shall be used for all text runs which are not:

  • Part of a region of text that is contained in a deleted region using the del element (§17.13.5.14)

  • Part of a region of text that is contained within a field code

[Example: Consider a paragraph of WordprocessingML content which reads This is text. This paragraph would therefore be represented as follows:

<w:p>
<w:r>
<w:t>This is text</w:t>
</w:r>
</w:p>

The text is contained in a t node. end example]

Parent Elements

r (§22.1.2.87); r (§17.3.2.25)

Attributes

Description

xml:space (Content Contains Significant Whitespace)

Namespace: http://www.w3.org/XML/1998/namespace

Specifies how white space should be handled for the contents of this element using the W3C space preservation rules.

[Example: Consider the following run contained within a WordprocessingML document:

<w:r>
<w:t>   significant whitespace   </w:t>
</w:r>

Although there are three spaces on each side of the text content in the run, that whitespace has not been specifically marked as significant, therefore it is subject to the space preservation rules currently specified in that run's scope. end example]

The possible values for this attribute are defined by §2.10 of the XML 1.0 specification.

[Note: The W3C XML Schema definition of this element’s content model (CT_Text) is located in §A.1. end note]

© ISO/IEC29500: 2008.

Examples

The following code example creates a document named "TextEx.docx" at the specified path, and writes the phrase "Hello, World!" to it.

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

namespace TextEx
{
    class Program
    {
        static void Main(string[] args)
        {
             string fileName = @"c:\users\public\documents\TextEx.docx";
             // Create a wordprocessing document with the specified file name.
            WordprocessingDocument wordprocessingDocument = 
                WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document);

            // Add a MainDocumentPart object.
            MainDocumentPart mainDocumentPart = 
                wordprocessingDocument.AddMainDocumentPart();
            // Creatre the document structure.
            mainDocumentPart.Document = new Document();
            Document document = mainDocumentPart.Document;
            Body body = document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            // Write some text to the file.
            run.AppendChild(new Text("Hello, World!"));

            // Close the document.
            wordprocessingDocument.Close();

            Console.WriteLine("The document has been created.\nPress a key");
            Console.ReadKey();
        }
    }
}
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Module Module1
    Sub Main(ByVal args As String())
        Dim fileName As String = "c:\users\public\documents\TextEx.docx"
        ' Create a wordprocessing document with the specified file name.
        Dim wordprocessingDocument As WordprocessingDocument = _
            wordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)

        ' Add a MainDocumentPart object.
        Dim mainDocumentPart As MainDocumentPart = _
            wordprocessingDocument.AddMainDocumentPart()
        ' Creatre the document structure.
        mainDocumentPart.Document = New Document()
        Dim document As Document = mainDocumentPart.Document
        Dim body As Body = document.AppendChild(New Body())
        Dim para As Paragraph = body.AppendChild(New Paragraph())
        Dim run As Run = para.AppendChild(New Run())
        ' Write some text to the file.
        run.AppendChild(New Text("Hello, World!"))

        ' Close the document.
        wordprocessingDocument.Close()

        Console.WriteLine("The document has been created." & vbLf & "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

Text Members

DocumentFormat.OpenXml.Wordprocessing Namespace