Get Ready to Unleash the
Power of UCanCode .NET

UCanCode
Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database
workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"

To
edit formatted text,
Windows provides a
RichEdit control (RICHED32.DLL,
respectively RICHED20.DLL), which is
encapsulated in MFC
using the RichEditCtrl
class. It is stored in the RichEdit (RTF)
format;
WordPad and MS Word are examples of applications that use
RTF files.
But today's world is speaking
HTML, so I
looked for a possibility to
convert RTF data to
HTML data. I haven't found any free
example yet,
so I decided to write my own class as an
Exporter. Here it is.

The source zip
file contains the needed classes and file:

RTF2HTMLConverter.h/cpp, which is the main
converter
class CRTF_HTMLConverter

The converter
class itself does no reading or writing from/to
files or RichEdit
controls; this has to be done
outside. (For example,
to learn how to stream the complete
RTF content
from/into a RichEdit
controls, just look here in the same
section.) The class is derived only from CObject,
and works with CString >>/<< streaming
functions. When streaming in, the data is
converted.

Note:
Only the RTF->HTML
direction is supported at the moment. There is
also a very small subset of possible
RTF
supported, at this time.

Bold,
Italic, Underline

Font Size,
Color, and Face

Paragraph
alignment

Special
characters, such as encoded German Umlauts

I hope the
class is easy to extend (for new tags, mostly
::R2H_InterpretTag has to be modified) and any
suggestions or extensions are very welcome; I'll
post them here. But please don't give me "My
RTF file
isn't correctly exported" comments; I mentioned
it is only a demo and only a few tags are
currently supported. I've made my
RTF file
using the WordPad editor shipped with Windows;
MS Word builds a more complex
RTF
structure. For complete RTF documentation, see MSDN ("RTF
Specification").

An
RTF file
stores text data in a structured way, together
with formatting tags (slightly similar to
HTML). Let's
have the following
example:

ConvertRTF2HTML is the main converting
procedure. It performs the following steps:

R2H_BuildTree
As you see, RTF
has a nested structure, where each section is
in braces {}. So, our first step is to build a
tree structure :
+RTF1
+COLORTBL
+FONTTBL
+F0
+F1
+F2
+F3
Here, I've just noted the section's first
attribute (section name). Each section then
contains more code; both plain text (RTF1 is
the main section with the main text) and
attributes.

R2H_SetMetaData
Sub-Items such as colortbl and fonttbl are
helper tables and in the main text's RTF tags
there are references to it, so these global
attributes have to be scanned and stored.

RTF tags
starting with a \. These have to be
converted to the correspondig HTML tags with
R2H_InterpretTag. Sometimes, there
must be look-ups in global tables (e.g.
color or font table), or previously inserted
elements must be scanned or modified.