Skip to content

OFD 注释与动作 ​

本节介绍 OFD 页面注释(Annot)的访问与配置,以及动作(Action 及其派生类)的配置。动作通常挂在注释外观图元(OFDGraphicsObject)上;取得 OFDPage 的方式请参阅 OFD 文档与页面。

能力概览 ​

能力说明
页面注释枚举、创建、修改注释属性与外观
页内跳转GoToAction:跳转到指定页与坐标
打开附件GotoAAction:按附件 ID 打开已嵌入附件
外部链接 / 多媒体URIAction;SoundAction / MovieAction(当前不支持)
动作集合外观图元上 CreateActions / AddAction / InsertAction
批注迁移文档级导出/导入批注数据

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

访问页面注释 ​

通过 OFDPage::GetAnnotCount 与 GetAnnot 遍历页面注释,读取类型与外接矩形。GetBoundary 返回的矩形基于 OFD 页面坐标系,UI 绘制或坐标转换时需结合页面显示矩阵,请参阅 OFD 渲染。

渲染时可通过 OFDRender 的内容标志控制是否绘制注释层。

c++
#include "ofd/fs_ofdpackage.h"
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdpage.h"
#include "ofd/annots/fs_ofdannot.h"

using namespace foxit;
using namespace foxit::ofd;

void ReadPageAnnots(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 annot_count = page.GetAnnotCount();
  for (int i = 0; i < annot_count; ++i) {
    Annot annot = page.GetAnnot(i);
    if (annot.IsEmpty()) {
      continue;
    }

    Annot::AnnotType type = annot.GetType();
    RectF boundary = annot.GetBoundary();
    String creator = annot.GetCreator();
  }
}
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.annots.Annot;

public class OFDAnnotReadExample {
    public static void readPageAnnots(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 annotCount = page.getAnnotCount();
        for (int i = 0; i < annotCount; i++) {
            Annot annot = page.getAnnot(i);
            if (annot.isEmpty()) {
                continue;
            }

            int type = annot.getType();
            RectF boundary = annot.getBoundary();
            String creator = annot.getCreator();
        }
    }
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
using foxit.ofd.annots;

void ReadPageAnnots(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 annotCount = page.GetAnnotCount();
                for (int i = 0; i < annotCount; ++i)
                {
                    using (Annot annot = page.GetAnnot(i))
                    {
                        if (annot.IsEmpty())
                        {
                            continue;
                        }

                        Annot.AnnotType type = annot.GetType();
                        RectF boundary = annot.GetBoundary();
                        string creator = annot.GetCreator();
                    }
                }
            }
        }
    }
}
python
from FoxitPDFSDKPython3 import *

def read_page_annots(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.GetAnnotCount()):
        annot = page.GetAnnot(i)
        if annot.IsEmpty():
            continue
        annot_type = annot.GetType()
        boundary = annot.GetBoundary()
        creator = annot.GetCreator()
go
package main

import "foxit.com/fsdk"

func ReadPageAnnots(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
	}

	count := page.GetAnnotCount()
	for i := 0; i < count; i++ {
		annot := page.GetAnnot(i)
		if annot.IsEmpty() {
			continue
		}
		_ = annot.GetType()
		boundary := annot.GetBoundary()
		_ = annot.GetCreator()
		fsdk.DeleteRectF(boundary)
		fsdk.DeleteOFDAnnot(annot)
	}
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function readPageAnnots(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 annotCount = page.GetAnnotCount();
  for (let i = 0; i < annotCount; i++) {
    const annot = page.GetAnnot(i);
    if (annot.IsEmpty()) {
      continue;
    }
    const type = annot.GetType();
    const boundary = annot.GetBoundary();
    const creator = annot.GetCreator();
  }
}

创建与配置注释 ​

调用 OFDPage::AddAnnot 创建指定类型的注释,再通过 SetBoundary 设置热区,SetCreator、EnableVisible、EnablePrintable、EnableReadOnly 等接口控制元数据与状态。为链接注释配置 URI 或 GotoA 时,推荐在外观图元上创建动作集合(见下文)。

签章场景可使用 e_AnnotTypeStamp,配合 AddAppearanceObject 设置签章外观;高亮注释可通过外观路径图元定义填充区域。

c++
#include "ofd/fs_ofdpage.h"
#include "ofd/fs_ofdaction.h"
#include "ofd/annots/fs_ofdannot.h"
#include "ofd/graphics/fs_ofdgraphicsobject.h"

using namespace foxit;
using namespace foxit::ofd;
using namespace foxit::ofd::graphics;

bool CreateLinkAnnot(OFDPage& page) {
  if (page.IsEmpty()) {
    return false;
  }

  Annot annot = page.AddAnnot(Annot::e_AnnotTypeLink);
  if (annot.IsEmpty()) {
    return false;
  }

  annot.SetBoundary(RectF(20.0f, 180.0f, 80.0f, 140.0f));
  annot.SetSubtype("Link");
  annot.SetCreator("DemoUser");
  annot.EnableVisible(true);
  annot.EnablePrintable(true);

  OFDGraphicsObject appearance = annot.AddAppearanceObject(
      OFDGraphicsObject::e_ObjectTypePath);
  if (appearance.IsEmpty()) {
    return false;
  }

  Actions actions = appearance.CreateActions();
  if (actions.IsEmpty()) {
    return false;
  }

  URIAction uri_action(actions.AddAction(Action::e_ActionTypeUri));
  if (uri_action.IsEmpty()) {
    return false;
  }
  uri_action.SetEventTrigger(Action::e_ActionEventTriggerTypeClick);
  uri_action.SetURI("https://www.example.com");
  return true;
}
java
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.ofd.Action;
import com.foxit.sdk.ofd.Actions;
import com.foxit.sdk.ofd.OFDPage;
import com.foxit.sdk.ofd.URIAction;
import com.foxit.sdk.ofd.annots.Annot;
import com.foxit.sdk.ofd.graphics.OFDGraphicsObject;

public class OFDAnnotCreateExample {
    public static boolean createLinkAnnot(OFDPage page) throws Exception {
        if (page.isEmpty()) {
            return false;
        }

        Annot annot = page.addAnnot(Annot.e_AnnotTypeLink);
        if (annot.isEmpty()) {
            return false;
        }

        annot.setBoundary(new RectF(20.0f, 180.0f, 80.0f, 140.0f));
        annot.setSubtype("Link");
        annot.setCreator("DemoUser");
        annot.enableVisible(true);
        annot.enablePrintable(true);

        OFDGraphicsObject appearance = annot.addAppearanceObject(
                OFDGraphicsObject.e_ObjectTypePath);
        if (appearance.isEmpty()) {
            return false;
        }

        Actions actions = appearance.createActions();
        if (actions.isEmpty()) {
            return false;
        }

        URIAction uriAction = new URIAction(
                actions.addAction(Action.e_ActionTypeUri));
        if (uriAction.isEmpty()) {
            return false;
        }
        uriAction.setEventTrigger(Action.e_ActionEventTriggerTypeClick);
        uriAction.setURI("https://www.example.com");
        return true;
    }
}
csharp
using foxit.common.fxcrt;
using foxit.ofd;
using foxit.ofd.annots;
using foxit.ofd.graphics;

bool CreateLinkAnnot(OFDPage page)
{
    if (page.IsEmpty())
    {
        return false;
    }

    using (Annot annot = page.AddAnnot(Annot.AnnotType.e_AnnotTypeLink))
    {
        if (annot.IsEmpty())
        {
            return false;
        }

        annot.SetBoundary(new RectF(20.0f, 180.0f, 80.0f, 140.0f));
        annot.SetSubtype("Link");
        annot.SetCreator("DemoUser");
        annot.EnableVisible(true);
        annot.EnablePrintable(true);

        using (OFDGraphicsObject appearance = annot.AddAppearanceObject(
            OFDGraphicsObject.ObjectType.e_ObjectTypePath))
        {
            if (appearance.IsEmpty())
            {
                return false;
            }

            using (Actions actions = appearance.CreateActions())
            {
                if (actions.IsEmpty())
                {
                    return false;
                }

                using (URIAction uriAction = new URIAction(actions.AddAction(Action.ActionType.e_ActionTypeUri)))
                {
                    if (uriAction.IsEmpty())
                    {
                        return false;
                    }
                    uriAction.SetEventTrigger(Action.ActionEventTriggerType.e_ActionEventTriggerTypeClick);
                    uriAction.SetURI("https://www.example.com");
                    return true;
                }
            }
        }
    }
}
python
from FoxitPDFSDKPython3 import *

