Click here to Skip to main content
12,246,601 members (36,787 online)
Click here to Skip to main content
Add your own
alternative version

Stats

50.6K views
1.4K downloads
41 bookmarked
Posted

HTML to WordML

, 29 Jan 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Class supporting dynamic creation of a .docx based on a template. Supports HTML content.

Introduction

The code provided creates a new docx document based on a previously created template.

The templates have bookmarks in the document body, header and footer. Their content is set dynamically.

It also supports the insertion of HTML formatted content in the document body.

Background

This solution is based on this article by Praveen Bonakurthi. I made his code a bit more user friendly and added the HTML functionality.

Getting Started

First, you need to create a docx template. Just open Word and create the file as you please.
In the areas where you want to insert plain text, just select the area and go to the ribbon toolbar, insert>bookmark, give it a unique name. You can apply Word formatting to the text that will be inserted later.

Setting HTML Content Locations

For HTML content, it's a bit more tricky. So just finish up adding all bookmarks for the plain text before you do this step.

Change the docx extension to zip. Open it and open Word/document.xml with any editor you like (I just use Notepad).

Place <w:altChunk r:id="IdForTheHtmlContent" /> nodes under the <w:body> node.

Replace "IdForTheHtmlContent" with whatever unique id you want.

Put them where you want the HTML to appear.

Save the file back when you're done. Change the extension back to .docx.

Now you got your template ready to be used!

Using the Code

There are just 2 methods to be used:

public static string CreateWordDocument(string templatePath, 
	Dictionary<string, string> Bookmarks, Dictionary<string, string> 
	BookmarksHeaderFooter, Dictionary<string, string> HtmlChunks) 

Defined in the class MyWordMLHTMLClasses.WordMl, this method is responsible for the creation of the document.

The method...

private void ReturnStream(string filepath, bool DeleteFileAfter) 

... returns the generated file in the HTTP Response stream and may or not delete it later.

This is how you use it:

// get values ready for insert in the docx file
Dictionary<string, string> bookmarks = new Dictionary<string, string>();
Dictionary<string, string> bookmarksHeaderFooter = new Dictionary<string, string>();
Dictionary<string, string> htmlChunks = new Dictionary<string, string>();
// HTML chuncks in the document area
htmlChunks.Add("S_Teste", "<b>Text HTML</b>" + 
"<br />simple html text after br<br /> " + 
"<table style=\"width:250px;\"><tr>" + 
"<td>html table cell 1</td>" + 
"<td>html table cell 2</td></tr><tr>" + 
"<td>html table cell 3</td>" + 
"<td>html table cell 4</td>" + 
"</tr></table>");
// bookmarks in the document area
bookmarks.Add("S_ADDRESS_V1", "Adress line 1");
bookmarks.Add("S_ADDRESS_V2", "Adress line 2");
// bookmarks in Header/Footer
bookmarksHeaderFooter.Add("FOOTER_1_1", "Inserted Footer Text");
bookmarksHeaderFooter.Add("FOOTER_1_2", "123");
bookmarksHeaderFooter.Add("HEADER5", "Inserted Header Text");

// generate docx file from template docx
string fileName = WordML.CreateWordDocument(
    Server.MapPath(@"~\_Documents\MyTemplate.docx"), // template docx path
    bookmarks, // bookmarks and Values
    bookmarksHeaderFooter, // header/footer bookmarks and Values
    htmlChunks // HTML chunks to be inserted 
    );

// returns file in stream, deletes it afterwards 
//(if option is selected as in this case)
ReturnStream(fileName, true);

Points of Interest

I did some extensive searching to find any HTML to Word ML snippet. Got nothing close to a finished snippet, so this may be useful.

If too many people find it hard to understand, I'll edit and post a better explanation on how to use the stuff.

Download the code, it will probably be simple to understand after watching it in action.

How To Use It On Your Projects?

  1. Download the code.
  2. Copy the WordML.cs to your project folder and add it to your project in Visual Studio.
  3. Add a reference to windowsbase.dll.
  4. Use it as shown above.

History

  • Download bug solved with a little help from stikoian

