This next example generates a slightly more complex Word document with two paragraphs, each containing text in a different font. Here's the process:
- Create the Word processing document package and add the main document part to it as in the previous example.
- Create the first paragraph containing the text "Hello World," but don't add it to the body yet:
Text textFirstLine1 = new Text("Hello World");
Run run1 = new Run(textFirstLine1);
Paragraph para1 = new Paragraph(run1);
- Create a second paragraph containing the text "Hello Open XML Community:"
Text textFirstLine2 = new Text("Hello Open XML Community");
Run run2 = new Run(textFirstLine2);
Paragraph para2 = new Paragraph(run2);
- You apply font formatting to a Run. Runs can contain a RunProperties object, which control the formatting applied to the text contained in that Run. Runs that don't have RunProperties use Word's default font. You can create RunProperties objects independently and then apply them to any Run. The following code creates a RunProperties object that causes the Run to display its text using the Arial Black font:
RunProperties runProp = new RunProperties();
RunFonts runFont = new RunFonts();
runFont.Ascii = "Arial Black";
- Apply the RunProperties object you just created to the Run run2 you created earlier:
- Create a new Body instance and append both paragraphs:
Body body = new Body();
- Add the body to the document. Save and close the package:
When you execute this program, it will create a document containing two paragraphs with different fonts (see Figure 2).
Search and Replace Text in a Word Document
Creating new documents addresses only one aspect of working with OOXML documents. This next example opens an existing document, searches for some text in that document, and replaces it with other text. This is typical of scenarios where you want to generate a large number of documents based on a small template: You'd read the template, replace some portion of the template with custom content, and then save the altered document. (You'll see a large-template scenario in the next section).
First, open the document using WordprocessingDocument class and get the MainDocumentPart. The document in this example is named SearchAndReplace.docx:
WordprocessingDocument doc =
MainDocumentPart mainPart =
Read the entire document contents using the GetStream method:
using (StreamReader sr = new StreamReader(
docText = sr.ReadToEnd();
At the end of this process, the docText variable contains all the XML for the document text. Next, replace contents in the docText variable as needed. For this example the template contains the text "The current version of [sdk] is [VersionNumber]."
The task is to replace the [sdk] and [VersionNumber] placeholders with actual values. You can use standard .NET string-manipulation code to make the replacement, so I won't show it here. After replacing the text, write the complete text back to the Main Document part using the following code:
using (StreamWriter sw = new