Click here to Skip to main content
Licence LGPL3
First Posted 8 Aug 2005
Views 165,764
Bookmarked 134 times

Gios WORD .NET Library (using RTF specification)

By | 8 Aug 2005 | Article
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:

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:

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

And now we set some alignment:

myWordDocument.SetTextAlign(WordTextAlign.Center);

To write a text... You can imagine:

myWordDocument.Write("Hello World!");

And, in the end, you output the document!

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:

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)

About the Author

Paolo Gios

Web Developer

Italy Italy

Member

Freelance software ASPNET / C# Software Developer
 
I live in Torino, Italy
 
my homepage is: http://www.paologios.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIssues on clean Windows 7 (32 bit) system? Pinmemberkembo14:53 25 Aug '11  
BugAutoFitBehavior (wdAutoFitContent) Pinmembermail-221:47 16 Aug '11  
GeneralTurkish Encoding Problem is solved simply. Pinmemberemarti11:18 4 Jun '11  
QuestionHow do I set the First Page Header Or Footer Different with other page on word document by using this library? Pinmemberlin ma596:05 23 Jul '09  
QuestionOpen an existing document? PinmemberTommy W0:17 13 May '09  
GeneralDocument won't show correctly in WordPad (or RichtextBox) PinmemberMember 45269124:54 27 Oct '08  
GeneralRe: Document won't show correctly in WordPad (or RichtextBox) PinmemberNever Winter17:18 5 Jan '09  
GeneralRender to image Pinmemberjosepaulino18:36 19 Aug '08  
Is it possible to render the rtf to an image using this library?
 
I loaded a rtf file in a richtextbox control and tried the following:
 
            Gios.Word.WordDocument d = new WordDocument(WordDocumentFormat.Letter_8_5x11);
 

            d.WriteRtf(richTextBox1.Rtf);
 
            MemoryStream ms = new MemoryStream();
 
            d.RenderToStream(ms);
 
            Image img = Image.FromStream(ms);
 
            img.Save("c:\test.bmp");
 
It threw an exception at Image.FromStream(ms) saying "Parameter is invalid". What is the output stream do? Do I need to use GDI+ to render it? If so how? Any insight would be good, I've been googling like crazy and not finding any source code to render an rtf.
 
Thanks in advance,
Jose
GeneralChange table size and Aligment PinmemberTai Fei Pao8:12 18 Aug '08  
GeneralSolution for Table of contents Pinmembervavavivi4:07 3 Jul '08  
GeneralPage Current Number Pinmembervavavivi1:56 3 Jul '08  
GeneralRe: Page Current Number PinmemberPaolo Gios2:01 3 Jul '08  
GeneralRe: Page Current Number Pinmembervavavivi2:31 3 Jul '08  
GeneralRe: Page Current Number PinmemberTatjana Pede10:57 16 Jul '09  
QuestionHow to add checkbox? Pinmembergkee16:42 28 Apr '08  
Generaladding landscape page and portrait page in same document. PinmemberNajeed17:24 12 Dec '07  
GeneralRTF to PDF Converter for .NET Pinmemberflchivu15:19 3 Sep '07  
GeneralUnicode characters bugfix Pinmemberdejandular4:28 12 Aug '07  
GeneralRe: Unicode characters bugfix PinmemberHamid Reza Mohammadi19:32 23 Sep '07  
GeneralRe: Unicode characters bugfix PinmemberSimon Pham17:17 1 Jan '08  
GeneralRe: Unicode characters bugfix Pinmemberinormis20:24 16 Feb '10  
GeneralRe: Unicode characters bugfix PinmemberMasoud Aghdasi Fam0:03 23 May '09  
GeneralRe: Unicode characters bugfix PinmemberMember 819932316:08 30 Aug '11  
QuestionMake Table on New Page PinmemberCatch Anand22:56 31 Jul '07  
AnswerRe: Make Table on New Page Pinmembergraciella7:34 3 Aug '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 8 Aug 2005
Article Copyright 2005 by Paolo Gios
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid