CreateDOCX Sample Program

This post covers a very simple program for creating an Office Open XML word-processing document. The source code for this program is included in the attachment, or you can download it here.

The syntax for using the CreateDOCX program is shown to the right. It's a command-line program that takes two arguments: a filename to be created, and some text to put in the file. That's all there is to it -- the program then creates the output file using the .NET packaging API. The resulting document can be opened with Microsoft Office, but Office is not required in order to create the document.

Let's look at what's going on under the hood. The first thing to note is the namespace used for the document markup:

string WordprocessingML = "https://schemas.openxmlformats.org/wordprocessingml/2006/3/main";

This is the namespace for the WordprocessingML markup that is used in the main body part ("start part") of the document. Next, we create an XML document with the structure of a WordprocessingML document (document, body, paragraph, run, etc.), and the text (the BodyText variable) embedded inside it:

// create the start part, set up the nested structure ...XmlDocument xmlStartPart = new XmlDocument();XmlElement tagDocument = xmlStartPart.CreateElement( "w:document" , WordprocessingML );xmlStartPart.AppendChild( tagDocument );XmlElement tagBody = xmlStartPart.CreateElement( "w:body" , WordprocessingML );tagDocument.AppendChild( tagBody );XmlElement tagParagraph = xmlStartPart.CreateElement( "w:p" , WordprocessingML );tagBody.AppendChild( tagParagraph );XmlElement tagRun = xmlStartPart.CreateElement( "w:r" , WordprocessingML );tagParagraph.AppendChild( tagRun );XmlElement tagText = xmlStartPart.CreateElement( "w:t" , WordprocessingML );tagRun.AppendChild( tagText );// insert text into the start part, as a "Text" node ...XmlNode nodeText = xmlStartPart.CreateNode(XmlNodeType.Text, "w:t", WordprocessingML);nodeText.Value = BodyText;tagText.AppendChild(nodeText);

Now that we've created the XML "start part" for the document, we just need to create a package (with the packaging API), add the start part to it, and create a relationship. (All parts must have a relationship.)

// create a new package (Open XML document) ...Package pkgOutputDoc = null;pkgOutputDoc = Package.Open( fileName , FileMode.Create , FileAccess.ReadWrite );// save the main document part (document.xml) ...Uri uri = new Uri( "/word/document.xml" , UriKind.Relative );PackagePart partDocumentXML = pkgOutputDoc.CreatePart( uri,"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" );StreamWriter streamStartPart = new StreamWriter( partDocumentXML.GetStream( FileMode.Create , FileAccess.Write ) );xmlStartPart.Save( streamStartPart );streamStartPart.Close();pkgOutputDoc.Flush();// create the relationship part, close the document ...pkgOutputDoc.CreateRelationship(uri, TargetMode.Internal,"https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId1");pkgOutputDoc.Flush();pkgOutputDoc.Close();

That's all there is to it -- this code will generate a DOCX that loads in Word 2007 or any other valid Open XML consumer. You can copy the code from the SaveDOCX method of the attached solution, and use that as a starting point for a document-creation or document-assembly solution. Note that there is no need for the Office clients in this scenario: your code can create the DOCX without any of the complexity and scalability issues of automating Word, through use of the .NET packaging API.

The next post in this series will cover CreateXLSX, the SpreadsheetML version of this sample.

CreateDOCX.zip