OFD 图元与图层
本节介绍页面内容流中的图元对象(foxit::ofd::graphics::OFDGraphicsObject 及其子类型)与图层(OFDLayer)的遍历与编辑,用于版式分析、内容抽取或二次编辑。取得 OFDPage 的方式请参阅 OFD 文档与页面。
能力概览
| 能力 | 说明 |
|---|---|
| 页面图层 | 按索引获取页面图层;图层类型含背景 / 正文 / 前景(OFDLayer::e_LayerTypeBackground 等) |
| 图层与图元容器 | 继承自 OFDGraphicsObject;AddPathObject、AddTextObject、AddImageObject 等;GetGraphicsObjectCount、GetGraphicsObject、RemoveGraphicsObject |
| 图元基类 | GetType、GetID、GetBoundary、GetMatrix、GetAlpha;颜色、线宽、虚线;CreateActions / GetActions |
| 路径图元 | SetFill / SetStroke、SetFillMode;OFDPathData(MoveTo、LineTo、CubicBezierTo 等)或 SetAbbreviatedData |
| 文本图元 | 字体、字号、阅读/字符方向;GetTextLayout、SetCharInfos;CharInfo |
| 图像图元 | SetImage(resource_id) 或从文件/缓冲区加载;GetImageBuffer、GetImageFormat |
| 视频图元 | SetVideoFromFile、SetType、SetFormat、SetTitle;视频边框(圆角、虚线等) |
| 复合图元 | AddUnit 添加 OFDCompositeUnit;SetThumbnail、SetSubstitution |
| 块对象 | 继承自 OFDLayer,作为嵌套图元容器(e_ObjectTypePageBlock) |
类与方法细节见接口文档。
遍历与访问页面图元
典型流程:从 OFDPage 获取图层 → 遍历图层内图元 → 根据 GetType 转为具体子类型。GetBoundary 与 GetMatrix 均基于 OFD 页面坐标系,实际渲染位置由二者共同决定。
以下示例演示遍历首页图层与图元。
c
#include "fs_ofdpackage_c.h"
#include "fs_ofddoc_c.h"
#include "fs_ofdpage_c.h"
#include "fs_ofdlayer_c.h"
#include "fs_ofdgraphicsobject_c.h"
#include "fs_ofdtextobject_c.h"
#include "fs_ofdpathobject_c.h"
#include "fs_ofdimageobject_c.h"
void TraversePageGraphics(const wchar_t* input_file) {
FS_OFDPACKAGE_HANDLE pkg = NULL;
FS_OFDDOC_HANDLE doc = NULL;
FS_OFDPAGE_HANDLE page = NULL;
FS_BOOL empty = FALSE;
FS_BSTR password = { 0, 0 };
int layer_count = 0;
int i = 0;
if (FSDK_OFDPackage_OFDCreate1(input_file, &pkg) != e_FSErrSuccess || !pkg) {
return;
}
if (FSDK_OFDPackage_OFDLoadDocument(pkg, 0, &password, &doc) != e_FSErrSuccess || !doc) {
FSDK_OFDPackage_OFDRelease(pkg);
return;
}
FSDK_OFDDoc_OFDGetPage(doc, 0, &page);
if (!page) {
FSDK_OFDDoc_OFDRelease(doc);
FSDK_OFDPackage_OFDRelease(pkg);
return;
}
FSDK_OFDPage_OFDGetLayerCount(page, &layer_count);
for (i = 0; i < layer_count; ++i) {
FS_OFDLAYER_HANDLE layer = NULL;
int object_count = 0;
int j = 0;
FSDK_OFDPage_OFDGetLayer(page, i, &layer);
if (!layer) {
continue;
}
FSDK_OFDLayer_OFDGetGraphicsObjectCount(layer, &object_count);
for (j = 0; j < object_count; ++j) {
FS_OFDGRAPHICSOBJECT_HANDLE obj = NULL;
FSOFDGraphicsObjectType type = e_FSOFDGraphicsObjectTypeGraphicsObjectTypeUnknown;
FSRectF boundary;
FSDK_OFDLayer_OFDGetGraphicsObjectType(layer, j, &type);
FSDK_OFDLayer_OFDGetGraphicsObject(layer, j, &obj);
if (!obj) {
continue;
}
memset(&boundary, 0, sizeof(boundary));
FSDK_OFDGraphicsObject_OFDGetBoundary(obj, &boundary);
if (type == e_FSOFDGraphicsObjectTypeGraphicsObjectTypeText) {
FS_OFDTEXTOBJECT_HANDLE text_obj = NULL;
FSDK_OFDTextObject_OFDCreate(obj, &text_obj);
} else if (type == e_FSOFDGraphicsObjectTypeGraphicsObjectTypePath) {
FS_OFDPATHOBJECT_HANDLE path_obj = NULL;
FSDK_OFDPathObject_OFDCreate(obj, &path_obj);
} else if (type == e_FSOFDGraphicsObjectTypeGraphicsObjectTypeImage) {
FS_OFDIMAGEOBJECT_HANDLE image_obj = NULL;
FSDK_OFDImageObject_OFDCreate(obj, &image_obj);
}
}
}
FSDK_OFDDoc_OFDRelease(doc);
FSDK_OFDPackage_OFDRelease(pkg);
}
c++
#include "ofd/fs_ofdpackage.h"
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdpage.h"
#include "ofd/fs_ofdlayer.h"
#include "ofd/graphics/fs_ofdgraphicsobject.h"
#include "ofd/graphics/fs_ofdpathobject.h"
#include "ofd/graphics/fs_ofdtextobject.h"
#include "ofd/graphics/fs_ofdimageobject.h"
using namespace foxit;
using namespace foxit::ofd;
using namespace foxit::ofd::graphics;
void TraversePageGraphics(const wchar_t* input_file) {
OFDPackage package(input_file);
if (package.IsEmpty() || package.GetDocumentCount() <= 0) {
return;
}
OFDDoc doc = package.LoadDocument(0, L"");
if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
return;
}
OFDPage page = doc.GetPage(0);
if (page.IsEmpty()) {
return;
}
// 遍历页面上的所有图层
int layer_count = page.GetLayerCount();
for (int i = 0; i < layer_count; ++i) {
OFDLayer layer = page.GetLayer(i);
if (layer.IsEmpty()) {
continue;
}
// 遍历图层内的图元
int object_count = layer.GetGraphicsObjectCount();
for (int j = 0; j < object_count; ++j) {
OFDGraphicsObject obj = layer.GetGraphicsObject(j);
if (obj.IsEmpty()) {
continue;
}
ObjectType type = obj.GetType();
RectF boundary = obj.GetBoundary();
Matrix matrix = obj.GetMatrix();
switch (type) {
case e_ObjectTypeImage: {
OFDImageObject image_obj(obj);
// 读取图像格式与缓冲区
break;
}
case e_ObjectTypeText: {
OFDTextObject text_obj(obj);
// 读取字体、字号、字符信息等
break;
}
case e_ObjectTypePath: {
OFDPathObject path_obj(obj);
// 读取路径数据或缩写路径
break;
}
case e_ObjectTypePageBlock: {
OFDBlockObject block_obj(obj);
// 块对象本身也是图层容器,可继续递归遍历
break;
}
default:
break;
}
}
}
}
java
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.OFDLayer;
import com.foxit.sdk.ofd.OFDPackage;
import com.foxit.sdk.ofd.OFDPage;
import com.foxit.sdk.ofd.graphics.OFDBlockObject;
import com.foxit.sdk.ofd.graphics.OFDGraphicsObject;
import com.foxit.sdk.ofd.graphics.OFDImageObject;
import com.foxit.sdk.ofd.graphics.OFDPathObject;
import com.foxit.sdk.ofd.graphics.OFDTextObject;
public class OFDGraphicsTraverseExample {
public static void traversePageGraphics(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;
}
int layerCount = page.getLayerCount();
for (int i = 0; i < layerCount; i++) {
OFDLayer layer = page.getLayer(i);
if (layer.isEmpty()) {
continue;
}
int objectCount = layer.getGraphicsObjectCount();
for (int j = 0; j < objectCount; j++) {
OFDGraphicsObject obj = layer.getGraphicsObject(j);
if (obj.isEmpty()) {
continue;
}
int type = obj.getType();
RectF boundary = obj.getBoundary();
Matrix2D matrix = obj.getMatrix();
if (type == OFDGraphicsObject.e_ObjectTypeImage) {
OFDImageObject imageObj = new OFDImageObject(obj);
} else if (type == OFDGraphicsObject.e_ObjectTypeText) {
OFDTextObject textObj = new OFDTextObject(obj);
} else if (type == OFDGraphicsObject.e_ObjectTypePath) {
OFDPathObject pathObj = new OFDPathObject(obj);
} else if (type == OFDGraphicsObject.e_ObjectTypePageBlock) {
OFDBlockObject blockObj = new OFDBlockObject(obj);
}
}
}
}
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
using foxit.ofd.graphics;
void TraversePageGraphics(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;
}
int layerCount = page.GetLayerCount();
for (int i = 0; i < layerCount; ++i)
{
using (OFDLayer layer = page.GetLayer(i))
{
if (layer.IsEmpty())
{
continue;
}
int objectCount = layer.GetGraphicsObjectCount();
for (int j = 0; j < objectCount; ++j)
{
using (OFDGraphicsObject obj = layer.GetGraphicsObject(j))
{
if (obj.IsEmpty())
{
continue;
}
OFDLayer.GraphicsObjectType type = layer.GetGraphicsObjectType(j);
RectF boundary = obj.GetBoundary();
Matrix2D matrix = obj.GetMatrix();
if (type == OFDLayer.GraphicsObjectType.e_GraphicsObjectTypeText)
{
using (OFDTextObject textObj = new OFDTextObject(obj)) { }
}
else if (type == OFDLayer.GraphicsObjectType.e_GraphicsObjectTypePath)
{
using (OFDPathObject pathObj = new OFDPathObject(obj)) { }
}
else if (type == OFDLayer.GraphicsObjectType.e_GraphicsObjectTypeImage)
{
using (OFDImageObject imageObj = new OFDImageObject(obj)) { }
}
}
}
}
}
}
}
}
}
python
from FoxitPDFSDKPython3 import *
def traverse_page_graphics(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
for i in range(page.GetLayerCount()):
layer = page.GetLayer(i)
if layer.IsEmpty():
continue
for j in range(layer.GetGraphicsObjectCount()):
obj_type = layer.GetGraphicsObjectType(j)
obj = layer.GetGraphicsObject(j)
if obj.IsEmpty():
continue
boundary = obj.GetBoundary()
if obj_type == OFDLayer.e_GraphicsObjectTypeText:
text_obj = OFDTextObject(obj)
elif obj_type == OFDLayer.e_GraphicsObjectTypePath:
path_obj = OFDPathObject(obj)
elif obj_type == OFDLayer.e_GraphicsObjectTypeImage:
image_obj = OFDImageObject(obj)
go
package main
import "foxit.com/fsdk"
func TraversePageGraphics(inputFile string) {
pkg := fsdk.NewOFDPackage(inputFile)
if pkg.IsEmpty() || pkg.GetDocumentCount() <= 0 {
return
}
defer fsdk.DeleteOFDPackage(pkg)
doc := pkg.LoadDocument(0, "")
if doc.IsEmpty() || doc.GetPageCount() <= 0 {
return
}
page := doc.GetPage(0)
if page.IsEmpty() {
return
}
layerCount := page.GetLayerCount()
for i := 0; i < layerCount; i++ {
layer := page.GetLayer(i)
if layer.IsEmpty() {
continue
}
objCount := layer.GetGraphicsObjectCount()
for j := 0; j < objCount; j++ {
objType := layer.GetGraphicsObjectType(j)
obj := layer.GetGraphicsObject(j)
if obj.IsEmpty() {
continue
}
boundary := obj.GetBoundary()
fsdk.DeleteRectF(boundary)
if objType == int(fsdk.OFDLayerE_GraphicsObjectTypeText) {
textObj := fsdk.NewOFDTextObject(obj)
fsdk.DeleteOFDTextObject(textObj)
} else if objType == int(fsdk.OFDLayerE_GraphicsObjectTypePath) {
pathObj := fsdk.NewOFDPathObject(obj)
fsdk.DeleteOFDPathObject(pathObj)
} else if objType == int(fsdk.OFDLayerE_GraphicsObjectTypeImage) {
imgObj := fsdk.NewOFDImageObject(obj)
fsdk.DeleteOFDImageObject(imgObj)
}
}
}
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function traversePageGraphics(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 layerCount = page.GetLayerCount();
for (let i = 0; i < layerCount; i++) {
const layer = page.GetLayer(i);
if (layer.IsEmpty()) {
continue;
}
const objectCount = layer.GetGraphicsObjectCount();
for (let j = 0; j < objectCount; j++) {
const objType = layer.GetGraphicsObjectType(j);
const obj = layer.GetGraphicsObject(j);
if (obj.IsEmpty()) {
continue;
}
const boundary = obj.GetBoundary();
if (objType === FSDK.OFDLayer.e_GraphicsObjectTypeText) {
const textObj = new FSDK.OFDTextObject(obj);
} else if (objType === FSDK.OFDLayer.e_GraphicsObjectTypePath) {
const pathObj = new FSDK.OFDPathObject(obj);
} else if (objType === FSDK.OFDLayer.e_GraphicsObjectTypeImage) {
const imageObj = new FSDK.OFDImageObject(obj);
}
}
}
}
创建与编辑图元
通过 OFDLayer::AddPathObject、AddTextObject、AddImageObject 等工厂方法创建图元,设置边界框、颜色、线宽等通用属性;不需要的图元可调用 RemoveGraphicsObject 或 RemoveAllGraphicsObjects 删除。修改后须 OFDPackage::Save / SaveAs 写回文件。
c
#include "fs_ofdlayer_c.h"
#include "fs_ofdpathobject_c.h"
#include "fs_ofdtextobject_c.h"
#include "fs_ofdimageobject_c.h"
void AddGraphicsObjects(FS_OFDLAYER_HANDLE layer, FS_UINT32 image_res_id) {
FS_BOOL empty = FALSE;
FS_OFDPATHOBJECT_HANDLE path_obj = NULL;
FS_OFDTEXTOBJECT_HANDLE text_obj = NULL;
FS_OFDIMAGEOBJECT_HANDLE img_obj = NULL;
FSRectF boundary;
FSDK_OFDLayer_OFDIsEmpty(layer, &empty);
if (empty) {
return;
}
FSDK_OFDLayer_OFDAddPathObject(layer, &path_obj);
if (path_obj) {
FSDK_OFDPathObject_OFDSetStroke(path_obj, TRUE);
FSDK_OFDPathObject_OFDSetLineWidth(path_obj, 0.5f);
FSDK_OFDPathObject_OFDSetStrokeColor(path_obj, 0xFF0000);
}
FSDK_OFDLayer_OFDAddTextObject(layer, &text_obj);
if (text_obj) {
FSDK_OFDTextObject_OFDSetFontName(text_obj, L"SimSun");
FSDK_OFDTextObject_OFDSetFontSize(text_obj, 12.0f);
memset(&boundary, 0, sizeof(boundary));
boundary.left = 10.0f;
boundary.bottom = 10.0f;
boundary.right = 200.0f;
boundary.top = 200.0f;
FSDK_OFDTextObject_OFDSetBoundary(text_obj, boundary);
FSDK_OFDTextObject_OFDInsertText(text_obj, "Hello OFD", 0.0f, 12.0f, TRUE);
}
FSDK_OFDLayer_OFDAddImageObject(layer, &img_obj);
if (img_obj) {
FSDK_OFDImageObject_OFDSetImage(img_obj, image_res_id);
}
}
c++
#include "ofd/fs_ofdlayer.h"
#include "ofd/graphics/fs_ofdpathobject.h"
#include "ofd/graphics/fs_ofdtextobject.h"
#include "ofd/graphics/fs_ofdimageobject.h"
using namespace foxit;
using namespace foxit::ofd;
using namespace foxit::ofd::graphics;
void AddGraphicsObjects(OFDLayer& layer, uint32 image_res_id) {
if (layer.IsEmpty()) {
return;
}
// 添加路径图元并设置描边样式
OFDPathObject path_obj = layer.AddPathObject();
if (!path_obj.IsEmpty()) {
path_obj.SetStroke(true);
path_obj.SetLineWidth(0.5f);
path_obj.SetStrokeColor(RGB(255, 0, 0));
}
// 添加文本图元,通过 TextLayout 设置内容与排版
OFDTextObject text_obj = layer.AddTextObject();
if (!text_obj.IsEmpty()) {
text_obj.SetFontName(L"SimSun");
text_obj.SetFontSize(12);
text_obj.SetBoundary(RectF(10, 10, 200, 200));
text_obj.SetFillColor(RGB(0, 255, 0));
TextLayout text_layout = text_obj.GetTextLayout();
if (!text_layout.IsEmpty()) {
text_layout.SetText(L"Hello OFD");
text_layout.SetFontName(L"SimSun");
text_layout.SetFontSize(12);
text_layout.Update();
}
}
// 添加图像图元,image_res_id 由 OFDDoc::AddImageResourceFromFile 等接口获得
OFDImageObject img_obj = layer.AddImageObject();
if (!img_obj.IsEmpty()) {
img_obj.SetImage(image_res_id);
img_obj.SetBoundary(RectF(100, 100, 200, 200));
img_obj.SetVisible(false);
}
}
java
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.ofd.OFDLayer;
import com.foxit.sdk.ofd.graphics.OFDImageObject;
import com.foxit.sdk.ofd.graphics.OFDPathObject;
import com.foxit.sdk.ofd.graphics.OFDTextObject;
import com.foxit.sdk.ofd.graphics.TextLayout;
public class OFDGraphicsEditExample {
public static void addGraphicsObjects(OFDLayer layer, int imageResId) throws Exception {
if (layer.isEmpty()) {
return;
}
OFDPathObject pathObj = layer.addPathObject();
if (!pathObj.isEmpty()) {
pathObj.setStroke(true);
pathObj.setLineWidth(0.5f);
pathObj.setStrokeColor(0xFF0000);
}
OFDTextObject textObj = layer.addTextObject();
if (!textObj.isEmpty()) {
textObj.setFontName("SimSun");
textObj.setFontSize(12);
textObj.setBoundary(new RectF(10, 10, 200, 200));
textObj.setFillColor(0x00FF00);
TextLayout textLayout = textObj.getTextLayout();
if (!textLayout.isEmpty()) {
textLayout.setText("Hello OFD");
textLayout.setFontName("SimSun");
textLayout.setFontSize(12);
textLayout.update();
}
}
OFDImageObject imgObj = layer.addImageObject();
if (!imgObj.isEmpty()) {
imgObj.setImage(imageResId);
imgObj.setBoundary(new RectF(100, 100, 200, 200));
imgObj.setVisible(false);
}
}
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
using foxit.ofd.graphics;
void AddGraphicsObjects(OFDLayer layer, uint imageResId)
{
if (layer.IsEmpty())
{
return;
}
using (OFDPathObject pathObj = layer.AddPathObject())
{
if (!pathObj.IsEmpty())
{
pathObj.SetStroke(true);
pathObj.SetLineWidth(0.5f);
pathObj.SetStrokeColor(0xFF0000);
}
}
using (OFDTextObject textObj = layer.AddTextObject())
{
if (!textObj.IsEmpty())
{
textObj.SetFontName("SimSun");
textObj.SetFontSize(12);
textObj.SetBoundary(new RectF(10, 10, 200, 200));
textObj.SetFillColor(0x00FF00);
textObj.InsertText("Hello OFD", 0.0f, 12.0f);
}
}
using (OFDImageObject imgObj = layer.AddImageObject())
{
if (!imgObj.IsEmpty())
{
imgObj.SetImage(imageResId);
imgObj.SetBoundary(new RectF(100, 100, 200, 200));
imgObj.SetVisible(false);
}
}
}
python
from FoxitPDFSDKPython3 import *
def add_graphics_objects(layer, image_res_id):
if layer.IsEmpty():
return
path_obj = layer.AddPathObject()
if not path_obj.IsEmpty():
path_obj.SetStroke(True)
path_obj.SetLineWidth(0.5)
path_obj.SetStrokeColor(0xFF0000)
text_obj = layer.AddTextObject()
if not text_obj.IsEmpty():
text_obj.SetFontName("SimSun")
text_obj.SetFontSize(12.0)
text_obj.SetBoundary(RectF(10, 10, 200, 200))
text_obj.InsertText("Hello OFD", 0.0, 12.0)
img_obj = layer.AddImageObject()
if not img_obj.IsEmpty():
img_obj.SetImage(image_res_id)
img_obj.SetBoundary(RectF(100, 100, 200, 200))
img_obj.SetVisible(False)
go
package main
import "foxit.com/fsdk"
func AddGraphicsObjects(layer fsdk.OFDLayer, imageResID uint) {
if layer.IsEmpty() {
return
}
pathObj := layer.AddPathObject()
if !pathObj.IsEmpty() {
pathObj.SetStroke(true)
pathObj.SetLineWidth(0.5)
pathObj.SetStrokeColor(0xFF0000)
}
textObj := layer.AddTextObject()
if !textObj.IsEmpty() {
textObj.SetFontName("SimSun")
textObj.SetFontSize(12)
boundary := fsdk.NewRectF(10, 10, 200, 200)
textObj.SetBoundary(boundary)
fsdk.DeleteRectF(boundary)
textObj.InsertText("Hello OFD", 0.0, 12.0)
}
imgObj := layer.AddImageObject()
if !imgObj.IsEmpty() {
imgObj.SetImage(imageResID)
}
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function addGraphicsObjects(layer, imageResId) {
if (layer.IsEmpty()) {
return;
}
const pathObj = layer.AddPathObject();
if (!pathObj.IsEmpty()) {
pathObj.SetStroke(true);
pathObj.SetLineWidth(0.5);
pathObj.SetStrokeColor(0xFF0000);
}
const textObj = layer.AddTextObject();
if (!textObj.IsEmpty()) {
textObj.SetFontName("SimSun");
textObj.SetFontSize(12);
textObj.SetBoundary(new FSDK.RectF(10, 10, 200, 200));
textObj.InsertText("Hello OFD", 0.0, 12.0);
}
const imgObj = layer.AddImageObject();
if (!imgObj.IsEmpty()) {
imgObj.SetImage(imageResId);
imgObj.SetBoundary(new FSDK.RectF(100, 100, 200, 200));
imgObj.SetVisible(false);
}
}
矢量路径绘制
路径可使用 OFDPathData 逐点构建(StartFigure、MoveTo、LineTo、CubicBezierTo、ArcTo、CloseFigure),也可通过 SetAbbreviatedData 设置 OFD 缩写路径字符串以减小文件体积。填充规则支持 e_PathFillNonZero、e_PathFillEvenOdd 等。
c
#include "fs_ofdlayer_c.h"
#include "fs_ofdpathobject_c.h"
void DrawRectanglePath(FS_OFDLAYER_HANDLE layer) {
FS_OFDPATHOBJECT_HANDLE path_obj = NULL;
FSRectF boundary;
FSDK_OFDLayer_OFDAddPathObject(layer, &path_obj);
if (!path_obj) {
return;
}
memset(&boundary, 0, sizeof(boundary));
boundary.left = 20.0f;
boundary.bottom = 25.0f;
boundary.right = 45.0f;
boundary.top = 95.0f;
FSDK_OFDPathObject_OFDSetBoundary(path_obj, boundary);
FSDK_OFDPathObject_OFDSetFill(path_obj, TRUE);
FSDK_OFDPathObject_OFDSetStroke(path_obj, TRUE);
FSDK_OFDPathObject_OFDSetFillMode(path_obj, e_FSOFDPathFillModePathFillNonZero);
FSDK_OFDPathObject_OFDSetFillColor(path_obj, 0xFFFFC8);
FSDK_OFDPathObject_OFDSetStrokeColor(path_obj, 0x28323C);
FSDK_OFDPathObject_OFDSetLineWidth(path_obj, 0.5f);
FSDK_OFDPathObject_OFDSetAbbreviatedData(path_obj, "M 0 0 L 20 0 L 20 20 L 0 20 B");
}
c++
#include "ofd/graphics/fs_ofdpathobject.h"
using namespace foxit;
using namespace foxit::ofd::graphics;
void DrawRectanglePath(OFDLayer& layer) {
OFDPathObject path_obj = layer.AddPathObject();
if (path_obj.IsEmpty()) {
return;
}
path_obj.SetBoundary(RectF(20.0f, 95.0f, 45.0f, 25.0f));
path_obj.SetFill(true);
path_obj.SetStroke(true);
path_obj.SetFillMode(OFDPathObject::e_PathFillNonZero);
path_obj.SetFillColor(RGB(255, 255, 200));
path_obj.SetStrokeColor(RGB(40, 50, 60));
path_obj.SetLineWidth(0.5f);
// 方式一:缩写路径(适合简单矩形等)
path_obj.SetAbbreviatedData("M 0 0 L 20 0 L 20 20 L 0 20 B");
// 方式二:OFDPathData 逐点构建(适合复杂曲线)
// OFDPathData path_data;
// path_data.StartFigure(0, 0);
// path_data.LineTo(20, 0);
// path_data.LineTo(20, 20);
// path_data.CloseFigure();
// path_obj.SetPathData(path_data);
}
java
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.ofd.OFDLayer;
import com.foxit.sdk.ofd.graphics.OFDPathObject;
public class OFDPathDrawExample {
public static void drawRectanglePath(OFDLayer layer) throws Exception {
OFDPathObject pathObj = layer.addPathObject();
if (pathObj.isEmpty()) {
return;
}
pathObj.setBoundary(new RectF(20.0f, 95.0f, 45.0f, 25.0f));
pathObj.setFill(true);
pathObj.setStroke(true);
pathObj.setFillMode(OFDPathObject.e_PathFillNonZero);
pathObj.setFillColor(0x00FFFFC8);
pathObj.setStrokeColor(0x0028323C);
pathObj.setLineWidth(0.5f);
pathObj.setAbbreviatedData("M 0 0 L 20 0 L 20 20 L 0 20 B");
}
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
using foxit.ofd.graphics;
void DrawRectanglePath(OFDLayer layer)
{
using (OFDPathObject pathObj = layer.AddPathObject())
{
if (pathObj.IsEmpty())
{
return;
}
pathObj.SetBoundary(new RectF(20.0f, 95.0f, 45.0f, 25.0f));
pathObj.SetFill(true);
pathObj.SetStroke(true);
pathObj.SetFillMode(OFDPathObject.e_PathFillNonZero);
pathObj.SetFillColor(0x00FFFFC8);
pathObj.SetStrokeColor(0x0028323C);
pathObj.SetLineWidth(0.5f);
pathObj.SetAbbreviatedData("M 0 0 L 20 0 L 20 20 L 0 20 B");
}
}
python
from FoxitPDFSDKPython3 import *
def draw_rectangle_path(layer):
path_obj = layer.AddPathObject()
if path_obj.IsEmpty():
return
path_obj.SetBoundary(RectF(20.0, 95.0, 45.0, 25.0))
path_obj.SetFill(True)
path_obj.SetStroke(True)
path_obj.SetFillMode(OFDPathObject.e_PathFillNonZero)
path_obj.SetFillColor(0x00FFFFC8)
path_obj.SetStrokeColor(0x0028323C)
path_obj.SetLineWidth(0.5)
path_obj.SetAbbreviatedData("M 0 0 L 20 0 L 20 20 L 0 20 B")
go
package main
import "foxit.com/fsdk"
func DrawRectanglePath(layer fsdk.OFDLayer) {
pathObj := layer.AddPathObject()
if pathObj.IsEmpty() {
return
}
boundary := fsdk.NewRectF(20.0, 95.0, 45.0, 25.0)
defer fsdk.DeleteRectF(boundary)
pathObj.SetBoundary(boundary)
pathObj.SetFill(true)
pathObj.SetStroke(true)
pathObj.SetFillMode(fsdk.OFDPathObjectE_PathFillNonZero)
pathObj.SetFillColor(0x00FFFFC8)
pathObj.SetStrokeColor(0x0028323C)
pathObj.SetLineWidth(0.5)
pathObj.SetAbbreviatedData("M 0 0 L 20 0 L 20 20 L 0 20 B")
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function drawRectanglePath(layer) {
const pathObj = layer.AddPathObject();
if (pathObj.IsEmpty()) {
return;
}
pathObj.SetBoundary(new FSDK.RectF(20.0, 95.0, 45.0, 25.0));
pathObj.SetFill(true);
pathObj.SetStroke(true);
pathObj.SetFillMode(FSDK.OFDPathObject.e_PathFillNonZero);
pathObj.SetFillColor(0x00FFFFC8);
pathObj.SetStrokeColor(0x0028323C);
pathObj.SetLineWidth(0.5);
pathObj.SetAbbreviatedData("M 0 0 L 20 0 L 20 20 L 0 20 B");
}
文本排版
OFDTextObject 支持 SetReadDirection、SetCharDirection 等阅读方向设置。复杂排版可通过 GetTextLayout 取得 TextLayout,设置文本内容、字号、对齐与行距后调用 Update 生效;字符级精确定位可使用 CharInfo 与 SetCharInfos。
c
#include "fs_ofdtextobject_c.h"
void ConfigureTextLayout(FS_OFDTEXTOBJECT_HANDLE text_obj) {
FS_BOOL empty = FALSE;
FS_OFDTEXTLAYOUT_HANDLE layout = NULL;
FSDK_OFDTextObject_OFDIsEmpty(text_obj, &empty);
if (empty) {
return;
}
FSDK_OFDTextObject_OFDSetFontName(text_obj, L"SimSun");
FSDK_OFDTextObject_OFDSetFontSize(text_obj, 12.0f);
FSDK_OFDTextObject_OFDSetReadDirection(text_obj, e_FSOFDReadDirectionReadDirectionLeftToRight);
FSDK_OFDTextObject_OFDGetTextLayout(text_obj, &layout);
if (!layout) {
return;
}
FSDK_OFDTextLayout_OFDSetText(layout, L"多行文本示例");
FSDK_OFDTextLayout_OFDSetFontName(layout, L"SimSun");
FSDK_OFDTextLayout_OFDSetFontSize(layout, 12.0f);
FSDK_OFDTextLayout_OFDUpdate(layout);
}
c++
#include "ofd/graphics/fs_ofdtextobject.h"
using namespace foxit::ofd::graphics;
void ConfigureTextLayout(OFDTextObject& text_obj) {
if (text_obj.IsEmpty()) {
return;
}
text_obj.SetFontName(L"SimSun");
text_obj.SetFontSize(12);
text_obj.SetReadDirection(OFDTextObject::e_ReadDirectionLeftToRight);
TextLayout layout = text_obj.GetTextLayout();
if (layout.IsEmpty()) {
return;
}
layout.SetText(L"多行文本示例");
layout.SetFontName(L"SimSun");
layout.SetFontSize(12);
// 可继续设置水平/垂直对齐、行间距、自动换行等(参见 TextLayout 接口文档)
layout.Update();
}
java
import com.foxit.sdk.ofd.graphics.OFDTextObject;
import com.foxit.sdk.ofd.graphics.TextLayout;
public class OFDTextLayoutExample {
public static void configureTextLayout(OFDTextObject textObj) throws Exception {
if (textObj.isEmpty()) {
return;
}
textObj.setFontName("SimSun");
textObj.setFontSize(12);
textObj.setReadDirection(OFDTextObject.e_ReadDirectionLeftToRight);
TextLayout layout = textObj.getTextLayout();
if (layout.isEmpty()) {
return;
}
layout.setText("多行文本示例");
layout.setFontName("SimSun");
layout.setFontSize(12);
layout.update();
}
}
csharp
using foxit.ofd.graphics;
void ConfigureTextLayout(OFDTextObject textObj)
{
if (textObj.IsEmpty())
{
return;
}
textObj.SetFontName("SimSun");
textObj.SetFontSize(12);
textObj.SetReadDirection(OFDTextObject.e_ReadDirectionLeftToRight);
using (TextLayout layout = textObj.GetTextLayout())
{
if (layout.IsEmpty())
{
return;
}
layout.SetText("多行文本示例");
layout.SetFontName("SimSun");
layout.SetFontSize(12);
layout.Update();
}
}
python
from FoxitPDFSDKPython3 import *
def configure_text_layout(text_obj):
if text_obj.IsEmpty():
return
text_obj.SetFontName("SimSun")
text_obj.SetFontSize(12)
text_obj.SetReadDirection(OFDTextObject.e_ReadDirectionLeftToRight)
layout = text_obj.GetTextLayout()
if layout.IsEmpty():
return
layout.SetText("多行文本示例")
layout.SetFontName("SimSun")
layout.SetFontSize(12)
layout.Update()
go
package main
import "foxit.com/fsdk"
func ConfigureTextLayout(textObj fsdk.OFDTextObject) {
if textObj.IsEmpty() {
return
}
textObj.SetFontName("SimSun")
textObj.SetFontSize(12)
textObj.SetReadDirection(fsdk.OFDTextObjectE_ReadDirectionLeftToRight)
layout := textObj.GetTextLayout()
if layout.IsEmpty() {
return
}
defer fsdk.DeleteOFDTextLayout(layout)
layout.SetText("多行文本示例")
layout.SetFontName("SimSun")
layout.SetFontSize(12)
layout.Update()
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function configureTextLayout(textObj) {
if (textObj.IsEmpty()) {
return;
}
textObj.SetFontName("SimSun");
textObj.SetFontSize(12);
textObj.SetReadDirection(FSDK.OFDTextObject.e_ReadDirectionLeftToRight);
const layout = textObj.GetTextLayout();
if (layout.IsEmpty()) {
return;
}
layout.SetText("多行文本示例");
layout.SetFontName("SimSun");
layout.SetFontSize(12);
layout.Update();
}
多媒体与复合内容
视频图元通过 OFDLayer::AddVideoObject 或 AddGraphicsObject(e_GraphicsObjectTypeVideo) 创建,使用 SetVideoFromFile 指定视频源,并可配置标题、格式与边框样式。复合对象通过 AddCompositeObject 创建,使用 AddUnit 添加子单元,并通过 SetThumbnail、SetSubstitution 设置缩略图与替代内容(资源 ID 须先加入文档资源库,请参阅 元数据、资源与自定义数据)。
c
#include "fs_ofdlayer_c.h"
#include "fs_ofdcompositeobject_c.h"
#include "fs_ofdvideoobject_c.h"
void AddCompositeWithThumbnail(FS_OFDLAYER_HANDLE layer, FS_UINT32 thumb_res_id) {
FS_OFDCOMPOSITEOBJECT_HANDLE composite_obj = NULL;
FS_OFDCOMPOSITEUNIT_HANDLE unit = NULL;
FSDK_OFDLayer_OFDAddCompositeObject(layer, &composite_obj);
if (!composite_obj) {
return;
}
FSDK_OFDCompositeObject_OFDAddUnit(composite_obj, &unit);
if (unit) {
FSDK_OFDCompositeUnit_OFDSetWidth(unit, 100.0f);
FSDK_OFDCompositeUnit_OFDSetHeight(unit, 80.0f);
FSDK_OFDCompositeUnit_OFDSetThumbnail(unit, thumb_res_id);
FSDK_OFDCompositeUnit_OFDSetSubstitution(unit, thumb_res_id);
}
}
void ConfigureVideoObject(FS_OFDLAYER_HANDLE layer, const char* video_path) {
FS_OFDGRAPHICSOBJECT_HANDLE base_obj = NULL;
FS_OFDVIDEOOBJECT_HANDLE video_obj = NULL;
FSDK_OFDLayer_OFDAddGraphicsObject(layer, e_FSOFDGraphicsObjectTypeGraphicsObjectTypeVideo, &base_obj);
FSDK_OFDVideoObject_OFDCreate(base_obj, &video_obj);
if (!video_obj) {
return;
}
FSDK_OFDVideoObject_OFDSetType(video_obj, "mp4");
FSDK_OFDVideoObject_OFDSetFormat(video_obj, "video/mp4");
FSDK_OFDVideoObject_OFDSetTitle(video_obj, "demo");
FSDK_OFDVideoObject_OFDSetVideoFromFile(video_obj, video_path);
}
c++
#include "ofd/fs_ofdlayer.h"
#include "ofd/graphics/fs_ofdcompositeobject.h"
#include "ofd/graphics/fs_ofdvideoobject.h"
using namespace foxit::ofd;
using namespace foxit::ofd::graphics;
void AddCompositeWithThumbnail(OFDLayer& layer, uint32 thumb_res_id) {
OFDCompositeObject composite_obj = layer.AddCompositeObject();
if (composite_obj.IsEmpty()) {
return;
}
OFDCompositeUnit unit = composite_obj.AddUnit();
if (!unit.IsEmpty()) {
unit.SetWidth(100.0f);
unit.SetHeight(80.0f);
unit.SetThumbnail(thumb_res_id);
unit.SetSubstitution(thumb_res_id);
}
}
void ConfigureVideoObject(OFDLayer& layer, const char* video_path) {
OFDGraphicsObject base_obj = layer.AddGraphicsObject(
OFDLayer::e_GraphicsObjectTypeVideo);
OFDVideoObject video_obj(base_obj);
if (video_obj.IsEmpty()) {
return;
}
video_obj.SetType("mp4");
video_obj.SetFormat("video/mp4");
video_obj.SetTitle("demo");
video_obj.SetVideoFromFile(video_path);
// 可选:配置视频边框圆角与虚线
video_obj.CreateBorder();
video_obj.SetBorderLineWidth(1.0f);
video_obj.SetBorderCornerRadius(1.0f, 2.0f);
}
java
import com.foxit.sdk.ofd.OFDLayer;
import com.foxit.sdk.ofd.graphics.OFDCompositeObject;
import com.foxit.sdk.ofd.graphics.OFDCompositeUnit;
import com.foxit.sdk.ofd.graphics.OFDGraphicsObject;
import com.foxit.sdk.ofd.graphics.OFDVideoObject;
public class OFDMediaExample {
public static void addCompositeWithThumbnail(OFDLayer layer, int thumbResId)
throws Exception {
OFDCompositeObject compositeObj = layer.addCompositeObject();
if (compositeObj.isEmpty()) {
return;
}
OFDCompositeUnit unit = compositeObj.addUnit();
if (!unit.isEmpty()) {
unit.setWidth(100.0f);
unit.setHeight(80.0f);
unit.setThumbnail(thumbResId);
unit.setSubstitution(thumbResId);
}
}
public static void configureVideoObject(OFDLayer layer, String videoPath)
throws Exception {
OFDGraphicsObject baseObj = layer.addGraphicsObject(
OFDLayer.e_GraphicsObjectTypeVideo);
OFDVideoObject videoObj = new OFDVideoObject(baseObj);
if (videoObj.isEmpty()) {
return;
}
videoObj.setType("mp4");
videoObj.setFormat("video/mp4");
videoObj.setTitle("demo");
videoObj.setVideoFromFile(videoPath);
videoObj.createBorder();
videoObj.setBorderLineWidth(1.0f);
videoObj.setBorderCornerRadius(1.0f, 2.0f);
}
}
csharp
using foxit.ofd;
using foxit.ofd.graphics;
void AddCompositeWithThumbnail(OFDLayer layer, uint thumbResId)
{
using (OFDCompositeObject compositeObj = layer.AddCompositeObject())
{
if (compositeObj.IsEmpty())
{
return;
}
using (OFDCompositeUnit unit = compositeObj.AddUnit())
{
if (!unit.IsEmpty())
{
unit.SetWidth(100.0f);
unit.SetHeight(80.0f);
unit.SetThumbnail(thumbResId);
unit.SetSubstitution(thumbResId);
}
}
}
}
void ConfigureVideoObject(OFDLayer layer, string videoPath)
{
using (OFDGraphicsObject baseObj = layer.AddGraphicsObject(OFDLayer.e_GraphicsObjectTypeVideo))
using (OFDVideoObject videoObj = new OFDVideoObject(baseObj))
{
if (videoObj.IsEmpty())
{
return;
}
videoObj.SetType("mp4");
videoObj.SetFormat("video/mp4");
videoObj.SetTitle("demo");
videoObj.SetVideoFromFile(videoPath);
videoObj.CreateBorder();
videoObj.SetBorderLineWidth(1.0f);
videoObj.SetBorderCornerRadius(1.0f, 2.0f);
}
}
python
from FoxitPDFSDKPython3 import *
def add_composite_with_thumbnail(layer, thumb_res_id):
composite_obj = layer.AddCompositeObject()
if composite_obj.IsEmpty():
return
unit = composite_obj.AddUnit()
if not unit.IsEmpty():
unit.SetWidth(100.0)
unit.SetHeight(80.0)
unit.SetThumbnail(thumb_res_id)
unit.SetSubstitution(thumb_res_id)
def configure_video_object(layer, video_path):
base_obj = layer.AddGraphicsObject(OFDLayer.e_GraphicsObjectTypeVideo)
video_obj = OFDVideoObject(base_obj)
if video_obj.IsEmpty():
return
video_obj.SetType("mp4")
video_obj.SetFormat("video/mp4")
video_obj.SetTitle("demo")
video_obj.SetVideoFromFile(video_path)
go
package main
import "foxit.com/fsdk"
func AddCompositeWithThumbnail(layer fsdk.OFDLayer, thumbResID uint) {
compositeObj := layer.AddCompositeObject()
if compositeObj.IsEmpty() {
return
}
unit := compositeObj.AddUnit()
if !unit.IsEmpty() {
unit.SetWidth(100.0)
unit.SetHeight(80.0)
unit.SetThumbnail(thumbResID)
unit.SetSubstitution(thumbResID)
}
}
func ConfigureVideoObject(layer fsdk.OFDLayer, videoPath string) {
baseObj := layer.AddGraphicsObject(int(fsdk.OFDLayerE_GraphicsObjectTypeVideo))
videoObj := fsdk.NewOFDVideoObject(baseObj)
if videoObj.IsEmpty() {
return
}
defer fsdk.DeleteOFDVideoObject(videoObj)
videoObj.SetType("mp4")
videoObj.SetFormat("video/mp4")
videoObj.SetTitle("demo")
videoObj.SetVideoFromFile(videoPath)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function addCompositeWithThumbnail(layer, thumbResId) {
const compositeObj = layer.AddCompositeObject();
if (compositeObj.IsEmpty()) {
return;
}
const unit = compositeObj.AddUnit();
if (!unit.IsEmpty()) {
unit.SetWidth(100.0);
unit.SetHeight(80.0);
unit.SetThumbnail(thumbResId);
unit.SetSubstitution(thumbResId);
}
}
function configureVideoObject(layer, videoPath) {
const baseObj = layer.AddGraphicsObject(FSDK.OFDLayer.e_GraphicsObjectTypeVideo);
const videoObj = new FSDK.OFDVideoObject(baseObj);
if (videoObj.IsEmpty()) {
return;
}
videoObj.SetType("mp4");
videoObj.SetFormat("video/mp4");
videoObj.SetTitle("demo");
videoObj.SetVideoFromFile(videoPath);
}
图元交互(动作)
任意图元可通过 OFDGraphicsObject::CreateActions / GetActions 绑定动作(跳转、URI 等),用法与页面级动作一致。完整示例请参阅 OFD 注释与动作 中「配置动作响应」一节。
c++
#include "ofd/fs_ofdaction.h"
#include "ofd/graphics/fs_ofdgraphicsobject.h"
using namespace foxit::ofd;
using namespace foxit::ofd::graphics;
void BindUriActionToGraphicsObject(OFDGraphicsObject& obj) {
if (obj.IsEmpty()) {
return;
}
Actions actions = obj.CreateActions();
if (actions.IsEmpty()) {
return;
}
URIAction uri_action(actions.AddAction(Action::e_ActionTypeUri));
if (!uri_action.IsEmpty()) {
uri_action.SetEventTrigger(Action::e_ActionEventTriggerTypeClick);
uri_action.SetURI("https://www.example.com");
}
}
注意事项
- 对象有效性:调用图元或图层方法前,使用
IsEmpty()检查句柄是否有效。 - 类型安全:将
OFDGraphicsObject转为OFDImageObject、OFDPathObject等子类前,须先通过GetType()确认类型匹配,否则构造派生类可能抛出异常。 - 继承关系:
OFDLayer继承自OFDGraphicsObject;OFDBlockObject继承自OFDLayer,可作为嵌套容器继续遍历子图元。 - 坐标系与变换:图元实际位置由
GetBoundary()与GetMatrix()共同影响,修改位置时需同时考虑矩阵变换。 - 资源依赖:图像、视频、复合单元缩略图等依赖
resource_id,须先通过OFDDoc::AddImageResourceFromFile等接口将资源加入文档,请参阅 元数据、资源与自定义数据。 - 持久化:对图层与图元的增删改均在内存中进行,须调用
OFDPackage::Save或SaveAs写回文件。 - 公文语义节点:
OfficeNode不属于图元章节,请参阅 自定义标引与公文节点。