How to print HTML using QHTM
Firstly, to print HTML you do not need a QHTM window. It may seem strange for a control to not require a window but it allows for greater flexibility.
Generally speaking you would use the following pseudo code to print HTML:
- Create a QHTM print context.
- Set the HTML text you wish to print.
- Determine how many pages a print will span.
- For each page
Print the page. - Destroy the QHTM print context.
Here is some simple sample code taken and simplified from the API example:
//
// Get a printer DC
PRINTDLG pd = { 0 };
pd.lStructSize = sizeof(pd);
pd.Flags = PD_NOSELECTION | PD_NOPAGENUMS | PD_USEDEVMODECOPIES | PD_RETURNDC;
pd.hwndOwner = hwndParent;
if( !PrintDlg( &pd ) )
return;
DOCINFO di = { 0 };
di.cbSize = sizeof(di);
di.lpszDocName = g_pszTitle;
if( !PrintDlg( &pd ) )
return;
QHTMCONTEXT qhtmCtx = QHTM_PrintCreateContext( g_nZoomLevel );
if( StartDoc( pd.hDC, &di ) > 0)
{
if( QHTM_PrintSetTextFile( qhtmCtx, g_pcszFileToPrint ) )
{
const RECT rcPage = { 0, 0, GetDeviceCaps( pd.hDC, HORZRES )
, GetDeviceCaps( pd.hDC, VERTRES ) };
//
// Layout our HTML and determine how many pages we need.
int nNumberOfPages;
// We should check this return value but for brevity...
(void)QHTM_PrintLayout( qhtmCtx, pd.hDC, &rcPage, &nNumberOfPages );
//
// For each page...
for( int iPage = 0; iPage < nNumberOfPages; iPage++ )
{
if( StartPage( pd.hDC ) <= 0 )
{
// Error
break;
}
//
// Print our actual page
if( !QHTM_PrintPage( qhtmCtx, pd.hDC, iPage, &rcPage ) )
{
// Error
break;
}
if( EndPage( pd.hDC ) <= 0 )
{
// Error
break;
}
}
if( iPage < nNumberOfPages || EndDoc( pd.hDC ) <= 0 )
{
//
// Error handling //to do
}
}
}
//
// Some cleanup.
DeleteDC( pd.hDC );
pd.hDC = NULL;
(void)QHTM_PrintDestroyContext( qhtmCtx );
Example
There are both MFC and API style examples of how to print.
The MFC example demonstrates using the standard doc-view mechanism complete with print preview.
The API example demonstrates standard API printing and also demonstratses a method you could use to add headers and footers to your printouts.