Using OpenXML Library to update word documents


The below post describes updating the word document files using the OpenXML library. The typical application of the below article would be replacing tags in a template document to create a new document. We will be using the content control to replace the tags. Hence this method will work only with files with .docx (Word 2007) extension, as files with .doc (Word 2003) extension do not support content controls.

You will need developer tab of the word document to add the content control to the document. To enable Developer tab in Word 2010, in File menu, click Options button and then Customize ribbon. Select Developer option in Main tabs.

Now create the word document file with content control for tags.

I have created a word file, ResumeFormat.docx. Now, click on the content control and click on properties button, a pop-up window opens up. Add the Title and Tag in the properties window.

As a rule, I have added the tag for all the controls in lower case. You can also check the locking check box to ensure that the controls are neither updated nor deleted by the user. Also formatting can be achieved by checking the style check box.

Now we need the Word 2007 Content Control Toolkit to bind the XML data to the content controls. Download the tool from CodePlex (http://dbe.codeplex.com/)

When we open the newly created word document in this tool, we see the content controls of the document. However, the XPath field is missing.

To populate the XPath field we will have to create a new Custom XML Part and map it with the content controls.

Click on “Create a new Custom XML Part” link provided at the bottom of the Namespace panel. Move to the “Edit View” tab of the panel. We need our XML node in the following format.

<nodename>[tagname]</nodename>

Where tagname is the text we entered in the content control properties window, in tags text box.

Hence our XML file would look like this,
<root>
<Name>[name]</Name>
<<FullName>[fullname]</FullName>
<Education>[education]</Education>
<Experience>[experience]</Experience>
<OS>[os]</OS>
<DBMS>[dbms]</DBMS>
<Other>[other]</Other>
<Server>[server]</Server>
<Domain>[domain]</Domain>
<Projects>[projects]</Projects>
</root>

In Bind tab you will see the XML structure and value of XPath at bottom of the panel.

Now drag on the XML node and drop it on the appropriate content control. This populates the content control XPath value. We are done with the configuration par, so click on save button to save the file.

In Visual Studio, add the references of OpenXML library and include the following namespaces.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;

Call the following function to replace the Custom XML with the values you want.
public void GenerateDocument()
{
string templatePath = @"~\ResumeFormat.docx ";
string documentPath = @"~\NewResume.docx ";

File.Copy(templatePath, documentPath);
MessageBox.Show(“Created copy of template …”);
using (WordprocessingDocument doc = WordprocessingDocument.Open(documentPath, true))
{
StringBuilder newXml = new StringBuilder(“<root>”);
newXml.AppendFormat(“<Name>{0}</Name>”, “Prashant Gaikwad”);
newXml.AppendFormat(“<FullName>{0}</FullName>”, “Prashant Vilas Gaikwad”);
newXml.AppendFormat(“<Education>{0}</Education>”, “Computer Science”);
newXml.AppendFormat(“<Experience>{0}</Experience>”, “3”);
newXml.AppendFormat(“<OS>{0}</OS>”, “Windows, Mac, Linux”);
newXml.AppendFormat(“<DBMS>{0}</DBMS>”, “SQL Server”);
newXml.AppendFormat(“<Other>{0}</Other>”, “None”);
newXml.AppendFormat(“<Server>{0}</Server>”, “IIS”);
newXml.AppendFormat(“<Domain>{0}</Domain>”, “BPM”);
newXml.AppendFormat(“<Projects>{0}</Projects>”, “Workflows Simplified”);
newXml.AppendFormat(“</root>”);

MainDocumentPart main = doc.MainDocumentPart;
main.DeleteParts<CustomXmlPart>(main.CustomXmlParts);

CustomXmlPart customXml = main.AddCustomXmlPart(CustomXmlPartType.CustomXml);
using (StreamWriter ts = new StreamWriter(customXml.GetStream()))
{
ts.Write(newXml);
}
}
}

You can download the source files from the below link.

OpenXML Source Files

Advertisements

About Rupesh

.NET and SharePoint 2010 developer
This entry was posted in .NET and tagged , , . Bookmark the permalink.

One Response to Using OpenXML Library to update word documents

  1. Anonymous says:

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s