 |
|
 |
Very simple, clear and usefull!
|
|
|
|
 |
|
 |
I want add to the rtf file.
Like:
BOOL MergeRtf(CStrint strPath1,CString strPath2,CString strOutPath);
thanks for you reply !
|
|
|
|
 |
|
 |
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;
rtf_open( fileNameChar, font_list, color_list );
Thanks in advance.
|
|
|
|
 |
|
 |
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,
int rtf_start_paragraph(char* text, bool newPar)
{
string strText = StrToASC(text);
int error = RTF_SUCCESS;
if(rtfParFormat.paragraphText)
{
delete[]rtfParFormat.paragraphText;
rtfParFormat.paragraphText = NULL;
}
rtfParFormat.paragraphText = new char[strText.size()+1];
strcpy( rtfParFormat.paragraphText, strText.c_str() );
rtfParFormat.newParagraph = newPar;
if( !rtf_write_paragraphformat() )
error = RTF_PARAGRAPHFORMAT_ERROR;
return error;
}
almost thoese are what i did,sorry for my pool English
PS,there are many memory leaks......
|
|
|
|
 |
|
 |
你好 我看了你那个支持中文显示的代码 发现StrToASC函数中for(int i = 0; i<iLen; i++)
和if(chData > 0xa0 && chData != 0)不明白什么意思 能不能解释一下
我现在正在作者方面的东西
|
|
|
|
 |
|
 |
#include <SSTREAM>
#include <WINDOWS.H>
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); std::wstring str(wpstr);
std::ostringstream sstr;
for(int i = 0; i < str.length(); i++)
{
sstr << "\\u" << wpstr[i] << " ";
}
delete[] wpstr;
return sstr.str();
}
|
|
|
|
 |
|
 |
Great!
Thanks.
Now supports Cyrillics!
|
|
|
|
 |
|
 |
I am using vst2005.
rebuilt the sample with the library as part of the source and got a stack corruption error around variable tblrw
int rtf_start_tablerow()
{
// Set error flag
int error = RTF_SUCCESS;
char tblrw[] = "";
tblrw is only allocated with 1 byte of storage in the original code
char tblrw[1024] = "";
fixes the stack.
|
|
|
|
 |
|
 |
IE, my application supports czech, polish and other european languages. I notice the API calls are all char*. Will it cope with these other languages?
|
|
|
|
 |
|
 |
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;
}
almost thoese are what i did,sorry for my pool English
|
|
|
|
 |
|
 |
Nice work!
It's a pity there are so many memory leaks though.
I found an other one in rtf_load_image, near the end of the function there's:
// Convert metafile binary data to hexadecimal
char* str = rtf_bin_hex_convert( buffer, size );
delete []buffer;
but str is never deleted. So add (at the end of the code block):
delete []str;
after:
strcpy( rtfText, "}" );
fwrite( rtfText, 1, strlen(rtfText), rtfFile );
BTW: The code by MRSilver features the same leak
|
|
|
|
 |
|
 |
Gday,
The rtflib was of great help for me. Nevertheless i met some quicks but these have been solved. Additionally i found one not yet discussed in this forum, so i want to share it with you.
In function rtf_write_paragraphformat -rather at the end of the function-, the actual writing to rtfFile takes place. All information is stored into the variable rtfText, which is an array of char of length 4096. When writing long texts, the upper bounds of rtfText are exceeded, which causes nasty errors. Therefore i propose to split the meta information and the actual text and write them separatedly:
// Set paragraph tabbed text
if ( rtfParFormat.tabbedText == false )
{
sprintf( rtfText, "\n%s\\fi%d\\li%d\\ri%d\\sb%d\\sa%d\\sl%d%s ", text,
rtfParFormat.firstLineIndent, rtfParFormat.leftIndent,
rtfParFormat.rightIndent, rtfParFormat.spaceBefore,
rtfParFormat.spaceAfter, rtfParFormat.lineSpacing, font );
}
else
sprintf( rtfText, "\\tab " );
// Writes RTF paragraph formatting properties
if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )
result = false;
// A paragraph text might be long. The variable rtfText is fixed in length
if ( fwrite( rtfParFormat.paragraphText, 1,
strlen(rtfParFormat.paragraphText), rtfFile) <
strlen(rtfParFormat.paragraphText))
result = false;
// Return error flag
return result;
21 cm, Hydrogens universal wavelength
|
|
|
|
 |
|
 |
