Click here to Skip to main content
Click here to Skip to main content
Go to top

Simple DocX Editor in Native C#

, 28 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Basic DocX editing in C# without using OpenXML or Office Interop

Introduction

I recently had to write a module to create an Invoice in Word (.docx) format. Though I found a number of solutions which do this, none were as simple as I wanted them to be. All I wanted was kind of search-and-replace that I could do from C# code. So this library effectively does a simple text based search-and-replace on a docx file without any external library (OpenXML or Office Interop). It doesn't require Office to be installed on the target machine as well.

Background

The .docx format is basically an archive containing several other files. The main content residing in the XML file word/document.xml. Details on the format can be found here.

So, what this library effectively does is:

  1. Extract the docX to a temporary location.
  2. Do a search-and-replace on the XML file containing main content (word/document.xml)
  3. Repackage the files to re-create docX.
  4. Copy/Rename the new file to the desired location

To archive/unarchive the docx file without any third party library, I've used the code from my earlier article.

Using the Code

Using the DocX Editor is extremely simple. The main class that exposes all the functionality is the DocXEditor class.

Before the DocXEditor class can edit the file, we need to provide 3-pieces of information to the class:

  1. The source file name
  2. A list of text to replace in the source file
  3. The destination file name

Here's how we specify each of the three:

1. Specify the source file

This is as simple as passing the file name to the constructor.

DocXEditor dox = new DocXEditor("Template.docx"); 

This will set the source (or template) file of our choosing.

2. List of text to replace in the template file

This is where we specify the search-replace stuff. The class will finally go through this list and replace the text one-by-one as specified. Here's how it's done.

ReplacementList replaceList = new ReplacementList();
replaceList.Add(new ReplaceItem("[PAT_NAME]", "John Doe"));
replaceList.Add(new ReplaceItem("[PAT_AGE]", "24"));

dox.ReplaceList = replaceList;     

Here, we are creating a new ReplacementList and then adding find-replace pair of strings. So, effectively, we are asking the DocXEditor to find the string "[PAT_NAME]" in the docx file and replace it with the string "John Doe". Similarly, in the third line, we want the string "[PAT_AGE]" to be replaced with the string "24".

In the last line above, we assign the newly created replacement list to the DocXEditor object created in step 1.

Note: The ReplacementList and ReplaceItem classes are just miniature helper classes to make our task of specifying search-replace list easier.

3. Specify the destination file

This is the final step where we specify the destination file and ask DocXEditor to actually do the work.

dox.ReplaceContent("Test_Destination.docx", true);  

Here, we set the destination file name. The second argument is optional and specifies if the destination file should be overwritten if it already exists.

That's it!

Points of Interest

  • The code snippets shown above have been slightly modified from the sample application attached. This has been done for the sake of making the snippet simpler and easier to understand.
  • Though this is not the most flexible and/or effective way to modify a docx file, in my opinion this is an extremely simple approach and might be useful in many scenarios. Additionally, this also has an advantage of not needing any external libraries.
  • I've written this code in VS 2012 and the target framework is 3.5. However, since this code doesn't use anything fancy, it should work with older versions of the .NET Framework just fine.
  • This library might not work in cases where the string to be searched is similar to the XML tags in the XML document.

History

  • Original post: 28 September, 2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kumar Kisalaya
Software Developer (Senior)
Nepal Nepal
No Biography provided

Comments and Discussions

 
QuestionIssues when running on server PinmemberMember 1092115929-Jul-14 4:19 
AnswerRe: Issues when running on server PinmemberKumar Kisalaya12-Sep-14 1:09 
QuestionVery good PinmemberMember 871853329-Apr-14 1:27 
QuestionNice! PinmemberMember 76951444-Dec-13 2:39 
QuestionSimple but Issues Pinmembercjb11029-Sep-13 21:26 
AnswerRe: Simple but Issues PinmemberKumar Kisalaya29-Sep-13 23:53 
GeneralMy vote of 5 PinmemberMJ.Idrees29-Sep-13 7:58 
AnswerRe: My vote of 5 PinmemberKumar Kisalaya29-Sep-13 19:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 28 Sep 2013
Article Copyright 2013 by Kumar Kisalaya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid