Building a plain text document is something we can do via the wonderful .txt file. However, if we’re taking the time to use the Open Office XML format, specifically the WordprocessinML format, we want to be able to format our generated document. In this article, we’ll examine WordprocessingML closer so that we can do just that.
The key to text formatting is the run. As previously discussed, a run is a particular block of text with the particular purpose of applying formatting. If you are familiar with HTML, consider the run akin to the <span> tag.
<w:document> <w:body> <w:p> <w:r> <w:t>Hello, world!</w:t> </w:r> </w:p> </w:body> </w:document>
The above is an unformatted run. To format a run, we need to supply properties for the particular run via the rPr element.
. . . <w:p> <w:rPr> <w:b/> </w:rPr> <w:t>I am bold!</w:t> </w:p> . . .
By simply specifying a run properties entity and putting in other run property elements, we are able to format our text. However, we can also order a particular run to not use a particular format:
. . .
<w:rPr>
<w:b w:val="false" />
</w:rPr>
. . .
This would be useful for ignoring higher level formatting from being applied to a particular run of text. Other than explicitly stating whether or not to apply a format option, we also have formatting elements that need attribute values to work. For example, the color entity:
. . .
<w:rPr>
<w:color w:val="FF0000" />
</w:rPr>
. . .
This example will make the run of text red, and follows the HTML color attribute style. You can also define “auto” as a value, which will automatically apply the appropriate color based on other parts defined in the document, i.e. a background part may specify an appropriate default font color.
Furthermore, there are theme level attributes available, i.e. the themeColor attribute. Often, you may have a document that specifies a Default, Heading, Sub-Heading, etc. formats, and this attribute can tap into those defined formats. Since these defined formats are stored within the document, we can generate these along with our document. Doing so will make it easier for anyone potentially modifying our generated document to maintain the various heading and text formatting across any modifications or additions they make. For our next article, we’ll examine how to add these predefined formats to our generated document.
For a more in depth list of text formatting options and entities, check out: http://openxmldeveloper.org