Skip to content

PDF 转图片 ​

本节说明如何在精简版中打开 PDF、渲染页面并保存为图片。完整可运行工程见发布包:

examples/simple_demo/pdf2image/

工程接入与初始化见 快速开始。

流程 ​

  1. 用 PDFDoc 打开 PDF(路径或内存缓冲区等),调用 Load。
  2. GetPage 取得页面,StartParse 解析页面。
  3. 按页面宽高(及目标缩放)创建并填充 Bitmap。
  4. GetDisplayMatrix 计算显示矩阵,Renderer::StartRender 渲染。
  5. Image::AddFrame + SaveAs 写出图片;多页 TIFF 可向同一 Image 多次 AddFrame。

API 概览 ​

功能C++ API说明
打开文档foxit::pdf::PDFDoc路径、内存或 ReaderCallback
加载PDFDoc::Load / LoadW可选密码
页面PDFDoc::GetPage / GetPageCount取得 PDFPage
解析PDFPage::StartParse渲染前需解析
尺寸 / 旋转GetWidth、GetHeight、GetRotation计算位图与矩阵
显示矩阵PDFPage::GetDisplayMatrix目标区域与旋转
渲染foxit::common::Renderer绑定 Bitmap(Windows 亦可绑定 HDC)后 StartRender
存图Image::AddFrame / SaveAs扩展名决定格式

支持的图片格式 ​

pdf2image 示例中,单页可分别保存为:

.bmp、.jpg / .jpeg、.png、.jpx、.jp2

多页可合并保存为:

.tif / .tiff

实际以 Image::SaveAs 与目标扩展名支持为准。更多说明可参考全功能库 图片转换(仅适用精简版已提供的接口)。

基本示例 ​

将 PDF 每一页保存为 PNG:

c++
#include "common/fs_common.h"
#include "common/fs_image.h"
#include "common/fs_render.h"
#include "pdf/fs_pdfdoc.h"
#include "pdf/fs_pdfpage.h"

using namespace foxit;
using namespace foxit::common;
using namespace foxit::pdf;

bool SavePdfPagesAsPng(const wchar_t* input_pdf, const wchar_t* output_dir) {
  PDFDoc doc(input_pdf);
  ErrorCode error_code = doc.Load();
  if (error_code != e_ErrSuccess) {
    return false;
  }

  int page_count = doc.GetPageCount();
  for (int i = 0; i < page_count; i++) {
    PDFPage page = doc.GetPage(i);
    if (page.IsEmpty()) {
      return false;
    }

    Progressive parse_progressive =
        page.StartParse(PDFPage::e_ParsePageNormal, NULL, false);
    int parse_progress = parse_progressive.GetRateOfProgress();
    if (parse_progress < 0) {
      return false;
    }
    Progressive::State parse_state =
        parse_progress == 100 ? Progressive::e_Finished : Progressive::e_ToBeContinued;
    while (parse_state == Progressive::e_ToBeContinued) {
      parse_state = parse_progressive.Continue();
    }
    if (parse_state != Progressive::e_Finished ||
        parse_progressive.GetRateOfProgress() != 100) {
      return false;
    }

    int width = static_cast<int>(page.GetWidth());
    int height = static_cast<int>(page.GetHeight());
    Matrix matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

    Bitmap bitmap(width, height, Bitmap::e_DIBArgb, NULL, 0);
    bitmap.FillRect(0xFFFFFFFF, NULL);

    Renderer render(bitmap, false);
    Progressive render_progressive = render.StartRender(page, matrix, NULL);
    int render_progress = render_progressive.GetRateOfProgress();
    if (render_progress < 0) {
      return false;
    }
    Progressive::State render_state =
        render_progress == 100 ? Progressive::e_Finished : Progressive::e_ToBeContinued;
    while (render_state == Progressive::e_ToBeContinued) {
      render_state = render_progressive.Continue();
    }
    if (render_state != Progressive::e_Finished ||
        render_progressive.GetRateOfProgress() != 100) {
      return false;
    }

    Image image;
    image.AddFrame(bitmap);

    WString file_name;
    file_name.Format(L"page_%d.png", i);
    WString output_file = WString(output_dir) + L"/" + file_name;

    if (!image.SaveAs(output_file)) {
      return false;
    }
  }
  return true;
}

NOTE

  • 示例在保存图片前检查页面是否有效,并按 Continue() 返回的 Progressive::State(e_ToBeContinued / e_Finished / 其他终止态)推进解析与渲染;完成条件为 e_Finished 且进度为 100%。

  • 调用前须已成功执行 Library::Initialize。需要更高清晰度时,按目标 DPI 放大 width / height(及对应 GetDisplayMatrix 参数),做法与 OFD 转图片 中高 DPI 示例类似。

常用扩展 ​

随包 pdf2image 示例还演示了:

场景做法
多格式导出同一 Bitmap 多次 SaveAs 不同扩展名
多页 TIFF各页渲染后向同一 Image AddFrame,再保存 .tif / .tiff
从内存打开 PDFPDFDoc(buffer, size) 后 Load
Windows HDC 渲染Renderer(HDC) + StartRender(仅 Windows;远程桌面等环境未验证)

页面渲染选项细节可参考全功能库 PDF 渲染;表单填充、注释编辑等完整 PDF 能力不属于精简版。

相关示例 ​

路径说明
examples/simple_demo/pdf2image/pdf2image.cpp文件/内存加载、多格式出图、多页 TIFF;Windows 另含 HDC 示例
  • Windows:用 simple_demo_mini_vs*.sln 打开并运行 pdf2image 工程。
  • Linux:在 examples/simple_demo/ 下执行 ./RunDemo.sh pdf2image(HDC 相关代码仅 Windows 编译)。

详见 快速开始。