Is there a way to generate word documents dynamically without having word on the machine

10,716

Solution 1

I've worked at a company in the past that really wanted generated word documents, in the end they were perfectly satisfied with RTF docs that had a ".doc" extension. Word has no problem recognizing and opening them.

The RTF docs were generated with iText.net (free .net library), the API is pretty easy to use, performs extremely well, you don't need word on the machine, also, you could extend to generating PDF, HTML, and Text docs in the future with very little effort. After four years the solution I created is still in place, so that's a little testimony in iText.net's favor.

It looks like the official iText page suggests that iText Sharp is the best .Net choice right now, so that's another option

Solution 2

You'd be better off generating an rtf file, which word will know how to open.

Solution 3

If want to generate Office 2007 documents check the Open XML File Formats, they're simple zipped XML files, check this links:

Edit: Check this project, can serve you as a good starting point:

Seems very simple and customizable, look this code snippet:

Paragraph p = new Paragraph();
p.Runs.Add(new Run("Text can have multiple format styles, they can be "));
p.Runs.Add(new Run("bold and italic", 
        TextFormats.Format.Bold | TextFormats.Format.Italic));
doc.Paragraphs.Add(p);

Solution 4

Word will quite happily open a HTML with a .doc extension. If you include an internal style sheet, you can have it fully formatted. There was previous post on this subject:

Export to Word Document in C#

Solution 5

Creating the old .DOC files (pre-Word 2007) is nigh-impossible without Word itself. The format is just too complex. Microsoft has released the format description, but it's enough to reduce a grown programmer to tears. There is a reason for that too (historical), but that doesn't make things better.

The new .DOCX would be easier, although quite a bit of hassle still. However depending on which Word versions you are targeting, there are some other options too.

For one, there is the classic .RTF. The format is pretty complex still, yet well documented and has strong support across many applications and platforms. And you might use some string-replacing into template files to make things easier (it's non-binary).

Then there are the "old" Word XML files. I think they worked starting with Word XP. Kinda the predecessors of .DOCX. I've used them, not bad. And the documentation is pretty OK.

Finally, the easy way that I would choose, is to make a simple HTML. Word can load HTML files just fine starting with version 2000. In the simplest way just change the extension of a HTML file to .DOC and you have it. You can also add a few word-specific tags and comments to make it look even better in Word. Use the Word's Save As...HTML option to see what they are.

Share:
10,716
minty
Author by

minty

Updated on June 03, 2022

Comments

  • minty
    minty almost 2 years

    I am planning on generating a Word document on the webserver dynamically. Is there good way of doing this in c#? I know I could script Word to do this but I would prefer another option.