// Rtf2Html.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Rtf2Html2.h"
#include "afxhtml.h"
CWinApp theApp; // Win32 App with MFC support
CRichEditCtrl g_ctlRichEdit; // the two controls
CHtmlEditCtrl g_ctlEditHtml;
// This is needed to load the RichEdit control from a file
static DWORD CALLBACK
MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
CFile* pFile = (CFile*) dwCookie;
*pcb = pFile->Read(pbBuff, cb);
return 0;
}
void LoadRtfFile( LPCTSTR pszFilename )
{
wchar_t szFilter[] = L"RTF files (*.rtf)|*.rtf;|"
L"All Files (*.*)|*.*||";
CFileDialog dlg(TRUE,0,pszFilename,6,szFilter );
if ( dlg.DoModal()!=IDOK ) {
return;
}
CFile cf( dlg.GetPathName(),CFile::modeRead );
// CFile cf( pszFilename,CFile::modeRead );
EDITSTREAM es;
es.dwCookie = (DWORD)&cf;
es.pfnCallback = MyStreamInCallback;
g_ctlRichEdit.StreamIn( SF_RTF, es ); // load from the file
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
AfxInitRichEdit2(); // needed for using CRichEditCtrl
CWnd* pWnd = CWnd::GetDesktopWindow();
CRect r(0,0,200,200);
g_ctlRichEdit.Create( ES_MULTILINE, r, pWnd, 1111);
g_ctlEditHtml.Create( 0,0, r, pWnd, 2222 );
LoadRtfFile( argv[1] ); // read the RTF file into the ctrl
g_ctlRichEdit.SetSel(0,-1); // select all in the RTF ctrl
g_ctlRichEdit.Copy(); // copy to clipboard
g_ctlEditHtml.Paste(); // paste into the Html Edit ctrl
g_ctlEditHtml.SaveAs( L"C:\\temp\\test.html"); // save HTML
return 0;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=unicode" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.7600.16535"></HEAD>
<BODY><FONT size=3>
<P>This is a </FONT><B><FONT color=#ff0000 size=3><FONT color=#ff0000
size=3>test </B></FONT></FONT><FONT size=3>This </FONT><B><I><FONT
size=6>is</B></I></FONT><FONT size=3> a </FONT><B><I><FONT size=4
face="Arial Black"><FONT size=4
face="Arial Black">Test</P></B></I></FONT></FONT><FONT size=3></FONT>
<TABLE dir=ltr border=1 cellSpacing=1 borderColor=#000000 cellPadding=7
width=157>
<TBODY>
<TR>
<TD width="58%"><FONT size=3>
<P>1a</FONT></P></TD>
<TD width="42%"><FONT size=3>
<P>1b</FONT></P></TD></TR>
<TR>
<TD width="58%"><FONT size=3>
<P>2a</FONT></P></TD>
<TD width="42%"><B><FONT color=#9b00d3 size=3 face=Broadway><FONT
color=#9b00d3 size=3 face=Broadway><FONT color=#9b00d3 size=3
face=Broadway>
<P>2b</B></FONT></FONT></FONT></P></TD></TR></TBODY></TABLE><FONT color=#ff0000
size=3><FONT color=#ff0000 size=3></FONT></FONT><FONT size=3>
<P>end of test</P></FONT></BODY></HTML>
Note that in addition to setting the fonts and coloring, it has correctly created and populated a <TABLE> element (Incidentally, I used Office Word to generate the RTF because WordPad does not support creating Tables). In other words, the HTML conversion appears to be quite robust and complete.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)