License

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

Share

About the Author

Paulo Vaz
Software Developer (Senior) WEBA, Lda
Portugal Portugal
Computer Engineer from ISEL, Lisbon

Founder and partner of WEBA, Lda.

Software Developer (and sometimes Network Manager)

You may also be interested in...

Comments and Discussions

 
QuestionLandscape Pin
wq723-Nov-12 10:21
memberwq723-Nov-12 10:21 
QuestionIn c# 4.0 Pin
vinnygambonni29-Jun-12 6:24
membervinnygambonni29-Jun-12 6:24 
GeneralWithout saving data on the server Pin
athiratha4-Jun-11 4:40
memberathiratha4-Jun-11 4:40 
GeneralRe: Without saving data on the server Pin
Gloold1-Oct-15 3:53
memberGloold1-Oct-15 3:53 
QuestionWhat about adding images ? Pin
Member 20977815-Mar-11 14:21
memberMember 20977815-Mar-11 14:21 
GeneralPerfect, but... Pin
Quietcougar12-Jan-11 7:03
memberQuietcougar12-Jan-11 7:03 
QuestionSpecial characters in html Pin
inferno66625-Aug-10 4:04
memberinferno66625-Aug-10 4:04 
AnswerRe: Special characters in html Pin
inferno66625-Aug-10 4:32
memberinferno66625-Aug-10 4:32 
AnswerRe: Special characters in html Pin
Paulo Vaz25-Aug-10 5:04
memberPaulo Vaz25-Aug-10 5:04 
GeneralRe: Special characters in html Pin
Paulo Vaz25-Aug-10 5:06
memberPaulo Vaz25-Aug-10 5:06 
QuestionError when creating my own template Pin
poisonaby12-Jul-10 2:24
memberpoisonaby12-Jul-10 2:24 
GeneralFixed - HTML now also works on Bookmarks Pin
wicusv28-Jun-10 0:16
memberwicusv28-Jun-10 0:16 
GeneralDOCX created in _Documents not deleted by f.Delete(); Pin
nickcr24-Jun-10 16:00
membernickcr24-Jun-10 16:00 
QuestionWord 2003 Pin
Bluebamboo15-Jun-10 12:22
memberBluebamboo15-Jun-10 12:22 
AnswerRe: Word 2003 Pin
Paulo Vaz17-Jun-10 4:39
memberPaulo Vaz17-Jun-10 4:39 
GeneralRe: Word 2003 Pin
Bluebamboo17-Jun-10 5:05
memberBluebamboo17-Jun-10 5:05 
QuestionHow to add a pagebreak? Pin
nickcr11-May-10 17:34
membernickcr11-May-10 17:34 
AnswerRe: How to add a pagebreak? Pin
nickcr11-May-10 18:34
membernickcr11-May-10 18:34 
GeneralMuito bom! Pin
Industria Virtual3-Feb-09 8:08
memberIndustria Virtual3-Feb-09 8:08 
GeneralRe: Muito bom! Pin
Paulo Vaz3-Feb-09 14:35
memberPaulo Vaz3-Feb-09 14:35 
AnswerGreat Article and Bug Fixed [modified] Pin
stikoian29-Jan-09 16:30
memberstikoian29-Jan-09 16:30 
GeneralRe: Great Article and Bug Fixed Pin
Paulo Vaz29-Jan-09 17:47
memberPaulo Vaz29-Jan-09 17:47 
QuestionHow do I change my docx to enable html injection without breaking my docx when rezipping? Pin
Jakob Flygare16-Jun-09 4:58
memberJakob Flygare16-Jun-09 4:58 
AnswerRe: How do I change my docx to enable html injection without breaking my docx when rezipping? Pin
Jimburg25-Aug-09 17:53
memberJimburg25-Aug-09 17:53 
GeneralRe: How do I change my docx to enable html injection without breaking my docx when rezipping? Pin
Paulo Vaz26-Aug-09 4:20
memberPaulo Vaz26-Aug-09 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 29 Jan 2009
Article Copyright 2009 by Paulo Vaz
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid