void AddImage(HPDF_Doc pdf, HPDF_Page page, const CString& imagePath)
{
HPDF_Image image = HPDF_LoadPngImageFromFile(pdf, CStringA(imagePath));
if (!image)
{
AfxMessageBox(_T("Error: Cannot load image."));
return;
}
HPDF_REAL width = HPDF_Image_GetWidth(image);
HPDF_REAL height = HPDF_Image_GetHeight(image);
HPDF_Page_DrawImage(page, image, 50, 500, width, height);
}
void CMainViewWnd::ExportToPDF(const CString& filePath)
{
HPDF_Doc pdf = HPDF_New(NULL, NULL);
if (!pdf)
{
AfxMessageBox(_T("Error: Cannot create PdfDoc object."));
return;
}
if (HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL) != HPDF_OK)
{
AfxMessageBox(_T("Error: Cannot set compression mode."));
HPDF_Free(pdf);
return;
}
HPDF_Page page = HPDF_AddPage(pdf);
if (!page)
{
AfxMessageBox(_T("Error: Cannot create PdfPage object."));
HPDF_Free(pdf);
return;
}
CString imagePath = _T("C:\\path\\to\\image.png");
AddImage(pdf, page, imagePath);
if (HPDF_SaveToFile(pdf, CStringA(filePath)) != HPDF_OK)
{
AfxMessageBox(_T("Error: Cannot save to file."));
HPDF_Free(pdf);
return;
}
HPDF_Free(pdf);
AfxMessageBox(_T("Exported to PDF successfully."));
}