First thanks for sharing your work.
You can load images with less memory and less code:
IPicture* pPic;
HRESULT hr = OleLoadPicturePath(const_cast(pszImagePath),
NULL,
0,
0,
IID_IPicture,
reinterpret_cast(&pPic));
if(SUCCEEDED(hr) && pPic!= NULL)
{
....
}
instead of:
// Read image file
int imageFile = _open( image, _O_RDONLY | _O_BINARY );
struct _stat st;
_fstat( imageFile, &st );
DWORD nSize = st.st_size;
BYTE* pBuff = new BYTE[nSize];
_read( imageFile, pBuff, nSize );
// Alocate memory for image data
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
void* pData = GlobalLock(hGlobal);
memcpy(pData, pBuff, nSize);
GlobalUnlock(hGlobal);
// Load image using OLE
IStream* pStream = NULL;
if ( CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK )
{
HRESULT hr;
if ((hr = OleLoadPicture( pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
error = RTF_IMAGE_ERROR;
pStream->Release();
}
delete []pBuff;
_close(imageFile);
Thanks again,
Abbas
|
|
|
|
 |
|
 |
Is there a way to set the document to be in landscape instead of protrait mode?
Thanks
|
|
|
|
 |
|
 |
thanks for share although haven't read the code thoroughly, i want to know if it can show jpg files directly in that rtf file.
|
|
|
|
 |
|
 |
I love it extremely, thanks
|
|
|
|
 |
|
 |
Hi, I have a template named hf.dot in which I have defined a header. I want to generate a RTF file that shows that header. Inside the RTF file, I can link to the template and use it's styles with a code like this:
{\template c:\\hf.dot}\linkstyles
but I don't know how to use the header. Any idea ?
|
|
|
|
 |
|
 |
Hi Darko!
Thanks for very usefull library.
I have some solution for unicode suport in mfc applicatons (RTF Specification, version 1.6 on http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp[^]).
In rtflib.h add new function declaration:
int rtf_start_paragraph(CString sUni, bool newPar)
in rtflib.cpp add this code:
int rtf_start_paragraph(CString sUni, bool newPar)
{
char * s = (char *)(LPCTSTR) sUni;
#ifdef _UNICODE
char cWord[1024];
strcpy( cWord , "{\\uc{" );
for(int i = 0; i < sUni.GetLength()*2; i+=2){
char lo = s[i];
char hi = s[i+1];
strcat( cWord , "\\u" );
int iUni = hi*256+lo;
char buffer[20];
_itoa( iUni, buffer, 10 );
strcat( cWord , buffer );
}
strcat( cWord , "}}");
return rtf_start_paragraph(cWord, newPar);
#else
return rtf_start_paragraph(s, newPar);
#endif
}
|
|
|
|
 |
|
 |
It would be nice if this rtflib could be unicode aware!
Andrew
|
|
|
|
 |
|
 |
if(int(lo) < 0 )
iUni += 256;
|
|
|
|
 |
|
 |
rtf_start_paragraph shoulb be like this
int rtf_start_paragraph(char* text, bool newPar)
{
// Set error flag
int error = RTF_SUCCESS;
// Copy paragraph text
rtfParFormat.paragraphText = new char[strlen(text)+1];
strcpy( rtfParFormat.paragraphText, text );
// Set new paragraph
rtfParFormat.newParagraph = newPar;
// Starts new RTF paragraph
if( !rtf_write_paragraphformat() )
error = RTF_PARAGRAPHFORMAT_ERROR;
delete [] rtfParFormat.paragraphText;
rtfParFormat.paragraphText=NULL;
// Return error flag
return error;
}
is required to reserve 1 more char for copying the NULL terminating string on the next strcpy
also the pointer is freed after being used.
more memory leaks on rtf_write_paragraphformat:
...
// Format paragraph border type
char *br = rtf_get_bordername(rtfParFormat.BORDERS.borderType);
strcat( border, br );
// add this after the call to delete the returned pointer
delete []br;
br=NULL;
and more on the same function at:
// Format paragraph shading
char* sh = rtf_get_shadingname( rtfParFormat.SHADING.shadingType, false );
// add this after the call to delete the returned pointer
strcat( text, sh );
delete sh;
sh=NULL;
some improvements could be made to insert a picture that you've already loaded from file, add this function to the library (.cpp and .h) and you're done:
// Inserts image from IPicture
int rtf_insert_image(IPicture* pImage, int width, int height)
{
// Set error flag
int error = RTF_SUCCESS;
// If IIpicture is valid
if ( pImage != NULL )
{
// Calculate image size
long hmWidth;
long hmHeight;
pImage->get_Width(&hmWidth);
pImage->get_Height(&hmHeight);
int nWidth = MulDiv( hmWidth, GetDeviceCaps(GetDC(NULL),LOGPIXELSX), 2540 );
int nHeight = MulDiv( hmHeight, GetDeviceCaps(GetDC(NULL),LOGPIXELSY), 2540 );
// Create metafile;
HDC hdcMeta = CreateMetaFile(NULL);
// Render picture to metafile
pImage->Render( hdcMeta, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL );
// Close metafile
HMETAFILE hmf = CloseMetaFile(hdcMeta);
// Get metafile data
UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
BYTE* buffer = new BYTE[size];
GetMetaFileBitsEx( hmf, size, buffer );
DeleteMetaFile(hmf);
// Convert metafile binary data to hexadecimal
char* str = rtf_bin_hex_convert( buffer, size );
delete []buffer;
// Format picture paragraph
RTF_PARAGRAPH_FORMAT* pf = rtf_get_paragraphformat();
pf->paragraphText = "";
rtf_write_paragraphformat();
// Writes RTF picture data
char rtfText[1024];
sprintf( rtfText, "\n{\\pict\\wmetafile8\\picwgoal%d\\pichgoal%d\\picscalex%d\\picscaley%d\n", hmWidth, hmHeight, width, height );
if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )
error = RTF_IMAGE_ERROR;
fwrite( str, 1, 2*size, rtfFile );
strcpy( rtfText, "}" );
fwrite( rtfText, 1, strlen(rtfText), rtfFile );
} else
{
error = RTF_IMAGE_ERROR;
}
// Return error flag
return error;
}
|
|
|
|
 |
|
 |
When creating tables, sometimes initial cell text contents is "\trleft0" and then the rest of cell text. What am I doing wrong?
Mei You Qian
|
|
|
|
 |
|
 |
FYI, problem appears only when including the rtflib.cpp file.
If I link with rtflib.lib, the problem does not appear.
May be the rtflib.cpp file is outdated?
Mei You Qian
|
|
|
|
 |
|
 |
In rtflib.cpp, line 1053, replace
char tblrw[] = "";
with
char tblrw[5];
|
|
|
|
 |
|
 |
Thank you very much!
Mei You Qian
Barcelona/Changsha/Nanning
|
|
|
|
 |
|