Skip to content

OFD 渲染 ​

本节介绍如何使用 OFDRender 将 OFD 页面渲染到位图。OFDRender 位于 foxit::common 命名空间,面向 OFDPage 对象;若需不区分 PDF/OFD 的统一渲染,请参阅 FLD 渲染。

能力概览 ​

能力说明
页面渲染构造绑定 Bitmap;StartRender(OFDPage, matrix) 返回 Progressive
渲染选项color_mode、back_color、fore_color、x_dpi / y_dpi、render_flags
内容标志e_RenderPage、注释/签章层等(SetRenderOption 传入)
显示矩阵left、top、width、height、Rotation
页面区域PageBoxType(如 e_PhysicalBox)用于计算位图尺寸
目标位图渲染前创建并填充背景色

类与方法细节见接口文档。

基本渲染流程 ​

从文件渲染 OFD 页面时,需先经 OFD 文件包 打开并加载文档,取得 OFDPage 后再调用 OFDRender。打开链路说明请参阅 OFD 概述。

取得 OFDPage 后的渲染步骤如下:

  1. 通过 GetPageBox 读取页面区域,按目标 DPI 计算位图宽高。
  2. 创建 Bitmap 并填充背景色。
  3. 调用 GetDisplayMatrix 计算显示矩阵。
  4. 构造 OFDRender,调用 StartRender 开始渲染。
  5. 循环推进 Progressive(C++ 为 Continue,Java 为 resume),直至返回 e_Finished 且进度为 100%;若返回 e_Error 或进度为 -1,表示渲染失败。

示例 ​

以下示例演示将第一页渲染为位图。

c
#include "fs_basictypes_c.h"
#include "fs_common_c.h"
#include "fs_render_c.h"
#include "fs_ofdpackage_c.h"
#include "fs_ofddoc_c.h"
#include "fs_ofdpage_c.h"

FS_BOOL RenderFirstOFDPage(const wchar_t* input_file) {
  FS_OFDPACKAGE_HANDLE pkg = NULL;
  FS_OFDDOC_HANDLE doc = NULL;
  FS_OFDPAGE_HANDLE page = NULL;
  FS_BITMAP_HANDLE bitmap = NULL;
  FS_OFDRENDER_HANDLE ofd_render = NULL;
  FS_PROGRESSIVE_HANDLE progressive = NULL;
  FS_BSTR password = { 0, 0 };
  FSRectF page_box;
  FSMatrix matrix;
  FSRectI fill_rect;
  int page_count = 0;
  int render_width = 0;
  int render_height = 0;
  int rate = 0;
  FS_BOOL ok = FALSE;

  if (FSDK_OFDPackage_OFDCreate1(input_file, &pkg) != e_FSErrSuccess || !pkg) {
    return FALSE;
  }
  if (FSDK_OFDPackage_OFDLoadDocument(pkg, 0, &password, &doc) != e_FSErrSuccess || !doc) {
    FSDK_OFDPackage_OFDRelease(pkg);
    return FALSE;
  }
  FSDK_OFDDoc_OFDGetPageCount(doc, &page_count);
  if (page_count <= 0 ||
      FSDK_OFDDoc_OFDGetPage(doc, 0, &page) != e_FSErrSuccess || !page) {
    if (doc) FSDK_OFDDoc_OFDRelease(doc);
    FSDK_OFDPackage_OFDRelease(pkg);
    return FALSE;
  }

  if (FSDK_OFDPage_OFDGetPageBox(page, e_FSOFDPageBoxTypePhysicalBox, &page_box) != e_FSErrSuccess) {
    FSDK_OFDPage_OFDRelease(page);
    FSDK_OFDDoc_OFDRelease(doc);
    FSDK_OFDPackage_OFDRelease(pkg);
    return FALSE;
  }
  render_width = (int)((page_box.right - page_box.left) * 96 / 72);
  render_height = (int)((page_box.bottom - page_box.top) * 96 / 72);

  if (FSDK_Bitmap_Create(render_width, render_height, e_FSDIBArgb, NULL, 0, &bitmap) != e_FSErrSuccess) {
    FSDK_OFDPage_OFDRelease(page);
    FSDK_OFDDoc_OFDRelease(doc);
    FSDK_OFDPackage_OFDRelease(pkg);
    return FALSE;
  }
  fill_rect.left = 0;
  fill_rect.top = render_height;
  fill_rect.right = render_width;
  fill_rect.bottom = 0;
  FSDK_Bitmap_FillRect(bitmap, 0xFFFFFFFF, &fill_rect);

  if (FSDK_OFDPage_OFDGetDisplayMatrix(page, 0, 0, render_width, render_height, e_FSRotation0, &matrix) != e_FSErrSuccess ||
      FSDK_OFDRender_Create(bitmap, &ofd_render) != e_FSErrSuccess ||
      FSDK_OFDRender_StartRender(ofd_render, page, matrix, NULL, &progressive) != e_FSErrSuccess ||
      !progressive) {
    if (ofd_render) FSDK_OFDRender_Release(ofd_render);
    FSDK_Bitmap_Release(bitmap);
    FSDK_OFDPage_OFDRelease(page);
    FSDK_OFDDoc_OFDRelease(doc);
    FSDK_OFDPackage_OFDRelease(pkg);
    return FALSE;
  }

  FSDK_Progressive_GetRateOfProgress(progressive, &rate);
  while (rate != 100) {
    FSState state;
    FSDK_Progressive_Continue(progressive, &state);
    FSDK_Progressive_GetRateOfProgress(progressive, &rate);
  }
  ok = (rate == 100) ? TRUE : FALSE;

  FSDK_Progressive_Release(progressive);
  FSDK_OFDRender_Release(ofd_render);
  FSDK_Bitmap_Release(bitmap);
  FSDK_OFDPage_OFDRelease(page);
  FSDK_OFDDoc_OFDRelease(doc);
  FSDK_OFDPackage_OFDRelease(pkg);
  return ok;
}
c++
#include "common/fs_render.h"
#include "ofd/fs_ofdpackage.h"
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdpage.h"

using namespace foxit;
using namespace foxit::common;
using namespace foxit::ofd;

bool RenderFirstOFDPage(const wchar_t* input_file) {
  OFDPackage package(input_file);
  if (package.IsEmpty()) {
    return false;
  }

  OFDDoc doc = package.LoadDocument(0, "");
  if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
    return false;
  }

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

  RectF page_box = page.GetPageBox(OFDPage::e_PhysicalBox);
  float page_width = page_box.right - page_box.left;
  float page_height = page_box.bottom - page_box.top;
  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);

  OFDRender render(bitmap);
  Progressive progressive = render.StartRender(page, matrix, NULL);
  int progress = progressive.GetRateOfProgress();
  if (progress < 0) {
    return false;
  }

  Progressive::State state =
      progress == 100 ? Progressive::e_Finished : Progressive::e_ToBeContinued;
  while (state == Progressive::e_ToBeContinued) {
    state = progressive.Continue();
  }
  return state == Progressive::e_Finished && progressive.GetRateOfProgress() == 100;
}
java
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.OFDRender;
import com.foxit.sdk.common.Progressive;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.ofd.OFDDoc;
import com.foxit.sdk.ofd.OFDPackage;
import com.foxit.sdk.ofd.OFDPage;

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

public class OFDRenderBasic {
    public static boolean renderFirstOFDPage(String inputFile) throws Exception {
        OFDPackage pkg = new OFDPackage(inputFile);
        if (pkg.isEmpty()) {
            return false;
        }

        OFDDoc doc = pkg.loadDocument(0, null);
        if (doc.isEmpty() || doc.getPageCount() <= 0) {
            return false;
        }

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

        RectF pageBox = page.getPageBox(OFDPage.e_PhysicalBox);
        float pageWidth = pageBox.getRight() - pageBox.getLeft();
        float pageHeight = pageBox.getBottom() - pageBox.getTop();
        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);

        OFDRender render = new OFDRender(bitmap);
        Progressive progressive = render.startRender(page, matrix, null);
        int progress = progressive.getRateOfProgress();
        if (progress < 0) {
            return false;
        }

        int state = progress == 100 ? Progressive.e_Finished : Progressive.e_ToBeContinued;
        while (state == Progressive.e_ToBeContinued) {
            state = progressive.resume();
        }
        return state == Progressive.e_Finished && progressive.getRateOfProgress() == 100;
    }
}
csharp
using System;
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.ofd;

bool RenderFirstOFDPage(string inputFile)
{
    using (OFDPackage pkg = new OFDPackage(inputFile))
    {
        if (pkg.IsEmpty())
        {
            return false;
        }

        using (OFDDoc doc = pkg.LoadDocument(0, null))
        {
            if (doc.IsEmpty() || doc.GetPageCount() <= 0)
            {
                return false;
            }

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

                RectF pageBox = page.GetPageBox(OFDPage.PageBoxType.e_PhysicalBox);
                int renderWidth = (int)((pageBox.right - pageBox.left) * 96 / 72);
                int renderHeight = (int)((pageBox.bottom - pageBox.top) * 96 / 72);

                using (Bitmap bitmap = new Bitmap(renderWidth, renderHeight, Bitmap.DIBFormat.e_DIBArgb, IntPtr.Zero, 0))
                {
                    bitmap.FillRect(0xFFFFFFFF, null);
                    Matrix2D matrix = page.GetDisplayMatrix(0, 0, renderWidth, renderHeight, Rotation.e_Rotation0);
                    using (OFDRender render = new OFDRender(bitmap))
                    using (Progressive progressive = render.StartRender(page, matrix, null))
                    {
                        Progressive.State state = Progressive.State.e_ToBeContinued;
                        while (state == Progressive.State.e_ToBeContinued)
                        {
                            state = progressive.Continue();
                        }
                        return state == Progressive.State.e_Finished && progressive.GetRateOfProgress() == 100;
                    }
                }
            }
        }
    }
}
go
package main

import "foxit.com/fsdk"

func RenderFirstOFDPage(inputFile string) bool {
	pkg := fsdk.NewOFDPackage(inputFile)
	if pkg.IsEmpty() {
		return false
	}
	defer fsdk.DeleteOFDPackage(pkg)

	doc := pkg.LoadDocument(0, "")
	if doc.IsEmpty() || doc.GetPageCount() <= 0 {
		return false
	}
	defer fsdk.DeleteOFDDoc(doc)

	page := doc.GetPage(0)
	if page.IsEmpty() {
		return false
	}
	defer fsdk.DeleteOFDPage(page)

	pageBox := page.GetPageBox(fsdk.OFDPageE_PhysicalBox)
	renderWidth := int((pageBox.GetRight() - pageBox.GetLeft()) * 96 / 72)
	renderHeight := int((pageBox.GetBottom() - pageBox.GetTop()) * 96 / 72)
	fsdk.DeleteRectF(pageBox)

	bitmap := fsdk.NewBitmap(renderWidth, renderHeight, fsdk.BitmapE_DIBArgb)
	defer fsdk.DeleteBitmap(bitmap)
	bitmap.FillRect(uint(0xFFFFFFFF))

	matrix := page.GetDisplayMatrix(0, 0, renderWidth, renderHeight, fsdk.E_Rotation0)
	defer fsdk.DeleteMatrix2D(matrix)

	render := fsdk.NewOFDRender(bitmap)
	defer fsdk.DeleteOFDRender(render)
	progressive := render.StartRender(page, matrix, nil)
	defer fsdk.DeleteProgressive(progressive)
	state := fsdk.ProgressiveE_ToBeContinued
	for state == fsdk.ProgressiveE_ToBeContinued {
		state = progressive.Continue()
	}
	return state == fsdk.ProgressiveE_Finished && progressive.GetRateOfProgress() == 100
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function renderFirstOFDPage(inputFile) {
  const pkg = new FSDK.OFDPackage(inputFile);
  if (pkg.IsEmpty()) {
    return false;
  }

  const doc = pkg.LoadDocument(0, "");
  if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
    return false;
  }

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

  const pageBox = page.GetPageBox(FSDK.OFDPage.e_PhysicalBox);
  const renderWidth = parseInt((pageBox.right - pageBox.left) * 96 / 72);
  const renderHeight = parseInt((pageBox.bottom - pageBox.top) * 96 / 72);

  const bitmap = new FSDK.Bitmap(renderWidth, renderHeight, FSDK.Bitmap.e_DIBArgb, null, 0);
  bitmap.FillRect(0xFFFFFFFF, null);
  const matrix = page.GetDisplayMatrix(0, 0, renderWidth, renderHeight, FSDK.e_Rotation0);

  const render = new FSDK.OFDRender(bitmap);
  const progressive = render.StartRender(page, matrix, null);
  let state = FSDK.Progressive.e_ToBeContinued;
  while (state == FSDK.Progressive.e_ToBeContinued) {
    state = progressive.Continue();
  }
  return state == FSDK.Progressive.e_Finished && progressive.GetRateOfProgress() == 100;
}

注意事项 ​

  • OFDRender::StartRender 返回 Progressive,需调用 Continue(C++)或 resume(Java)推进渲染;循环中应检查返回状态,仅在 e_Finished 且进度为 100% 时表示成功,e_Error 或进度 -1 表示失败。
  • FLDRender 面向 FLDPage 且为同步 Render 返回布尔值,与 OFDRender 的渐进式模型不同。
  • 创建 OFDRender 时传入的 Bitmap 须在渲染完成前保持有效。