Click here to Skip to main content
15,899,754 members
Articles / Programming Languages / C#
Article

Gios WORD .NET Library (using RTF specification)

Rate me:
Please Sign up or sign in to vote.
4.85/5 (47 votes)
8 Aug 2005LGPL31 min read 363.3K   4.5K   149   110
A .NET library for generating Word documents (.DOC) using the RTF (Rich Text Format) specification.

This is the output of "Example 1", included in the library.

Introduction

The Gios WORD .NET is the second thing that a good C# developer needs to deploy in his business projects... The first one?!? You probably know it is the Gios PDF .NET library!

So, that's why I decided to develop this library: after the need for generating PDFs, you will need the same for WORDs!

Gios WORD .NET supports:

  • Tables with rows and columns span.
  • Images
  • Header and Footer
  • Line indent - paragraph
  • Custom Control Words (See the Rich Text Format (RTF) Specification for this implementation.)
  • Output to a generic System.IO.Stream object.

Background

Building a library for exporting documents in the Rich Text Format is quite easier than for PDFs. In fact, the Rich Text Format (RTF) Specification, version 1.8 shows how simple it is to generate a word document with all the well known features. I used the version 1.5 (which is fully compatible with WORD 97).

Using the code

The library is very simple to be used! The first thing you need to do is instantiating a new WordDocument:

C#
WordDocument myWordDocument=
    new WordDocument(WordDocumentFormat.Letter_8_5x11);

and with the WordDocument object you can start with the description of the document. It's like an automation. For example, if you want to set the font, you can write:

C#
myWordDocument.SetFont(new Font("Comic Sans MS",
                                60,FontStyle.Bold));
myWordDocument.SetForegroundColor(Color.Red);
myWordDocument.SetFontBackgroundColor(Color.Yellow);

And now we set some alignment:

C#
myWordDocument.SetTextAlign(WordTextAlign.Center);

To write a text... You can imagine:

C#
myWordDocument.Write("Hello World!");

And, in the end, you output the document!

C#
myWordDocument.SaveToFile("HelloWorld.doc");

And this is the result:

Remember, you can also output the WORD Document to a generic stream. These are the lines for a web response:

C#
Response.ClearHeaders();
Response.AppendHeader("Content-disposition",
   "attachment;filename=HelloWorld.doc");
Response.ContentType="application/msword"; 
myPdfDocument.SaveToStream(Response.OutputStream);
Response.End();

To continue with the training, please download the library and follow the examples!

History

  • August 8th, 2005 - First release.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Italy Italy
Freelance software ASPNET / C# Software Developer

I live in Torino, Italy

my homepage is: http://www.paologios.com

Comments and Discussions

 
GeneralVB.Net Pin
rit018-Dec-06 3:47
rit018-Dec-06 3:47 
QuestionRe: VB.Net Pin
rit018-Dec-06 4:04
rit018-Dec-06 4:04 
GeneralRe: VB.Net Pin
Paolo Gios8-Dec-06 4:53
Paolo Gios8-Dec-06 4:53 
GeneralRe: VB.Net Pin
rit018-Dec-06 4:57
rit018-Dec-06 4:57 
QuestionHow to insert wordtable in wordtable? Pin
sharrow7-Dec-06 0:52
sharrow7-Dec-06 0:52 
AnswerRe: How to insert wordtable in wordtable? Pin
Paolo Gios7-Dec-06 1:38
Paolo Gios7-Dec-06 1:38 
GeneralTable direction is right to left in Word by default, how to change? [modified] Pin
Michael Ort26-Aug-06 13:02
Michael Ort26-Aug-06 13:02 
GeneralError while trying to save file Pin
tod1d.net6-Jun-06 4:25
tod1d.net6-Jun-06 4:25 
Hi,

I found a little bit of quirkiness while using your great component.

I created a method that used your component to generate a dynamic word doc. However, whenever I tried to call the method I got the error "Error opening destination file", when calling SaveToFile(). When calling <code>SaveToStream(), I would get the error "Object reference not set to an instance of an object." However, none of the objects I could view. while using the debugger, were null. I finally figured out that if I don't call the SetFont() method right after I create a WordDocument object, I get this error when saving.

