I've got a CDC and I need to export to pdf
Do you have a sample which makes this ?
Thank you for your help
c++ CDC to pdf
ce que je fais et qui doit etre incorrect
Voilà ce que j'ai fait
voyez-vous une erreur ?
CExporttoPDF::CExporttoPDF(CDC* pDC,CRect rectPrint)
{
//ExporttoPDF();
m_strPrinterName = AMYUNI_PRINTER;
m_strPDFPrinterName = PDFPRINTER;
m_szCompany= LICENSTO;
m_szCode = CODE;
m_pDC = pDC;
m_rectPrint = rectPrint;
}
void CExporttoPDF::ConverttoPDF(int type)
{
m_hPrinter = DriverInit( m_strPrinterName );
//Check if retrieved printer handle
if ( NULL == m_hPrinter )
{
// cannot initialize new printer, get the error message from CDINTF
CHAR buf[255];
GetLastErrorMsg( buf, sizeof( buf ) );
// CString will convert error message to Unicode if necessary
::MessageBox( NULL, CString(buf), _T( "Error initializing printer" ),MB_OK | MB_ICONWARNING );
}
CDISetDefaultPrinter( m_hPrinter );
SetDefaultFileName ( m_hPrinter, _T(m_strFileTo) );
SetFileNameOptions (m_hPrinter, NoPrompt | UseFileName);
//Need to do this each time output is sent to the PDF Converter
EnablePrinter ( m_hPrinter, m_szCompany , m_szCode );
//print something to the PDF Converter
PrintBmp();
//restauration etat innitial imprimante
RestoreDefaultPrinter ( m_hPrinter );
DriverEnd ( m_hPrinter );
}
void CExporttoPDF::PrintBmp()
{
// CBitmap bmpSrc;
// BITMAP bmp;
// bmpSrc.LoadBitmap(IDB_BITMAP_LOGO); //from resource
// bmpSrc.GetObject( sizeof(bmp), &bmp );
CDC dc;
DOCINFO di;
//create a printer device context
if ( !dc.CreateDC( _T( "WINSPOOL" ), m_strPrinterName, NULL, NULL ) )
{
// cannot initialize new printer, get the error message from CDINTF
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
// CString will convert error message to Unicode if necessary
::MessageBox( NULL, CString(buf), _T("NULL == hDC"),
MB_OK | MB_ICONWARNING );
}
// init the DOCINFO structure, set the output file name
memset( &di, 0, sizeof(di) );
di.cbSize = sizeof( di );
di.lpszDocName = "Test document";
// di.lpszOutput = "c:\\temp\\myTest.pdf";
//If the function succeeds, the return value is greater than zero. This value is the print job identifier for the document.
//Windows 95/98: If the function succeeds, the return value is always 1.
//If the function fails, the return value is less than or equal to zero.
//Windows NT/Windows 2000: To get extended error information, call GetLastError.
int y;
y = dc.StartDoc( &di );
// start printing
if ( y <= 0 )
//if ( ::StartDoc( dc, &di ) <= 0 )
{
// StartDoc failed
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
// CString will convert error message to Unicode if necessary
::MessageBox( NULL, CString(buf), _T("StartDoc <= 0"),
MB_OK | MB_ICONWARNING );
}
//If the function succeeds, the return value is greater than zero.
//If the function fails, the return value is less than or equal to zero.
//Windows NT/Windows 2000: To get extended error information, call GetLastError.
if ( dc.StartPage() <= 0 )
{
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
::MessageBox( NULL, CString(buf), _T("StartPage <= 0"),
MB_OK | MB_ICONWARNING );
}
//***** print bitmap
// m_pDC->CreateCompatibleDC(m_pDC);
// dcSrc.CreateCompatibleDC( NULL );
// dcSrc.SelectObject( &bmpSrc );
dc.BitBlt(0, 0, m_rectPrint.Width(), m_rectPrint.Height(),
m_pDC, 0, 0, SRCCOPY);
if ( EndDoc( dc ) <= 0 )
{
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
::MessageBox( NULL, CString(buf), _T("EndDoc <= 0"),
MB_OK | MB_ICONWARNING );
}
DeleteDC( dc );
}
voyez-vous une erreur ?
CExporttoPDF::CExporttoPDF(CDC* pDC,CRect rectPrint)
{
//ExporttoPDF();
m_strPrinterName = AMYUNI_PRINTER;
m_strPDFPrinterName = PDFPRINTER;
m_szCompany= LICENSTO;
m_szCode = CODE;
m_pDC = pDC;
m_rectPrint = rectPrint;
}
void CExporttoPDF::ConverttoPDF(int type)
{
m_hPrinter = DriverInit( m_strPrinterName );
//Check if retrieved printer handle
if ( NULL == m_hPrinter )
{
// cannot initialize new printer, get the error message from CDINTF
CHAR buf[255];
GetLastErrorMsg( buf, sizeof( buf ) );
// CString will convert error message to Unicode if necessary
::MessageBox( NULL, CString(buf), _T( "Error initializing printer" ),MB_OK | MB_ICONWARNING );
}
CDISetDefaultPrinter( m_hPrinter );
SetDefaultFileName ( m_hPrinter, _T(m_strFileTo) );
SetFileNameOptions (m_hPrinter, NoPrompt | UseFileName);
//Need to do this each time output is sent to the PDF Converter
EnablePrinter ( m_hPrinter, m_szCompany , m_szCode );
//print something to the PDF Converter
PrintBmp();
//restauration etat innitial imprimante
RestoreDefaultPrinter ( m_hPrinter );
DriverEnd ( m_hPrinter );
}
void CExporttoPDF::PrintBmp()
{
// CBitmap bmpSrc;
// BITMAP bmp;
// bmpSrc.LoadBitmap(IDB_BITMAP_LOGO); //from resource
// bmpSrc.GetObject( sizeof(bmp), &bmp );
CDC dc;
DOCINFO di;
//create a printer device context
if ( !dc.CreateDC( _T( "WINSPOOL" ), m_strPrinterName, NULL, NULL ) )
{
// cannot initialize new printer, get the error message from CDINTF
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
// CString will convert error message to Unicode if necessary
::MessageBox( NULL, CString(buf), _T("NULL == hDC"),
MB_OK | MB_ICONWARNING );
}
// init the DOCINFO structure, set the output file name
memset( &di, 0, sizeof(di) );
di.cbSize = sizeof( di );
di.lpszDocName = "Test document";
// di.lpszOutput = "c:\\temp\\myTest.pdf";
//If the function succeeds, the return value is greater than zero. This value is the print job identifier for the document.
//Windows 95/98: If the function succeeds, the return value is always 1.
//If the function fails, the return value is less than or equal to zero.
//Windows NT/Windows 2000: To get extended error information, call GetLastError.
int y;
y = dc.StartDoc( &di );
// start printing
if ( y <= 0 )
//if ( ::StartDoc( dc, &di ) <= 0 )
{
// StartDoc failed
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
// CString will convert error message to Unicode if necessary
::MessageBox( NULL, CString(buf), _T("StartDoc <= 0"),
MB_OK | MB_ICONWARNING );
}
//If the function succeeds, the return value is greater than zero.
//If the function fails, the return value is less than or equal to zero.
//Windows NT/Windows 2000: To get extended error information, call GetLastError.
if ( dc.StartPage() <= 0 )
{
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
::MessageBox( NULL, CString(buf), _T("StartPage <= 0"),
MB_OK | MB_ICONWARNING );
}
//***** print bitmap
// m_pDC->CreateCompatibleDC(m_pDC);
// dcSrc.CreateCompatibleDC( NULL );
// dcSrc.SelectObject( &bmpSrc );
dc.BitBlt(0, 0, m_rectPrint.Width(), m_rectPrint.Height(),
m_pDC, 0, 0, SRCCOPY);
if ( EndDoc( dc ) <= 0 )
{
CHAR buf[255];
GetErrorMessageText( buf, sizeof(buf) );
::MessageBox( NULL, CString(buf), _T("EndDoc <= 0"),
MB_OK | MB_ICONWARNING );
}
DeleteDC( dc );
}