This article is about generating RTF documents from the source code. For this purpose, a free C-library is developed in order to help developers. Considering this release, 100% compatibility is achieved only with MS Word 97 to MS Word 2003 applications used as external RTF viewer to generated RTF documents. For the future, it is planned to extend this to all possible RTF viewers that support RTF specification 1.8 which is documented by Microsoft and can be found here.
A developer may use
CRichEditCtrl, a MFC control, in order to get RTF output from the source code. This is the control upon which WordPad application is built. But, there are many limitations when considering the generated RTF file. Also, an RTF document can be created with any advanced text editor directly. This library, however, provides a low-level interface to RTF syntax which is not so easy to be hand-coded like HTML. It can be extended and adopted to custom needs. Also, a custom high-level interface is a challenge to be built on this low-level interface base.
Using the code
A full description of using the library code is provided in a separate guide (can be found in the download section above). To use the library in a custom project, include the provided header files (rtflib.h, errors.h, globals.h) and add links in project settings to rtflib.lib compiled module, or download library source and compile it and then include headers and add link to the compiled module. The library source is not hard-tested so memory leaks or some other bugs are possible to be found. If this is the case, please report. But, don't consider wrong output on destination RTF viewer as library error. It is stated here that full compatibility is achieved, for this release, only with MS Word 97 to MS Word 2003 applications.
See an example on using the library below:
// // "HelloWorld" in RTF document example
// Add links to header files (and link to rtflib.lib in project settings)
// #include "rtflib.h"
// #include "errors.h"
// #include "globals.h"
// void main()
// // RTF document font and color table definition
// char font_list = “Times New Roman;Arial;”;
// char color_list = “0;0;0;255;0;0;192;192;192”;
// // Create new RTF document
// rtf_open( “Sample.rtf”, font_list, color_list );
// // Write simple paragraph text
// rtf_start_paragraph( “Hello, World !!!”, false );
// // Close RTF document
Points of Interest
I am interested in continuing the work on this library and hope that interested developers will include with their ideas and solutions. A C++ class solution is very simple to be designed on this base. I also plan to write similar PDF generating libraries.
rtflib v1.0 features:
- document creating and formatting
- section creating and formatting
- paragraph creating and formatting
- character formatting
- images loading (*.bmp, *.jpg, *.gif)
- table creating and formatting
- bullets and numbering