def create_link_annot(page):
    if page.IsEmpty():
        return False

    annot = page.AddAnnot(OFDAnnot.e_AnnotTypeLink)
    if annot.IsEmpty():
        return False

    annot.SetBoundary(RectF(20.0, 180.0, 80.0, 140.0))
    annot.SetSubtype("Link")
    annot.SetCreator("DemoUser")
    annot.EnableVisible(True)
    annot.EnablePrintable(True)

    appearance = annot.AddAppearanceObject(OFDGraphicsObject.e_ObjectTypePath)
    if appearance.IsEmpty():
        return False

    actions = appearance.CreateActions()
    if actions.IsEmpty():
        return False

    uri_action = OFDURIAction(actions.AddAction(OFDAction.e_ActionTypeUri))
    if uri_action.IsEmpty():
        return False
    uri_action.SetEventTrigger(OFDAction.e_ActionEventTriggerTypeClick)
    uri_action.SetURI("https://www.example.com")
    return True
go
package main

import "foxit.com/fsdk"

func CreateLinkAnnot(page fsdk.OFDPage) bool {
	if page.IsEmpty() {
		return false
	}

	annot := page.AddAnnot(fsdk.OFDAnnotE_AnnotTypeLink)
	defer fsdk.DeleteOFDAnnot(annot)
	if annot.IsEmpty() {
		return false
	}

	rect := fsdk.NewRectF(20.0, 180.0, 80.0, 140.0)
	defer fsdk.DeleteRectF(rect)
	annot.SetBoundary(rect)
	annot.SetSubtype("Link")
	annot.SetCreator("DemoUser")
	annot.EnableVisible(true)
	annot.EnablePrintable(true)

	appearance := annot.AddAppearanceObject(fsdk.OFDGraphicsObjectE_ObjectTypePath)
	defer fsdk.DeleteOFDGraphicsObject(appearance)
	if appearance.IsEmpty() {
		return false
	}

	actions := appearance.CreateActions()
	defer fsdk.DeleteOFDActions(actions)
	if actions.IsEmpty() {
		return false
	}

	uriAction := fsdk.NewOFDURIAction(actions.AddAction(fsdk.OFDActionE_ActionTypeUri))
	defer fsdk.DeleteOFDURIAction(uriAction)
	if uriAction.IsEmpty() {
		return false
	}
	uriAction.SetEventTrigger(fsdk.OFDActionE_ActionEventTriggerTypeClick)
	uriAction.SetURI("https://www.example.com")
	return true
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function createLinkAnnot(page) {
  if (page.IsEmpty()) {
    return false;
  }

  const annot = page.AddAnnot(FSDK.OFDAnnot.e_AnnotTypeLink);
  if (annot.IsEmpty()) {
    return false;
  }

  annot.SetBoundary(new FSDK.RectF(20.0, 180.0, 80.0, 140.0));
  annot.SetSubtype("Link");
  annot.SetCreator("DemoUser");
  annot.EnableVisible(true);
  annot.EnablePrintable(true);

  const appearance = annot.AddAppearanceObject(FSDK.OFDGraphicsObject.e_ObjectTypePath);
  if (appearance.IsEmpty()) {
    return false;
  }

  const actions = appearance.CreateActions();
  if (actions.IsEmpty()) {
    return false;
  }

  const uriAction = new FSDK.OFDURIAction(actions.AddAction(FSDK.OFDAction.e_ActionTypeUri));
  if (uriAction.IsEmpty()) {
    return false;
  }
  uriAction.SetEventTrigger(FSDK.OFDAction.e_ActionEventTriggerTypeClick);
  uriAction.SetURI("https://www.example.com");
  return true;
}

配置动作响应 ​

动作定义用户与页面对象交互时的行为。推荐在注释的 外观图元(Annot::AddAppearanceObject / GetAppearanceObject 得到的 OFDGraphicsObject)上创建动作集合:

  1. appearance.CreateActions() 取得 Actions。
  2. AddAction 或 InsertAction(type, index) 指定动作类型。
  3. 将返回的 Action 转为派生类并设置参数与 SetEventTrigger。

GoToAction 用于 页内跳转;若要在点击后 打开已嵌入附件,请使用下文的 GotoAAction。

以下示例以 C++ / Java 为例;其余语言绑定 API 同构,可参考发布包 examples 与接口文档。

c++
#include "ofd/fs_ofdaction.h"
#include "ofd/graphics/fs_ofdgraphicsobject.h"

using namespace foxit::ofd;
using namespace foxit::ofd::graphics;

void ConfigureAppearanceActions(OFDGraphicsObject& appearance, uint32 page_id) {
  if (appearance.IsEmpty()) {
    return;
  }

  Actions actions = appearance.CreateActions();
  if (actions.IsEmpty()) {
    return;
  }

  GoToAction goto_action(actions.AddAction(Action::e_ActionTypeGoto));
  if (!goto_action.IsEmpty()) {
    goto_action.SetEventTrigger(Action::e_ActionEventTriggerTypeClick);
    goto_action.SetDestination(12.5f, 34.5f, page_id);
  }

  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");
  }
}
java
import com.foxit.sdk.ofd.Action;
import com.foxit.sdk.ofd.Actions;
import com.foxit.sdk.ofd.GoToAction;
import com.foxit.sdk.ofd.URIAction;
import com.foxit.sdk.ofd.graphics.OFDGraphicsObject;

public class OFDActionExample {
    public static void configureAppearanceActions(OFDGraphicsObject appearance, long pageId)
            throws Exception {
        if (appearance == null || appearance.isEmpty()) {
            return;
        }

        Actions actions = appearance.createActions();
        if (actions.isEmpty()) {
            return;
        }

        GoToAction gotoAction = new GoToAction(
                actions.addAction(Action.e_ActionTypeGoto));
        if (!gotoAction.isEmpty()) {
            gotoAction.setEventTrigger(Action.e_ActionEventTriggerTypeClick);
            gotoAction.setDestination(12.5f, 34.5f, (int) pageId);
        }

        URIAction uriAction = new URIAction(
                actions.addAction(Action.e_ActionTypeUri));
        if (!uriAction.isEmpty()) {
            uriAction.setEventTrigger(Action.e_ActionEventTriggerTypeClick);
            uriAction.setURI("https://www.example.com");
        }
    }
}

打开嵌入附件(GotoA) ​

业务上常需要:将文件作为附件嵌入 OFD 后,用户点击大纲节点或页面上的指定对象即可打开该附件。

与 GoToAction(页内跳转)不同,GotoAAction 通过 附件 ID 关联目标附件:

  1. 先按 OFD 文档附件 添加附件,并取得 Attachment::GetID()。
  2. 在外观图元上 CreateActions(),再 AddAction(Action::e_ActionTypeGotoA)(或 InsertAction)。
  3. 构造 GotoAAction,调用 SetAttachmentID(attachment_id),并设置触发时机(如单击)。

大纲节点也可直接调用 Outline::SetAttachmentID,效果等价于为该节点设置 GotoA 动作;详见 书签、大纲与目录。

以下示例以 C++ / Java 为例;其余语言绑定 API 同构,可参考发布包 examples 与接口文档。

c++
#include "ofd/fs_ofdaction.h"
#include "ofd/graphics/fs_ofdgraphicsobject.h"

using namespace foxit;
using namespace foxit::ofd;
using namespace foxit::ofd::graphics;

