Skip to content

FLD 渲染

本节介绍如何使用 FLDRender 将 PDF/OFD 固定版式页面统一渲染到位图。通过 FLDDoc 加载文档并取得 FLDPage 后,无需预先区分输入格式即可调用 FLDRender::Render 完成出图。

任务场景

  • FLDDoc::LoadFLDPage::Parse 取得页面后,统一渲染 PDF 或 OFD 来源的固定版式页。
  • 使用 FLDRender::RenderFLDPage 绘制到 Bitmap,并导出为 PNG 等图片文件。
  • 通过 FLDRenderOption 配置 color_mode、前景/背景色、x_dpi / y_dpiis_print 等参数。

API 概览

功能C++ API(foxit::common / foxit::fld核心参数 / 描述
页面渲染FLDRender构造绑定 BitmapRender(FLDPage, matrix) 同步完成
渲染选项FLDRenderOptioncolor_modeback_colorfore_colorx_dpi / y_dpiis_print
显示矩阵FLDPage::GetDisplayMatrixlefttopwidthheightRotation
页面尺寸FLDPage::GetWidthGetHeight单位 1/72 英寸;须先 Parse
目标位图Bitmap按 DPI 计算尺寸后创建并填充背景色

基本渲染流程

典型流程为:加载 FLDDoc → 获取并解析 FLDPage → 按目标 DPI 计算位图尺寸 → 创建 Bitmap 并填充背景 → 计算显示矩阵 → 调用 FLDRender::Render 完成渲染。

  1. 通过 GetWidthGetHeight 读取页面尺寸,按目标 DPI 计算位图宽高。
  2. 创建 Bitmap 并填充背景色。
  3. 调用 GetDisplayMatrix 计算显示矩阵。
  4. 构造 FLDRender 并调用 Render 同步完成渲染。

以下示例演示将文档第一页渲染为位图并保存为图片文件的完整流程:

c++
#include "common/fs_image.h"
#include "common/fs_render.h"
#include "fld/fs_flddoc.h"
#include "fld/fs_fldpage.h"

using namespace foxit;
using namespace foxit::common;
using namespace foxit::fld;

bool RenderFLDPageToBitmap(const wchar_t* input_file, const wchar_t* output_file) {
  FLDDoc doc(input_file);
  if (doc.IsEmpty()) {
    return false;
  }

  ErrorCode error_code = doc.Load("");
  if (error_code != e_ErrSuccess || doc.GetPageCount() <= 0) {
    return false;
  }

  FLDPage page = doc.GetPage(0);
  if (page.IsEmpty()) {
    return false;
  }

  error_code = page.Parse();
  if (error_code != e_ErrSuccess) {
    return false;
  }

  float page_width = page.GetWidth();
  float page_height = page.GetHeight();
  int render_width = (int)(page_width * 96 / 72);
  int render_height = (int)(page_height * 96 / 72);

  Bitmap bitmap(render_width, render_height, Bitmap::e_DIBArgb);
  bitmap.FillRect(0xFFFFFFFF, NULL);

  Matrix matrix = page.GetDisplayMatrix(0, 0, render_width, render_height, e_Rotation0);

  FLDRender render(bitmap);
  if (!render.Render(page, matrix)) {
    return false;
  }

  Image image;
  image.AddFrame(bitmap);
  image.SaveAs(output_file);
  return true;
}
java
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.FLDRender;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.fld.FLDDoc;
import com.foxit.sdk.fld.FLDPage;

import static com.foxit.sdk.common.Constants.*;

public class FLDRenderBasic {
    public static boolean renderFLDPageToBitmap(String inputFile, String outputFile) throws PDFException {
        FLDDoc doc = new FLDDoc(inputFile);
        if (doc.isEmpty()) {
            return false;
        }

        int errorCode = doc.load(null);
        if (errorCode != e_ErrSuccess || doc.getPageCount() <= 0) {
            return false;
        }

        FLDPage page = doc.getPage(0);
        if (page.isEmpty()) {
            return false;
        }

        errorCode = page.parse();
        if (errorCode != e_ErrSuccess) {
            return false;
        }

        float pageWidth = page.getWidth();
        float pageHeight = page.getHeight();
        int renderWidth = (int) (pageWidth * 96 / 72);
        int renderHeight = (int) (pageHeight * 96 / 72);

        Bitmap bitmap = new Bitmap(renderWidth, renderHeight, Bitmap.e_DIBArgb, null, 0);
        bitmap.fillRect(0xFFFFFFFFL, null);

        Matrix2D matrix = page.getDisplayMatrix(0, 0, renderWidth, renderHeight, e_Rotation0);

        FLDRender render = new FLDRender(bitmap);
        if (!render.render(page, matrix)) {
            return false;
        }

        Image image = new Image();
        image.addFrame(bitmap);
        image.saveAs(outputFile);
        return true;
    }
}

配置渲染选项

通过 FLDRenderOption 可设置颜色模式、前景/背景色、DPI 和打印模式。设置后调用 SetRenderOption 应用到 FLDRender 实例。

以下示例演示在已完成页面解析并创建 Bitmap 的前提下,配置灰度映射与 300 DPI 后执行渲染(片段代码,省略文档加载与页面解析步骤):

c++
FLDRenderOption render_option;
render_option.color_mode = FLDRenderOption::e_ColorModeMappingGray;
render_option.back_color = 0xFF000000;
render_option.fore_color = 0xFFFFFFFF;
render_option.x_dpi = 300;
render_option.y_dpi = 300;

FLDRender render(bitmap);
render.SetRenderOption(render_option);
bool result = render.Render(page, matrix);
java
FLDRenderOption renderOption = new FLDRenderOption();
renderOption.setColor_mode(FLDRenderOption.e_ColorModeMappingGray);
renderOption.setBack_color(0xFF000000L);
renderOption.setFore_color(0xFFFFFFFFL);
renderOption.setX_dpi(300);
renderOption.setY_dpi(300);

FLDRender render = new FLDRender(bitmap);
render.setRenderOption(renderOption);
boolean result = render.render(page, matrix);

注意事项

  • FLDRender::Render 为同步调用,直接返回布尔值表示渲染是否成功;这与 OFDRender::StartRender 的渐进式(progressive)渲染模式不同。
  • FLDRender 面向 FLDPage;若已明确文档为 OFD 且只需 OFD 渲染能力,也可使用 OFD 模块中的 OFDRender
  • 创建 FLDRender 时传入的 Bitmap 须在渲染完成前保持有效。
  • 提高 DPI 会增大位图尺寸和内存占用,批量渲染时应合理评估资源消耗。