Example: THIS CODE DOES WORK

private string CreateWordDoc(int journalID, string journalImagePath, DisputeEntity dispute)
{
	Font normalFont = new Font("Arial", 12);
	Font largeFont = new Font("Arial", 36);

	WordDocument doc = new WordDocument(WordDocumentFormat.Letter_8_5x11);
	doc.SetFont(normalFont);

	WordTable rt1 = doc.NewTable(normalFont, Color.Black, 1, 2, 10);
	rt1.SetColumnsWidth( new int[] {50, 50});
	rt1.Rows[0][0].Write("Hello");
	rt1.Rows[0][1].Write("World");
	rt1.SaveToDocument(10000, 0);

	doc.SaveToFile(@"C:\helloworld.doc");

	return journalFaxPath;
}


Example: THIS CODE DOES NOT WORK

private string CreateWordDoc(int journalID, string journalImagePath, DisputeEntity dispute)
{
	Font normalFont = new Font("Arial", 12);
	Font largeFont = new Font("Arial", 36);

	WordDocument doc = new WordDocument(WordDocumentFormat.Letter_8_5x11);
	// Commenting out the line below causes an error.
	//doc.SetFont(normalFont);

	WordTable rt1 = doc.NewTable(normalFont, Color.Black, 1, 2, 10);
	rt1.SetColumnsWidth( new int[] {50, 50});
	rt1.Rows[0][0].Write("Hello");
	rt1.Rows[0][1].Write("World");
	rt1.SaveToDocument(10000, 0);

	doc.SaveToFile(@"C:\helloworld.doc");

	return journalFaxPath;
}


Is this considered a bug? Hopefully this helps someone else.

Thanks again for the great component.

--
Tod Birdsall, MCSD for .Net
software blog: http://www.tod1d.net
GeneralExcellent! Pin
tod1d.net2-Jun-06 11:18
tod1d.net2-Jun-06 11:18 
GeneralComplimenti! Pin
AlePu24-May-06 23:22
AlePu24-May-06 23:22 
QuestionCan i use code Pin
jiturinku17-May-06 19:42
jiturinku17-May-06 19:42 
NewsFix for backcolors in tablecells Pin
j-on12-Apr-06 10:30
j-on12-Apr-06 10:30 
GeneralRe: Fix for backcolors in tablecells Pin
frankosaurus25-Jun-07 8:28
frankosaurus25-Jun-07 8:28 
GeneralMultiple Font-Styles Pin
danh3311-Apr-06 12:17
danh3311-Apr-06 12:17 
AnswerRe: Multiple Font-Styles Pin
nep4uk22-Nov-06 23:16
nep4uk22-Nov-06 23:16 
QuestionHow can it support Chinese? Pin
btshida4-Apr-06 2:30
btshida4-Apr-06 2:30 
GeneralDPI For Images? :confused: Pin
badoubade13-Feb-06 23:18
badoubade13-Feb-06 23:18 
GeneralPage Numbers Pin
DadKind19-Jan-06 12:54
DadKind19-Jan-06 12:54 
GeneralRe: Page Numbers Pin
Paolo Gios19-Jan-06 12:58
Paolo Gios19-Jan-06 12:58 
AnswerRe: Page Numbers Pin
j-on12-Apr-06 8:56
j-on12-Apr-06 8:56 
Questiongreat job - do u know how to make excel files ? Pin
haim magen20-Dec-05 17:44
haim magen20-Dec-05 17:44 
AnswerRe: great job - do u know how to make excel files ? Pin
Paolo Gios20-Dec-05 21:23
Paolo Gios20-Dec-05 21:23 
Generalexcel request Pin
haim magen20-Dec-05 22:15
haim magen20-Dec-05 22:15 
AnswerRe: excel request Pin
Carsten Zeumer21-Dec-05 8:46
Carsten Zeumer21-Dec-05 8:46 
GeneralRe: excel request Pin
haim magen21-Dec-05 11:06
haim magen21-Dec-05 11:06 

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.