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 后的渲染步骤如下:
- 通过
GetPageBox读取页面区域,按目标 DPI 计算位图宽高。 - 创建
Bitmap并填充背景色。 - 调用
GetDisplayMatrix计算显示矩阵。 - 构造
OFDRender,调用StartRender开始渲染。 - 循环推进
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须在渲染完成前保持有效。