Jaa


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

Comments

  • Anonymous
    June 29, 2006
    Doug Mahugh had a post a couple days ago where he provides some basic code that will generate a WordprocessingML...

  • Anonymous
    July 15, 2006
    This post covers the code for a CreateXlsx program that creates a simple Open XML spreadsheet from scratch...

  • Anonymous
    July 20, 2006
    Creating Open XML documents from a template can help simplify your work and allow for changes to the...

  • Anonymous
    August 29, 2006
    One of the nice things about the WF activity model is that you can pretty rapidly take existing code...

  • Anonymous
    October 03, 2006
    A while back, the OpenXmlDeveloper.org website offered an example of how to create a WordProcessingML

  • Anonymous
    December 04, 2006
    Here at the Open XML workshop in Paris this week, Jerome Berthaud pointed out to me that my CreateXlsx

  • Anonymous
    December 06, 2006
    Ich hatte versprochen, die Ressourcen für meine beiden Vorträge auf der Office, Vista und Exchange Launch

  • Anonymous
    May 30, 2008
    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

  • Anonymous
    June 06, 2008
    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

  • Anonymous
    June 02, 2009
    PingBack from http://outdoorceilingfansite.info/story.php?id=56519

  • Anonymous
    June 08, 2009
    PingBack from http://quickdietsite.info/story.php?id=9418