c++ CDC to pdf

If you are a C/C++/C# developer and have any questions about using our products from your application here is the place to post them.
Post Reply
makina
Posts: 14
Joined: Fri Apr 14 2006

c++ CDC to pdf

Post by makina »

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
makina
Posts: 14
Joined: Fri Apr 14 2006

ce que je fais et qui doit etre incorrect

Post by makina »

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 );
}
makina
Posts: 14
Joined: Fri Apr 14 2006

Post by makina »

Do you think I need to make a bmp with my CDC first then convert to pdf ?
Can't I convert my CDC to pdf directly ?
Thank you for your help
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

I suggest that you call enablePrinter() when catching the EnablePre() event.

Hope this helps.
makina
Posts: 14
Joined: Fri Apr 14 2006

Post by makina »

No, unhappily it does not help
but would you have a example which would take a CDC and write to pdf ?
or do you see where I could have done a mistake in the examle I gave before ?
thanks
makina
Posts: 14
Joined: Fri Apr 14 2006

Post by makina »

event conflict resolved
Thanks
Post Reply