void BindGotoAAction(OFDGraphicsObject& appearance, uint32 attachment_id) {
  if (appearance.IsEmpty()) {
    return;
  }

  Actions actions = appearance.CreateActions();
  if (actions.IsEmpty()) {
    return;
  }

  GotoAAction goto_a(actions.AddAction(Action::e_ActionTypeGotoA));
  if (goto_a.IsEmpty()) {
    return;
  }

  goto_a.SetEventTrigger(Action::e_ActionEventTriggerTypeClick);
  goto_a.SetAttachmentID(attachment_id);
}
java
import com.foxit.sdk.ofd.Action;
import com.foxit.sdk.ofd.Actions;
import com.foxit.sdk.ofd.GotoAAction;
import com.foxit.sdk.ofd.graphics.OFDGraphicsObject;

public class GotoAExample {
    public static void bindGotoAAction(OFDGraphicsObject appearance, long attachmentId) {
        if (appearance == null || appearance.isEmpty()) {
            return;
        }

        Actions actions = appearance.createActions();
        if (actions.isEmpty()) {
            return;
        }

        GotoAAction gotoA = new GotoAAction(
                actions.addAction(Action.e_ActionTypeGotoA));
        if (gotoA.isEmpty()) {
            return;
        }

        gotoA.setEventTrigger(Action.e_ActionEventTriggerTypeClick);
        gotoA.setAttachmentID((int) attachmentId);
    }
}

批注数据迁移 ​

文档级迁移可使用 OFDDoc::ExportAnnots 将指定页范围批注导出为独立文件,再通过 ImportAnnots 导入目标文档。跨文件合并多个批注包时,可使用 AnnotCombination::AddAnnotFile 聚合后 ExportToFile 或 ExportAnnotFile 输出。

以下示例以 C++ / Java 为例;其余语言绑定 API 同构,可参考发布包 examples 与接口文档。

c++
#include "ofd/fs_ofddoc.h"
#include "ofd/annots/fs_ofdannot.h"

using namespace foxit::ofd;

void ExportDocumentAnnots(OFDDoc& doc, const char* export_path) {
  if (doc.IsEmpty() || doc.GetPageCount() <= 0) {
    return;
  }

  doc.ExportAnnots(0, doc.GetPageCount() - 1, export_path);
}

bool MergeAnnotFiles(const char* output_path) {
  AnnotCombination combination;
  if (combination.IsEmpty()) {
    return false;
  }

  if (!combination.AddAnnotFile("annot_pack_1.ofd")) {
    return false;
  }
  if (!combination.AddAnnotFile("annot_pack_2.ofd")) {
    return false;
  }

  return combination.ExportToFile(output_path);
}
java
import com.foxit.sdk.ofd.OFDDoc;
import com.foxit.sdk.ofd.annots.AnnotCombination;

public class OFDAnnotMigrationExample {
    public static void exportDocumentAnnots(OFDDoc doc, String exportPath) throws Exception {
        if (doc.isEmpty() || doc.getPageCount() <= 0) {
            return;
        }

        doc.exportAnnots(0, doc.getPageCount() - 1, exportPath);
    }

    public static boolean mergeAnnotFiles(String outputPath) throws Exception {
        AnnotCombination combination = new AnnotCombination();
        if (combination.isEmpty()) {
            return false;
        }

        if (!combination.addAnnotFile("annot_pack_1.ofd")) {
            return false;
        }
        if (!combination.addAnnotFile("annot_pack_2.ofd")) {
            return false;
        }

        return combination.exportToFile(outputPath);
    }
}

注意事项 ​

  • 坐标系:GetBoundary 与动作目标坐标均基于 OFD 页面坐标系,与屏幕/UI 坐标转换时请使用 OFDPage::GetDisplayMatrix。
  • 资源依赖:SoundAction、MovieAction 依赖 SetResourceID 所引用的资源 ID,须先将音频或视频加入 OFDDoc 资源库后再绑定动作。
  • 类型匹配:从 Action 构造 GoToAction、URIAction 等派生类时,动作类型必须匹配,否则将抛出异常。
  • 持久化:对 Annot 或 Action 的修改均为内存操作,须调用 OFDPackage::Save 或 SaveAs 写回文件,请参阅 OFD 文件包。
  • 与 PDF 模块的差异:OFD 动作类型与 PDF 模块不完全相同,请以 OFD 接口文档 为准。