OFD 文档与页面
本节介绍 OFDDoc 与 OFDPage 的常用操作,包括通过文件包加载文档、获取页数、访问页面区域,以及页面与模板页的基础管理。取得 OFDDoc 的常见方式请参阅 OFD 文件包;也可通过 OFDDoc(FLDDoc) 从 FLD 统一入口转入,请参阅 FLD 文档与页面。
能力概览
| 能力 | 说明 |
|---|---|
| 文档对象 | 由 OFDPackage::LoadDocument 取得;不可按路径直接构造 |
| 页面对象 | GetPage(index)、GetPageByID;GetPageBox、GetDisplayMatrix |
| 页面管理 | 索引从 0 起;MovePage 调整页序 |
| 模板页 | 继承 OFDPage;AddTemplatePage、GetTemplatePage、 GetTemplatePageByName、RemoveTemplatePage |
| 模板应用 | 从模板新建正文页,或在已有页绑定模板(背景/正文/前景层) |
| 文档合并 | dest_index;将另一 OFDDoc 页插入当前文档 |
| 页码 | 参数为 PageNumberParameter(位置、字体、起止页等) |
| 文档版本 | 切换当前版本;CreateVersion 创建并切换到新版本(每个文档仅可创建一次) |
| 页数查询 | 增删页或插入目录页后须重新获取 |
类与方法细节见接口文档。
加载文档并访问页面
以下示例演示通过 OFDPackage 加载文档、获取第一页并读取物理页面区域。
c
#include "fs_ofdpackage_c.h"
#include "fs_ofddoc_c.h"
#include "fs_ofdpage_c.h"
void ReadFirstOFDPage(const wchar_t* input_file) {
FS_OFDPACKAGE_HANDLE pkg = NULL;
FS_OFDDOC_HANDLE doc = NULL;
FS_OFDPAGE_HANDLE page = NULL;
FS_BSTR password = { 0, 0 };
FS_BOOL empty = FALSE;
int doc_count = 0;
int page_count = 0;
FSRectF page_box;
if (FSDK_OFDPackage_OFDCreate1(input_file, &pkg) != e_FSErrSuccess || !pkg) {
return;
}
FSDK_OFDPackage_OFDIsEmpty(pkg, &empty);
FSDK_OFDPackage_OFDGetDocumentCount(pkg, &doc_count);
if (empty || doc_count <= 0) {
FSDK_OFDPackage_OFDRelease(pkg);
return;
}
if (FSDK_OFDPackage_OFDLoadDocument(pkg, 0, &password, &doc) != e_FSErrSuccess || !doc) {
FSDK_OFDPackage_OFDRelease(pkg);
return;
}
FSDK_OFDDoc_OFDIsEmpty(doc, &empty);
FSDK_OFDDoc_OFDGetPageCount(doc, &page_count);
if (empty || page_count <= 0) {
FSDK_OFDDoc_OFDRelease(doc);
FSDK_OFDPackage_OFDRelease(pkg);
return;
}
if (FSDK_OFDDoc_OFDGetPage(doc, 0, &page) == e_FSErrSuccess && page) {
FSDK_OFDPage_OFDGetPageBox(page, e_FSOFDPageBoxTypePhysicalBox, &page_box);
FSDK_OFDPage_OFDRelease(page);
}
FSDK_OFDDoc_OFDRelease(doc);
FSDK_OFDPackage_OFDRelease(pkg);
}
c++
#include "ofd/fs_ofdpackage.h"
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdpage.h"
using namespace foxit;
using namespace foxit::ofd;
void ReadFirstOFDPage(const wchar_t* input_file) {
OFDPackage package(input_file);
if (package.IsEmpty() || package.GetDocumentCount() <= 0) {
return;
}
OFDDoc doc = package.LoadDocument(0, "");
if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
return;
}
OFDPage page = doc.GetPage(0);
if (page.IsEmpty()) {
return;
}
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;
}
java
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;
public class OFDDocumentPage {
public static void readFirstOFDPage(String inputFile) throws Exception {
OFDPackage pkg = new OFDPackage(inputFile);
if (pkg.isEmpty() || pkg.getDocumentCount() <= 0) {
return;
}
OFDDoc doc = pkg.loadDocument(0, null);
if (doc.isEmpty() || doc.getPageCount() <= 0) {
return;
}
OFDPage page = doc.getPage(0);
if (page.isEmpty()) {
return;
}
RectF pageBox = page.getPageBox(OFDPage.e_PhysicalBox);
float pageWidth = pageBox.getRight() - pageBox.getLeft();
float pageHeight = pageBox.getBottom() - pageBox.getTop();
}
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
void ReadFirstOFDPage(string inputFile)
{
using (OFDPackage pkg = new OFDPackage(inputFile))
{
if (pkg.IsEmpty() || pkg.GetDocumentCount() <= 0)
{
return;
}
using (OFDDoc doc = pkg.LoadDocument(0, null))
{
if (doc.IsEmpty() || doc.GetPageCount() <= 0)
{
return;
}
using (OFDPage page = doc.GetPage(0))
{
if (page.IsEmpty())
{
return;
}
RectF pageBox = page.GetPageBox(OFDPage.PageBoxType.e_PhysicalBox);
float pageWidth = pageBox.right - pageBox.left;
float pageHeight = pageBox.bottom - pageBox.top;
}
}
}
}
python
from FoxitPDFSDKPython3 import *
def read_first_ofd_page(input_file):
pkg = OFDPackage(input_file)
if pkg.IsEmpty() or pkg.GetDocumentCount() <= 0:
return
doc = pkg.LoadDocument(0, "")
if doc is None or doc.IsEmpty() or doc.GetPageCount() <= 0:
return
page = doc.GetPage(0)
if page.IsEmpty():
return
page_box = page.GetPageBox(OFDPage.e_PhysicalBox)
page_width = page_box.right - page_box.left
page_height = page_box.bottom - page_box.top
go
package main
import "foxit.com/fsdk"
func ReadFirstOFDPage(inputFile string) {
pkg := fsdk.NewOFDPackage(inputFile)
defer fsdk.DeleteOFDPackage(pkg)
if pkg.IsEmpty() || pkg.GetDocumentCount() <= 0 {
return
}
doc := pkg.LoadDocument(0, "")
defer fsdk.DeleteOFDDoc(doc)
if doc.IsEmpty() || doc.GetPageCount() <= 0 {
return
}
page := doc.GetPage(0)
defer fsdk.DeleteOFDPage(page)
if page.IsEmpty() {
return
}
pageBox := page.GetPageBox(fsdk.OFDPageE_PhysicalBox)
_ = pageBox.GetRight() - pageBox.GetLeft()
_ = pageBox.GetBottom() - pageBox.GetTop()
fsdk.DeleteRectF(pageBox)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function readFirstOFDPage(inputFile) {
const pkg = new FSDK.OFDPackage(inputFile);
if (pkg.IsEmpty() || pkg.GetDocumentCount() <= 0) {
return;
}
const doc = pkg.LoadDocument(0, "");
if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
return;
}
const page = doc.GetPage(0);
if (page.IsEmpty()) {
return;
}
const pageBox = page.GetPageBox(FSDK.OFDPage.e_PhysicalBox);
const pageWidth = pageBox.right - pageBox.left;
const pageHeight = pageBox.bottom - pageBox.top;
}
创建模板页并应用到正文
OFDTemplatePage 继承自 OFDPage,用于定义可复用的页面版式。调用 OFDDoc::AddTemplatePage 会在文档中 创建模板页并加入文档的模板页列表(可通过 GetTemplatePageCount 确认)。典型流程如下:
- 创建模板页:
OFDDoc::AddTemplatePage返回OFDTemplatePage。 - 设置名称:
OFDTemplatePage::SetName设置模板页名称,便于后续通过GetTemplatePageByName查找。 - 编辑模板内容:在模板页上设置页面区域、图层、图元等;
OFDTemplatePage复用OFDPage的页面编辑接口(如SetPageBox)。 - 应用到正文:将模板用于正文页,常见方式包括:
OFDDoc::AddPageFromTemplatePage:按模板页索引 新建 正文页;OFDPage::SetTemplatePage:在 已有 正文页上按图层(背景 / 正文 / 前景)绑定模板页。
以下示例演示在空白文档中创建模板页、设置名称与页面区域,并基于该模板页创建正文页。示例假定已通过 OFDPackage::AddBlankDocument 取得 OFDDoc;若从已有 OFD 文件加载,请先使用 OFD 文件包 取得 OFDDoc 后再执行相同步骤。
c++
#include "ofd/fs_ofdpackage.h"
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdpage.h"
using namespace foxit;
using namespace foxit::ofd;
bool CreateTemplateAndApplyToBody() {
OFDPackage package;
if (package.IsEmpty()) {
return false;
}
OFDDoc doc = package.AddBlankDocument();
if (doc.IsEmpty()) {
return false;
}
// 1. 创建模板页(加入文档模板页列表)
OFDTemplatePage template_page = doc.AddTemplatePage();
if (template_page.IsEmpty()) {
return false;
}
// 2. 设置名称
if (!template_page.SetName(L"BodyTemplate")) {
return false;
}
// 3. 编辑模板内容(设置物理页面区域)
RectF page_box(0, 200, 200, 0);
if (!template_page.SetPageBox(OFDPage::e_PhysicalBox, page_box)) {
return false;
}
// 4. 应用到正文:基于模板页索引创建正文页
int template_index = doc.GetTemplatePageCount() - 1;
OFDPage body_page = doc.AddPageFromTemplatePage(template_index, 0);
if (body_page.IsEmpty()) {
return false;
}
return body_page.SetPageBox(OFDPage::e_PhysicalBox, page_box);
}
java
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 com.foxit.sdk.ofd.OFDTemplatePage;
public class OFDTemplatePageExample {
public static boolean createTemplateAndApplyToBody() throws Exception {
OFDPackage pkg = new OFDPackage();
if (pkg.isEmpty()) {
return false;
}
OFDDoc doc = pkg.addBlankDocument();
if (doc.isEmpty()) {
return false;
}
// 1. 创建模板页(加入文档模板页列表)
OFDTemplatePage templatePage = doc.addTemplatePage();
if (templatePage.isEmpty()) {
return false;
}
// 2. 设置名称
if (!templatePage.setName("BodyTemplate")) {
return false;
}
// 3. 编辑模板内容(设置物理页面区域)
RectF pageBox = new RectF(0, 200, 200, 0);
if (!templatePage.setPageBox(OFDPage.e_PhysicalBox, pageBox)) {
return false;
}
// 4. 应用到正文:基于模板页索引创建正文页
int templateIndex = doc.getTemplatePageCount() - 1;
OFDPage bodyPage = doc.addPageFromTemplatePage(templateIndex, 0);
if (bodyPage.isEmpty()) {
return false;
}
return bodyPage.setPageBox(OFDPage.e_PhysicalBox, pageBox);
}
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
bool CreateTemplateAndApplyToBody()
{
using (OFDPackage pkg = new OFDPackage())
{
if (pkg.IsEmpty())
{
return false;
}
using (OFDDoc doc = pkg.AddBlankDocument())
{
if (doc.IsEmpty())
{
return false;
}
using (OFDTemplatePage templatePage = doc.AddTemplatePage())
{
if (templatePage.IsEmpty())
{
return false;
}
if (!templatePage.SetName("BodyTemplate"))
{
return false;
}
RectF pageBox = new RectF(0, 200, 200, 0);
if (!templatePage.SetPageBox(OFDPage.PageBoxType.e_PhysicalBox, pageBox))
{
return false;
}
int templateIndex = doc.GetTemplatePageCount() - 1;
using (OFDPage bodyPage = doc.AddPageFromTemplatePage(templateIndex, 0))
{
if (bodyPage.IsEmpty())
{
return false;
}
return bodyPage.SetPageBox(OFDPage.PageBoxType.e_PhysicalBox, pageBox);
}
}
}
}
}
python
from FoxitPDFSDKPython3 import *
def create_template_and_apply_to_body():
pkg = OFDPackage()
if pkg.IsEmpty():
return False
doc = pkg.AddBlankDocument()
if doc.IsEmpty():
return False
template_page = doc.AddTemplatePage()
if template_page.IsEmpty():
return False
if not template_page.SetName("BodyTemplate"):
return False
page_box = RectF(0, 200, 200, 0)
if not template_page.SetPageBox(OFDPage.e_PhysicalBox, page_box):
return False
template_index = doc.GetTemplatePageCount() - 1
body_page = doc.AddPageFromTemplatePage(template_index, 0)
if body_page.IsEmpty():
return False
return body_page.SetPageBox(OFDPage.e_PhysicalBox, page_box)
go
package main
import "foxit.com/fsdk"
func CreateTemplateAndApplyToBody() bool {
pkg := fsdk.NewOFDPackage()
defer fsdk.DeleteOFDPackage(pkg)
if pkg.IsEmpty() {
return false
}
doc := pkg.AddBlankDocument()
defer fsdk.DeleteOFDDoc(doc)
if doc.IsEmpty() {
return false
}
templatePage := doc.AddTemplatePage()
defer fsdk.DeleteOFDTemplatePage(templatePage)
if templatePage.IsEmpty() {
return false
}
if !templatePage.SetName("BodyTemplate") {
return false
}
pageBox := fsdk.NewRectF(0, 200, 200, 0)
defer fsdk.DeleteRectF(pageBox)
if !templatePage.SetPageBox(fsdk.OFDPageE_PhysicalBox, pageBox) {
return false
}
templateIndex := doc.GetTemplatePageCount() - 1
bodyPage := doc.AddPageFromTemplatePage(templateIndex, 0)
defer fsdk.DeleteOFDPage(bodyPage)
if bodyPage.IsEmpty() {
return false
}
return bodyPage.SetPageBox(fsdk.OFDPageE_PhysicalBox, pageBox)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function createTemplateAndApplyToBody() {
const pkg = new FSDK.OFDPackage();
if (pkg.IsEmpty()) {
return false;
}
const doc = pkg.AddBlankDocument();
if (doc.IsEmpty()) {
return false;
}
const templatePage = doc.AddTemplatePage();
if (templatePage.IsEmpty()) {
return false;
}
if (!templatePage.SetName("BodyTemplate")) {
return false;
}
const pageBox = new FSDK.RectF(0, 200, 200, 0);
if (!templatePage.SetPageBox(FSDK.OFDPage.e_PhysicalBox, pageBox)) {
return false;
}
const templateIndex = doc.GetTemplatePageCount() - 1;
const bodyPage = doc.AddPageFromTemplatePage(templateIndex, 0);
if (bodyPage.IsEmpty()) {
return false;
}
return bodyPage.SetPageBox(FSDK.OFDPage.e_PhysicalBox, pageBox);
}
若正文页已存在,可将模板页绑定到指定图层,例如:
c++
body_page.SetTemplatePage(template_page, foxit::ofd::OFDLayer::e_LayerTypeBody);
java
bodyPage.setTemplatePage(templatePage, com.foxit.sdk.ofd.OFDLayer.e_LayerTypeBody);
添加页码
PageNumberParameter 封装页码位置(PageNumberLocationType)、字体、字号、颜色、起止页索引、是否跳过空白页等选项。调用 OFDDoc::AddPageNumber 后页码写入文档内容,须 SaveAs 持久化。
c++
#include "ofd/fs_ofddoc.h"
using namespace foxit;
using namespace foxit::ofd;
void AddDocumentPageNumbers(OFDDoc& doc) {
if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
return;
}
int last_page = doc.GetPageCount() - 1;
PageNumberParameter param(
PageNumberParameter::e_PageNumberLocationTypeBottomMiddle,
L"宋体", 12, RGB(0, 0, 0),
0, last_page,
false, 1, false,
PageNumberParameter::e_PageNumberMarginTypeNormal,
false);
doc.AddPageNumber(param);
}
java
import com.foxit.sdk.common.RGB;
import com.foxit.sdk.ofd.OFDDoc;
import com.foxit.sdk.ofd.PageNumberParameter;
public class OFDPageNumberExample {
public static void addDocumentPageNumbers(OFDDoc doc) throws Exception {
if (doc.isEmpty() || doc.getPageCount() <= 0) {
return;
}
int lastPage = doc.getPageCount() - 1;
PageNumberParameter param = new PageNumberParameter(
PageNumberParameter.e_PageNumberLocationTypeBottomMiddle,
"宋体", 12, new RGB(0, 0, 0),
0, lastPage,
false, 1, false,
PageNumberParameter.e_PageNumberMarginTypeNormal,
false);
doc.addPageNumber(param);
}
}
csharp
using foxit.common;
using foxit.ofd;
void AddDocumentPageNumbers(OFDDoc doc)
{
if (doc.IsEmpty() || doc.GetPageCount() <= 0)
{
return;
}
int lastPage = doc.GetPageCount() - 1;
PageNumberParameter param = new PageNumberParameter(
PageNumberParameter.PageNumberLocationType.e_PageNumberLocationTypeBottomMiddle,
"宋体", 12, new RGB(0, 0, 0),
0, lastPage,
false, 1, false,
PageNumberParameter.PageNumberMarginType.e_PageNumberMarginTypeNormal,
false);
doc.AddPageNumber(param);
}
python
from FoxitPDFSDKPython3 import *
def add_document_page_numbers(doc):
if doc.IsEmpty() or doc.GetPageCount() <= 0:
return
last_page = doc.GetPageCount() - 1
param = PageNumberParameter(
PageNumberParameter.e_PageNumberLocationTypeBottomMiddle,
"宋体", 12, RGB(0, 0, 0),
0, last_page,
False, 1, False,
PageNumberParameter.e_PageNumberMarginTypeNormal,
False)
doc.AddPageNumber(param)
go
package main
import "foxit.com/fsdk"
func AddDocumentPageNumbers(doc fsdk.OFDDoc) {
if doc.IsEmpty() || doc.GetPageCount() <= 0 {
return
}
lastPage := doc.GetPageCount() - 1
color := fsdk.NewRGB(0, 0, 0)
defer fsdk.DeleteRGB(color)
param := fsdk.NewOFDPageNumberParameter(
fsdk.OFDPageNumberParameterE_PageNumberLocationTypeBottomMiddle,
"宋体", 12, color,
0, lastPage,
false, 1, false,
fsdk.OFDPageNumberParameterE_PageNumberMarginTypeNormal,
false)
defer fsdk.DeleteOFDPageNumberParameter(param)
doc.AddPageNumber(param)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function addDocumentPageNumbers(doc) {
if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
return;
}
const lastPage = doc.GetPageCount() - 1;
const param = new FSDK.OFDPageNumberParameter(
FSDK.OFDPageNumberParameter.e_PageNumberLocationTypeBottomMiddle,
"宋体", 12, new FSDK.RGB(0, 0, 0),
0, lastPage,
false, 1, false,
FSDK.OFDPageNumberParameter.e_PageNumberMarginTypeNormal,
false);
doc.AddPageNumber(param);
}
文档版本
OFD 文档可含多个版本快照。GetVersionCount 返回版本数量;SetCurrentVersion(index) 切换当前编辑/读取的版本;CreateVersion 基于当前状态创建新版本并自动切换。每个文档仅可调用一次 CreateVersion。
c++
#include "ofd/fs_ofddoc.h"
using namespace foxit::ofd;
void ManageDocumentVersions(OFDDoc& doc) {
if (doc.IsEmpty()) {
return;
}
int version_count = doc.GetVersionCount();
if (version_count > 0) {
doc.SetCurrentVersion(0);
}
// 每个文档仅可创建一次新版本
doc.CreateVersion();
version_count = doc.GetVersionCount();
for (int i = 0; i < version_count; ++i) {
doc.SetCurrentVersion(i);
}
}
java
import com.foxit.sdk.ofd.OFDDoc;
public class OFDDocumentVersionExample {
public static void manageDocumentVersions(OFDDoc doc) throws Exception {
if (doc.isEmpty()) {
return;
}
int versionCount = doc.getVersionCount();
if (versionCount > 0) {
doc.setCurrentVersion(0);
}
doc.createVersion();
versionCount = doc.getVersionCount();
for (int i = 0; i < versionCount; i++) {
doc.setCurrentVersion(i);
}
}
}
csharp
using foxit.ofd;
void ManageDocumentVersions(OFDDoc doc)
{
if (doc.IsEmpty())
{
return;
}
int versionCount = doc.GetVersionCount();
if (versionCount > 0)
{
doc.SetCurrentVersion(0);
}
doc.CreateVersion();
versionCount = doc.GetVersionCount();
for (int i = 0; i < versionCount; ++i)
{
doc.SetCurrentVersion(i);
}
}
python
from FoxitPDFSDKPython3 import *
def manage_document_versions(doc):
if doc.IsEmpty():
return
version_count = doc.GetVersionCount()
if version_count > 0:
doc.SetCurrentVersion(0)
doc.CreateVersion()
version_count = doc.GetVersionCount()
for i in range(version_count):
doc.SetCurrentVersion(i)
go
package main
import "foxit.com/fsdk"
func ManageDocumentVersions(doc fsdk.OFDDoc) {
if doc.IsEmpty() {
return
}
versionCount := doc.GetVersionCount()
if versionCount > 0 {
doc.SetCurrentVersion(0)
}
doc.CreateVersion()
versionCount = doc.GetVersionCount()
for i := 0; i < versionCount; i++ {
doc.SetCurrentVersion(i)
}
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function manageDocumentVersions(doc) {
if (doc.IsEmpty()) {
return;
}
let versionCount = doc.GetVersionCount();
if (versionCount > 0) {
doc.SetCurrentVersion(0);
}
doc.CreateVersion();
versionCount = doc.GetVersionCount();
for (let i = 0; i < versionCount; i++) {
doc.SetCurrentVersion(i);
}
}
注意事项
- 页面索引从
0开始,有效范围为0至GetPageCount() - 1。 - 页码范围:
PageNumberParameter的start_page/end_page为页面索引,有效范围为0至GetPageCount() - 1。 - 文档版本:
SetCurrentVersion的index须在0至GetVersionCount() - 1范围内,否则抛出参数异常;CreateVersion每个文档仅可调用一次。 GetPageBox、GetDisplayMatrix等几何相关接口用于渲染与版面计算,详见 OFD 渲染。- 附件、书签、大纲、元数据等文档级能力在
OFDDoc上提供,请参阅对应专题章节。 - 修改文档内容后需通过
OFDPackage::Save或SaveAs写回文件。