Click here to Skip to main content
13,793,675 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


104 bookmarked
Posted 30 Jul 2010
Licenced CPOL

RTF Document Constructor Library

, 16 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Create Rich Text Format documents programatically.


Some time ago, I needed to construct Rich Text Format reports, and I decided to write my own library from scratch as I couldn't find any suitable solution on the web.

The main idea was to create a library that could be easily expanded, so that developers could add any feature from the RTF Specification that is not implemented by default. Look at the second part of this article for details.

Using the Code

The RtfDocument class has a constructor that takes a RtfCodepage enum value as parameter. I tested the library with Windows-1251 Cyrillic encoding, and I'm quite sure it works just as well with others. Unicode is supported too.

RtfDocument rtf = new RtfDocument();

We go on with adding fonts and colors to the specified tables. Later, we will use indexes to refer to them (that's certainly the simplest but not the best implementation). RtfDocument.DefaultFont defines the index of the font used for paragraphs with no FontIndex set explicitly.

rtf.FontTable.Add(new RtfFont("Calibri"));
rtf.FontTable.Add(new RtfFont("Constantia"));
rtf.ColorTable.AddRange(new RtfColor[] {
    new RtfColor(Color.Red),
    new RtfColor(0, 0, 255)

The contents of the document are paragraphs, formatted paragraphs, table rows, and tables. Let's create a header paragraph with centered 16pt text.

RtfFormattedParagraph header = 
  new RtfFormattedParagraph(new RtfParagraphFormatting(16, RtfTextAlign.Center));

Add some text, formatted text, and an empty paragraph to the header:

header.AppendText(new RtfFormattedText(" Bold", RtfCharacterFormatting.Bold));

Add another paragraph with a different formatting. We set FontIndex to 1 and IndentLeft to 6.05cm. Most of the indents and widths are set in twips. TwipConverter converts millimeters, centimeters, and points to twips, and vice versa.

RtfFormattedParagraph p = 
  new RtfFormattedParagraph(new RtfParagraphFormatting(12, RtfTextAlign.Left));
p.Formatting.FontIndex = 1;
p.Formatting.IndentLeft = TwipConverter.ToTwip(6.05F, MetricUnit.Centimeter);
p.AppendText(new RtfFormattedText("Superscript", RtfCharacterFormatting.Superscript));

Here is an example demonstrating inline font size change. The font index -1 to be ignored.

p.AppendParagraph(new RtfFormattedText("Inline", -1, 8));
p.AppendText(new RtfFormattedText(" font size ", -1, 14));
p.AppendText(new RtfFormattedText("change", -1, 8));

Pictures are supported in different output formats. JPEG and PNG cannot be read by WordPad, so it's better to use WMF for compatibility. The conversion to WMF is done with P/Invoke calls, and the credits for this part go to David Bennett.

RtfImage picture = new RtfImage(Properties.Resources.lemon, RtfImageFormat.Wmf);
picture.ScaleX = 50;
picture.ScaleY = 50;


A hyperlink with common formatting:

RtfFormattedText linkText = 
  new RtfFormattedText("View article", RtfCharacterFormatting.Underline, 2);
p.AppendParagraph(new RtfHyperlink("RtfConstructor.aspx", linkText));

A centered table with 2 columns and 3 rows:

RtfTable t1 = new RtfTable(RtfTableAlign.Center, 2, 3);

The cells can be merged both horizontally and vertically:

t1.MergeCellsVertically(1, 0, 2);

Formatting to use within cells:

RtfParagraphFormatting LeftAligned12 = new RtfParagraphFormatting(12, RtfTextAlign.Left);
RtfParagraphFormatting Centered10 = new RtfParagraphFormatting(10, RtfTextAlign.Center);

The table cell class derives from formatted paragraph, and has some additional properties.

t1[0, 0].Definition.Style = 
  new RtfTableCellStyle(RtfBorderSetting.None, LeftAligned12, 
t1[0, 0].AppendText("Bottom");

t1[1, 0].Definition.Style = 
  new RtfTableCellStyle(RtfBorderSetting.Left, Centered10, 
t1[1, 1].Definition.Style = t1[1, 0].Definition.Style;
t1[1, 0].AppendText("Vertical");

We set TextColorIndex of the cell to 1, and add RtfFormattedText with different colors.

t1[0, 1].Formatting = new RtfParagraphFormatting(10, RtfTextAlign.Center);
t1[0, 1].Formatting.TextColorIndex = 1;
t1[0, 1].AppendText(new RtfFormattedText("Black", 0));
t1[0, 1].AppendText(" Red ");
t1[0, 1].AppendText(new RtfFormattedText("Blue", 2));

This part shows an example of bitwise operations on RtfCharacterFormatting:

t1[0, 2].AppendText("Normal");
t1[1, 2].AppendText(new RtfFormattedText("Italic", 
         RtfCharacterFormatting.Caps | RtfCharacterFormatting.Italic));
t1[1, 2].AppendParagraph("+");
t1[1, 2].AppendParagraph(new RtfFormattedText("Caps", 
         RtfCharacterFormatting.Caps | RtfCharacterFormatting.Italic));

Adding the contents to the document:

rtf.Contents.AddRange(new IRtfDocumentPart[] {

When the document is complete, we use RtfWriter to convert it to RTF code:

RtfWriter rtfWriter = new RtfWriter();
TextWriter writer = new StreamWriter("test.rtf");
rtfWriter.Write(writer, rtf);

And voila, the resulting file as seen in Microsoft Word:


And that's the RTF code:

{\fonttbl {\f0\fnil\fcharset1\fprq0 Calibri;}{\f1\fnil\fcharset1\fprq0 Constantia;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue255;}
\pard\plain\qc\fi0\li0\ri0\sl0\sb0\sa240\fs32 Calibri 
    {\b Bold}\par\trowd\trrh1134\trqc\clvertalb\cltxlrtb\cellx1701\clvmgf
    intbl\plain\ql\fi0\li0\ri0\sl0\sb0\sa0\fs24 Bottom\cell\pard\intbl
\plain\qc\fi0\li0\ri0\sl0\sb0\sa0\fs20 Vertical\cell\row\
    qc\fi0\li0\ri0\sl0\sb0\sa0\cf1\fs20{\cf0 Black } Red {\cf2 Blue}\cell
\cellx2835\pard\intbl\plain\qc\fi0\li0\ri0\sl0\sb0\sa0\fs20 Normal\
   cell\pard\intbl\plain\qc\fi0\li0\ri0\sl0\sb0\sa0\fs20{\i\caps I
talic}\par +\par{\i\caps Caps}\cell\row\pard\plain\ql\fi0\li3430\
   ri0\sl0\sb120\sa0\f1\fs24 Constantia {\super Superscript}\par{
\fs16 Inline}{\fs28  font size }{\fs16 change}\par{\pict\picscalex50\


\par{\field{\fldinst HYPERLINK "
       cs/RtfConstructor.aspx"}{\fldrslt{\cf2\cb1\ul View article}}}}

The code above has been wrapped to prevent scrolling.

Reflection Part

The conversion of RtfDocument to RTF code is done using Reflection.

Each class representing a control word has a specific attribute which is recognized by the RtfWriter. And that's what makes it easy to expand the library and add your own classes to support more control words. It is somehow similar to what you do when you add System.Xml.Serialization attributes to classes and members for serialization purposes. Except that you will have to study the RTF specification.

It comes as no surprise that the most used attribute is RtfControlWord. If its RtfControlWord.Name property is not set, RtfWriter uses the member name for the control word.

public int Red { get; set; }

Values of int members are appended to control words. RtfWriter ignores members marked with the RtfIndex attribute if their value is -1.

[RtfControlWord("cf"), RtfIndex]
public int TextColorIndex { get; set; }

The enums are special case, and they need the RtfEnumAsControlWord attribute as there are different ways they can be treated.

public enum RtfTableAlign

[RtfEnumAsControlWord(RtfEnumConversion.UseValue, Prefix = "fprq")]
public enum RtfFontPitch
    Default,     // as /fprq0
    Fixed,       // as /fprq1
    Variable     // as /fprq2

public enum RtfDocumentCharacterSet
    ANSI,        // as /ansi
    Mac,         // as /mac
    PC,          // as /pc
    PCa          // as /pca

Some members not marked with RtfControlWord must be included, and RtfInclude tells the RtfWriter to do that.

public class RtfTable
    public RtfTableRowCollection Rows
        get { return _rows; }

Some members should be included only if some conditions are met, and that's where the RtfInclude.ConditionMember property comes in handy.

[RtfControlWord("clbrdrt"), RtfInclude(ConditionMember = "IsTopBorderSet")]
public RtfBorder Top
    get { return top; }

public bool IsTopBorderSet
    get { return Top.Width > 0; }

A bool member marked with RtfControlWord is included only if its value is true.

public bool Bold = false;

Some control words are paired, like /trowd to start a table row, and /row to end it. The RtfControlWordDenotingEnd attribute is used to define the second one.

[RtfControlWord("pard"), RtfControlWordDenotingEnd("cell")]
public class RtfTableCell {

Some members must be ignored, and RtfIgnore is used for that.

public class RtfTableCell
    public RtfTable Table
        get { return RowInternal.Table; }

The RtfEnclosingBraces attribute and its RtfEnclosingBraces.ClosingSemicolon property speak for themselves.

[RtfControlWord("rtf1"), RtfEnclosingBraces]
public class RtfDocument {

[RtfControlWord("f", IsIndexed = true), 
 RtfEnclosingBraces(ClosingSemicolon = true)]
public class RtfFont {

Two attributes left unmentioned are RtfTextData to mark text, and RtfControlGroup which was added for some reason I don't remember. Only font and color tables are marked with it.

Points of Interest

Writing the RtfDocument with a lot of content takes a considerable time, but only once as the information about the types' attributes and members is stored in specific classes. Please take a look at the RtfDocumentInfo, RtfTypeInfo, and RtfAttributeInfo classes.


  • 16.08.10: Code revised, added support for tabs (not shown in the article).
  • 07.08.10: Added support for images, hyperlinks, inline color, and font formatting.
  • 05.08.10: Reflection Part added.
  • 02.08.10: Text revised, RTF code added.
  • 30.07.10: Initial release.


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


About the Author

Dima Popov
Software Developer Energoservice
Russian Federation Russian Federation
Dmitry lives in Arkhangelsk, Russia. He has developed C# applications since 2007.

You may also be interested in...


Comments and Discussions

Questionpage break Pin
Member 1294246029-Apr-17 2:57
memberMember 1294246029-Apr-17 2:57 
QuestionHi sir Page break (RTF Document Constructor) Pin
Member 129424604-Apr-17 3:47
memberMember 129424604-Apr-17 3:47 
QuestionRtfclass file use your code all work completed but one problem not solve there are my report .rtf file i want to something data atomaticaly next page (insert page) break Pin
Member 129424604-Apr-17 2:24
memberMember 129424604-Apr-17 2:24 
QuestionAny thought of getting this to work in UWP apps? Pin
65Goat24-Apr-16 5:49
member65Goat24-Apr-16 5:49 
QuestionNewLine in appending text Pin
rinaldin10-Apr-16 1:37
memberrinaldin10-Apr-16 1:37 
GeneralMy vote of 5 Pin
vperezquilez29-Nov-15 22:13
membervperezquilez29-Nov-15 22:13 
SuggestionThanks for sharing this :), with a small mod it was a key part of a solution Pin
OblivionRedHawk18-Feb-15 2:49
memberOblivionRedHawk18-Feb-15 2:49 
Questiontables without visual border Pin
Member 81354037-Nov-14 21:24
memberMember 81354037-Nov-14 21:24 
AnswerRe: tables without visual border Pin
Member 1159906319-May-15 11:27
memberMember 1159906319-May-15 11:27 
QuestionAdd Paging in the rtf file Pin
mukulsharma114630-Oct-14 21:51
membermukulsharma114630-Oct-14 21:51 
QuestionIRtfDocumentPart missing Pin
Member 1073665929-Aug-14 0:33
memberMember 1073665929-Aug-14 0:33 
QuestionAny code to add ability to embedd rtf strings into the document Pin
Asumege23-Jun-14 22:46
memberAsumege23-Jun-14 22:46 
AnswerRe: Any code to add ability to embedd rtf strings into the document Pin
Dima Popov30-Jun-14 4:04
memberDima Popov30-Jun-14 4:04 
Questionposition image Pin
Member 1006112728-May-14 0:12
memberMember 1006112728-May-14 0:12 
AnswerRe: position image Pin
Dima Popov30-Jun-14 3:57
memberDima Popov30-Jun-14 3:57 
QuestionAdd different colors to different table cells Pin
MattBerry22-May-14 7:09
memberMattBerry22-May-14 7:09 
QuestionHow to Split cells and add a table at the position where the cursor is? Pin
M.Shahid.Sultan7-Oct-13 5:44
memberM.Shahid.Sultan7-Oct-13 5:44 
AnswerRe: How to Split cells and add a table at the position where the cursor is? Pin
Dima Popov7-Oct-13 22:38
memberDima Popov7-Oct-13 22:38 
GeneralRe: How to Split cells and add a table at the position where the cursor is? Pin
M.Shahid.Sultan9-Oct-13 0:33
memberM.Shahid.Sultan9-Oct-13 0:33 
GeneralRe: How to Split cells and add a table at the position where the cursor is? Pin
Dima Popov9-Oct-13 3:08
memberDima Popov9-Oct-13 3:08 
GeneralRe: How to Split cells and add a table at the position where the cursor is? Pin
M.Shahid.Sultan9-Oct-13 5:21
memberM.Shahid.Sultan9-Oct-13 5:21 
GeneralRe: How to Split cells and add a table at the position where the cursor is? Pin
Dima Popov9-Oct-13 5:31
memberDima Popov9-Oct-13 5:31 
GeneralRe: How to Split cells and add a table at the position where the cursor is? Pin
M.Shahid.Sultan9-Oct-13 5:47
memberM.Shahid.Sultan9-Oct-13 5:47 
QuestionHow to trigger any function based on cursor postion when cursor is in cell or table? Pin
M.Shahid.Sultan28-Sep-13 2:08
memberM.Shahid.Sultan28-Sep-13 2:08 
AnswerRe: How to trigger any function based on cursor postion when cursor is in cell or table? Pin
Dima Popov28-Sep-13 23:27
memberDima Popov28-Sep-13 23:27 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.181207.3 | Last Updated 16 Aug 2010
Article Copyright 2010 by Dima Popov
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid