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.

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.

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.

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.

[RtfControlWord]
publicint 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.

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.

History

16.08.10: Code revised, added support for tabs (not shown in the article).

I would suggest moving the reflect stuff into a static class and calling it from the RTFDocument "ToString" and then you don't really need the RTFwriter class at all. I would also consider renaming RTFDocument as RTFBuilder to mirror stringbuilder. But all of this is rather selfishly to make the aesthetics of my solution nicer :p. Good work!

I wrote my own rtf-constructor library, especialy to make reports of technical calculations. It works well, but ...
I wanted to make a table without any border. But he Rtf-editor always shows a light border . Is there a possibility to create tables without visual border?

I have tried adding the BackgroundColorIndex property but it seems to be table wide, could you tell me how to color individual cells? I need to have different fonts and different colors in each cell. If I instantiate the table.defaultcellstyle I can set the fonts but not the background colors and if I don't instantiate it I can set the colors but not the fonts. What am I doing wrong please help?

Sir, does your library have anything that could split up cells? If yes, then please tell me how to do this via short example? Secondly, how to add a table at specified position specifically where the cursor is? Moreover, please give me a small example on how to merge cells ,though i went through the article but i didn't get it. Please give me any small example to achieve these? And last but not the least, how to remove a single row and adding a single column in a existing table?

Again you ask something that doesn't relate to the library. It was meant for creating static documents (e.g. reports) and not for interactive editing. So if you want to use it this way (inserting tables at cursor etc.), you are on your own. There are better places to ask general questions.

I doubt that you have read the article if you ask about merging cells. (Yes, there's not much on it in the text, but I don't think you've even tried to use MergeCellsHorizontally or MergeCellsVertically). I doubt that you are using the library at all if you ask how to remove columns, and I doubt that you have an idea of what a collection is. So I doubt that you have the basic knowledge of the language you're trying to use. And I suggest you to read a good introductory book on C#, and to move step by step to some more complicated stuff. And it's a lot of work, you know.

Consider you find yourself in the dark. A place with no light at all. You think you're in a room and search for a door, but keep hitting the walls. That's what you do with programming, except that you sit there in the dark and wait for someone else to pull you out. I see the code I posted unmodified. That's quite impressive!

You can't write software without knowledge. You learn by doing. You can find the ideas in books, get help from the community online, but most of the time you try and fail, then try again. You can't learn without practice.

Ok, I noticed that you've made at least a humble attempt:

richTextBox.Rtf += rtfString;

I'll explain why this line makes no sense. The property you try to append text to contains the complete rtf document. It starts with '{' and ends with '}'. So it won't work this way. You'll have to modify the document and replace the contents of the control.

But seriously, here's my advice to you - forget it. This may sound harsh, but try something else. Maybe another language, another project. Or another activity like gardening, or maths, or boxing, or whatever. You want to build a house and you just sit there watching the bricks. You have to get your hands dirty to get the job done. If you don't understand what you're doing, learn. If you don't want to learn, look in another direction.

Thanks Sir for your patience and yeah i am literally a dumb idiot and beg your pardon as i thought, i could get a help no matter how dumb questions i am asking but thinking that someone could try to answer rather taunting!

Well, i was just supposing taking this piece of code(i didn't actually considering this piece of code) and i did every thing and after getting all them implemented, i just didn't get how to add/append another table if i hit a button second or any other time. I am learning by doing and doing by learning!

If you use RichTextBox there's no way you can do it (as far as I know). So you'll have to create your own control, that means parsing Rtf to a representation of its structure, and creating a view of that structure. That's really not an option for you. What is that you're trying to do? It seems that you're solving the wrong problem.

yeah i am trying to parse a cell. Yeah i 'll make my own control that would be displayed but what about your cell i mean to say that how could i know that there is a cell under the pointer? Please help me!

I'll rephrase. If you plan to use RichTextBox, I don't see any possibility to find out what's under the pointer.

If you create your own RichTextBox, you'll have full control. But it is a really complicated task. You'll need several modules. One for displaying text nicely formatted and storing information about the coordinates (that's how you know what's under the pointer). The other one is that parses RTF into some in-memory representation (maybe the one described in the article).

I don't see how you can implement this. It will take a lot of time, and I'm not sure you're skilled enough. So maybe there's another way to do what you need?

I got your point Sir and would be upto something else regarding this. Well, as far as my skill is concerned, though i can't take the risks to implement it right now as it would really be taking long enough to get completed and i don't have time yet to do this stuff as my project has got more other stuff to do, but would be doing this another moment.

Thanks Sir, i will tell you after getting it implemented. By the way one suggestion i would like to give you about your article for your marvelous effort you have put to write this amazing piece of code is that if you could explain it meticulously so we could have more grip at library controls manipulation!