|
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)<br />
{<br />
char * s = (char *)(LPCTSTR) sUni;<br />
#ifdef _UNICODE<br />
char cWord[1024];<br />
strcpy( cWord , "{\\uc{" );<br />
for(int i = 0; i < sUni.GetLength()*2; i+=2){<br />
char lo = s[i];<br />
char hi = s[i+1];<br />
strcat( cWord , "\\u" );<br />
int iUni = hi*256+lo;<br />
char buffer[20];<br />
_itoa( iUni, buffer, 10 );<br />
strcat( cWord , buffer );<br />
}<br />
strcat( cWord , "}}");<br />
return rtf_start_paragraph(cWord, newPar);<br />
#else<br />
return rtf_start_paragraph(s, newPar);<br />
#endif<br />
}<br />
|
|
|
|
|
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
|
|
|
|
|
function :
int rtf_start_paragraph(char* text, bool newPar)
{
.......
rtfParFormat.paragraphText = new char[strlen(text)];
......
}
must be change :
rtfParFormat.paragraphText = new char[strlen(text)+1];
because the end of line char '\0' cuase crash
thanks
|
|
|
|
|
Dear sir
it's nice but:
many times call below function with different str value,
it work in VC7 project correctly,
but crash with exe debug
void CRichEditWriter::rtf_start_paragraph_SABA(LPCTSTR str, bool BreakLine )
{
ch = new char[str.GetLength()+1];
LPSTR pstr = str;
strcpy(ch,pstr)
rtf_start_paragraph(ch, BreakLine );
}
hamid danaei
|
|
|
|
|
------------------------------------
In your function rtf_start_tablerow:
char tblrw[] = "";
and then:
strcat(tblrw,"\\trql");
....
strcat(tblrw,"\\trqr");
What is it?
------------------------------------
|
|
|
|
|
I modified the demo program and added some Chinese characters. The Chinese characters contained in the generated sample.rtf can't be displyed correctly by MS word2003. I wonder if this library support Unicode characaters? Thanks!
|
|
|
|
|
As it stands it is not unicode aware. All of the function calls are char* and the header has the code page hardcoded to 1252.
It would have to be modified to be unicode aware for other language support. But since the author has not replied to you in 3 years, I doubt he will reply to me either.
Andrew
PS. But thank you for providding the library - it is a great concept - if only unicode aware
|
|
|
|
|
I used the code to migrate it to C# but I have some problems with the image so i am working on that
ElToBiS (Mexico)
|
|
|
|
|
Your rtf parser is an interesting contribution. Thanks a lot!!
I would like to observe the following:
When rtlib v1.0 starts new RTF paragraph, you create a new char array as follows:
rtfParFormat.paragraphText = new char[strlen(text)];
That's fine, but the necessary delete [] rtfParFormat.paragraphText pair is missing, resulting in memory leaks. Similar situations are observed in other places else of your code, where you dynamically create arrays.
Leopoldo Peralta
|
|
|
|
|
Further on the undeleted memory issue:
Each time you insert a paragraph, you append it to the rtfFile stream that contains the rtf document.
Instead of doing it, implementing a RTF document as a collection of section structures, each section being composed of a collection of paragraph structures, will greatly help in the task of freeing allocated memory.
Under this approach, first, you fill the collection, then, you iterate the document to write it to file, and at last, you properly de allocate unused memory ('delete [] rtfParFormat.paragraphText' for each entry).
Additionally, have you consider to include in your RTF lib functionality to do the opposite, parsing well-formed rtf text into plain text?
Leopoldo Peralta
|
|
|
|
|
you can add the following code:
///////////////////////
if(rtfParFormat.paragraphText!=NULL&&
strcmp(rtfParFormat.paragraphText,""))// add the senctence
{
delete[]rtfParFormat.paragraphText;
rtfParFormat.paragraphText = NULL;
}
/////////////////
before
// Copy paragraph text
|
|
|
|
|
This would be nice too. Will look into the RTF spec and see if I can expand the library and send you the update - unless you already have that in the making
|
|
|
|
|
I see there is a rtfSecFormat.showPageNumber but I seem unable to make the page number appear when viewing the document in word. Anyone have an example with working page numbers?
|
|
|
|
|
To get page numbering do the following:
RTF_SECTION_FORMAT* sf = rtf_get_sectionformat();
sf->newSection = false;
sf->showPageNumber = true;
sf->pageNumberOffsetX = 720;
sf->pageNumberOffsetY = 720;
rtf_write_sectionformat();
This is done only once for the first section in the document and then page numbering can be turned off later.
|
|
|
|
|
Great library! It would be nice if jpeg's were stored directly inside the RTF file instead of converting to WMF storing it as WMF inside the file This means drasticly smaller rtf files since it images are stored natively|. Word supports viewing rtf documents which uses jpeg's directly.
|
|
|
|
|
That would be a very nice upgrade of the rtf_load_image() function.
Thanks...
|
|
|
|
|
Got good compile and link, but when tried to run sample, it crashed.
William
Fortes in fide et opere!
|
|
|
|
|
I know, but an update is on the way. Compile only library source and using rtflib_guide make your own sample. It will for sure work.
PS.
Sample crashes because I forgot to delete a path to image file. Remove it in the demo and it will not crash. Or change the path to some your picture.
|
|
|
|
|
Thank you for the work. After I builded the project and viewed the sample.rtf file, I found the table cells not like your posted picture in this article. There are three cells in the table, but there are two cells those have black background and the other has gray one. Why?
Can you help me? Please?
Ziyoo
www.ziyoo.com
|
|
|
|