添加表格MFC PDF

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."));
}
 

相关推荐

  1. 添加表格MFC PDF

    2024-03-28 05:40:07       16 阅读
  2. layui 表格添加 checkbox 开关

    2024-03-28 05:40:07       10 阅读
  3. 使用easyexcel对导出表格添加合计行

    2024-03-28 05:40:07       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 05:40:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 05:40:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 05:40:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 05:40:07       18 阅读

热门阅读

  1. go实现队列

    2024-03-28 05:40:07       16 阅读
  2. python提取视频中的音频

    2024-03-28 05:40:07       19 阅读
  3. 贪心算法C++

    2024-03-28 05:40:07       17 阅读
  4. 稀碎从零算法笔记Day31-LeetCode:接雨水

    2024-03-28 05:40:07       19 阅读
  5. HuggingFace: 掌握自然语言处理的利器

    2024-03-28 05:40:07       19 阅读