Click here to Skip to main content
Click here to Skip to main content

rtflib v1.0

By , 5 Jun 2005
 

Sample Image

Introduction

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.

Background

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
//     rtf_close();
// }
//

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.

History

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
  • tabs
  • columns
  • bullets and numbering

License

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

About the Author

darkoman
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
Member
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionLicense for RFTLIBmemberPaolo Rugarli26 Sep '12 - 7:27 
Hi
 
I was just looking for such a tool, i.e. an engine which can drive the creation of an RTF file from source, i.e. without user interaction via CRichEditCtrl.
 
Please let me know if I may use this in order to embed the lib into a commercial sw or not and which is the cost if there is one.
 
Best regards and many thanks
Paolo
AnswerRe: License for RFTLIBmemberdarkoman26 Sep '12 - 9:51 
Hello,
 
thanks for the interest on control.
On the CodeProject web site all source code is free to download and modify according to your needs. The CPOL (Code Project Open Licence) is described here:
http://www.codeproject.com/info/cpol10.aspx[^]
So, the software (or the source code) is free to be used in any type of application: free or commercial.
If you feel you would like to make a donation, you are free to contact me directly on the following e-mail:
darko1978@open.telekom.rs
 

Best regards,
Darkoman
"Avaritia est radix omnium malorum..."

GeneralMy vote of 5memberK4HVDs30 Mar '12 - 8:52 
Very simple, clear and usefull!
GeneralHow to merge two rtf files ?memberKoma Wang12 Apr '11 - 20:01 
Smile | :)
 
I want add to the rtf file.
 
Like:
 
BOOL MergeRtf(CStrint strPath1,CString strPath2,CString strOutPath);
 
thanks for you reply !
QuestionTable width and paper widthmembernaghekyan9 Jul '10 - 4:19 
I would like to understand how to set table width, table row width and paper width.
 
For paper width I have tried this, but there is no effect:
 
 RTF_DOCUMENT_FORMAT * df = rtf_get_documentformat();
 df->gt;marginLeft = 10;
 df->gt;marginRight = 10;
 df->gt;paperWidth= 850;
 

  // Open RTF file
  rtf_open( fileNameChar, font_list, color_list );
 
Thanks in advance.
GeneralI modified a copy to support the Chinese languagememberzyj40230126 Apr '10 - 5:40 
I modified a copy to support the Chinese language,
I modified funciton rtf_write_header,change language
strcpy( rtfText, "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052{\\fonttbl" );
 
modiyed fuciton rtf_init,to add chinese fonts
strcat( rtfFontTable, "{\\f0\\froman\\fcharset0\\fprq2{\\*\\panose 02020603050405020304}Times New Roman;}" );
	strcat( rtfFontTable, "{\\f13\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'cb\\'ce\\'cc\\'e5{\\*\\falt SimSun};}" );
	strcat( rtfFontTable, "{\\f17\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'ba\\'da\\'cc\\'e5{\\*\\falt SimHei};}" );
	strcat( rtfFontTable, "{\\f109\\froman\\fcharset238\\fprq2 Times New Roman CE;}" );
	strcat( rtfFontTable, "{\\f110\\froman\\fcharset204\\fprq2 Times New Roman Cyr;}" );
	strcat( rtfFontTable, "{\\f55\\fmodern\\fcharset134\\fprq1{\\*\\panose 02010609030101010101}\\'bf\\'ac\\'cc\\'e5_GB2312;}" );
	strcat( rtfFontTable, "{\\f621\\fnil\\fcharset0\\fprq2 @\\'cb\\'ce\\'cc\\'e5 Western;}" );
 
Add a funciton to translate chinese string to ascii string
string StrToASC(char* text)
{
	string strResult;
	int iLen = strlen(text);
	for(int i = 0; i<iLen; i++)
	{
		BYTE chData = text[i];
		if(chData > 0xa0 && chData != 0)
		{
			char szHex[12]={0};
			wsprintf(szHex,"\\'%2.2x", chData);
			strResult += szHex;
		}
		else
		{
			strResult += (char) chData;
		}
	}
 
	return strResult;
}
 
then modified fuction rtf_start_paragraph,
// Starts new RTF paragraph
int rtf_start_paragraph(char* text, bool newPar)
{
	string strText = StrToASC(text);
 
	// Set error flag
	int error = RTF_SUCCESS;
 
	// Copy paragraph text
	if(rtfParFormat.paragraphText)
	{
		delete[]rtfParFormat.paragraphText;
		rtfParFormat.paragraphText = NULL;
	}
 
	rtfParFormat.paragraphText = new char[strText.size()+1];
	strcpy( rtfParFormat.paragraphText, strText.c_str() );
 
	// Set new paragraph
	rtfParFormat.newParagraph = newPar;
 
	// Starts new RTF paragraph
	if( !rtf_write_paragraphformat() )
		error = RTF_PARAGRAPHFORMAT_ERROR;
 
	// Return error flag
	return error;
}
almost thoese are what i did,sorry for my pool English
PS,there are many memory leaks......
GeneralRe: I modified a copy to support the Chinese languagegrouptianyige072625 Aug '10 - 15:47 
你好 我看了你那个支持中文显示的代码 发现StrToASC函数中for(int i = 0; i<iLen; i++)
和if(chData > 0xa0 && chData != 0)不明白什么意思 能不能解释一下
我现在正在作者方面的东西
GeneralRe: I modified a copy to support the Chinese language [modified]memberchenlilming19 Jul '12 - 19:49 
这个是由于网页显示问题,实际上你要是懂html的话,你就懂了,比如>这个代表的是大于号,其它慢慢查吧,在html相关的符号说明有的
This is the web page display problems, in fact, if you know html, then you will understand, such as>   this represents a greater-than symbol, the other you can check the book about html and you can find it

-- modified 20 Jul '12 - 2:12.
GeneralUnicode patchmembermargaritaville17 Sep '09 - 15:18 
#include <SSTREAM>
#include <WINDOWS.H>

/************************************************************************/
/* Takes stream with unicode characters, convert it into RTF format 
/* string, like \u2514.
/************************************************************************/
inline std::string InputToRTFstream(const char* input)
{
	DWORD dwNum = ::MultiByteToWideChar (CP_ACP, 0, input, -1, NULL, 0);
	wchar_t* wpstr = new wchar_t[::strlen(input) + 1];
	::MultiByteToWideChar(CP_ACP,   0,   input,   -1,   wpstr,   dwNum);   //ASCII   to   UNICODE 
	std::wstring str(wpstr);
	std::ostringstream sstr;
 
	for(int i = 0; i < str.length(); i++)
	{
		sstr << "\\u" << wpstr[i] << "  ";
	}
 
	delete[]   wpstr; 
 
	return sstr.str();
}

GeneralRe: Unicode patchmember_EOA_26 Dec '10 - 7:29 
Great!
Thanks.
Now supports Cyrillics!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 6 Jun 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid