void AddTable(HPDF_Doc pdf, HPDF_Page page)
{
HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", NULL);
if (!font)
{
AfxMessageBox(_T("Error: Cannot get Font."));
return;
}
HPDF_Page_SetFontAndSize(page, font, 12);
HPDF_REAL rowHeight = 20;
HPDF_REAL colWidth = 100;
HPDF_REAL x = 50;
HPDF_REAL y = 500;
int numRows = 5;
int numCols = 3;
for (int i = 0; i <= numRows; i++)
{
HPDF_Page_MoveTo(page, x, y - i * rowHeight);
HPDF_Page_LineTo(page, x + numCols * colWidth, y - i * rowHeight);
HPDF_Page_Stroke(page);
}
for (int i = 0; i <= numCols; i++)
{
HPDF_Page_MoveTo(page, x + i * colWidth, y);
HPDF_Page_LineTo(page, x + i * colWidth, y - numRows * rowHeight);
HPDF_Page_Stroke(page);
}
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
CString text;
text.Format(_T("Cell %d,%d"), i + 1, j + 1);
HPDF_Page_BeginText(page);
HPDF_Page_MoveTextPos(page, x + j * colWidth + 5, y - (i + 1) * rowHeight + 5);
HPDF_Page_ShowText(page, CStringA(text));
HPDF_Page_EndText(page);
}
}
}
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;
}
AddTable(pdf, page);
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."));
}