PDF 书签 (PDF Outline)
福昕 PDF SDK 提供了名为 Bookmarks 的导航工具,允许用户在 PDF 文档中快速定位和链接他们感兴趣的部分。PDF 书签也称为 outline,每个书签包含一个目标位置或动作来描述它链接到的位置。它是一个树形的层次结构,因此在访问 bookmark 树之前,必须首先调用接口 PDFDoc.GetRootBookmark
以获取整个 bookmark 树的 root。这里,"root bookmark" 是一个抽象对象,它只有一些 child bookmarks,没有 next sibling bookmarks, 也没有任何数据 (包括 bookmark 数据,目标位置数据和动作数据)。因为它没有任何数据,因此无法在应用程序界面上显示,能够调用的接口只有 Bookmark.GetFirstChild
。
在检索 root bookmark
后,就可以调用以下的接口去访问其他的 bookmarks
:
- 访问
parent bookmark
,使用Bookmark.GetParent
接口。 - 访问第一个
child bookmark
,使用Bookmark.GetFirstChild
接口。 - 访问
next sibling bookmark
,使用Bookmark.GetNextSibling
接口。 - 插入一个新的
bookmark
,使用Bookmark.Insert
接口。 - 移动一个
bookmark
,使用Bookmark.MoveTo
接口。
如何使用深度优先顺序遍历 PDF 文档的 bookmarks
js
import {FoxitRDKNative} from 'foxit_rdk';
class BookmarkUnit {
private depthFistTravelBookmarkTree(bookmark: FoxitRDKNative.pdf.Bookmark, doc: FoxitRDKNative.pdf.PDFDoc): void {
if (bookmark == null || bookmark.IsEmpty()) {
return;
}
try {
this.depthFistTravelBookmarkTree(bookmark.GetFirstChild(), doc);
while (true) {
// 获取书签标题
const title = bookmark.GetTitle();
const dest = bookmark.GetDestination();
if (dest != null && !dest.IsEmpty()) {
let left: number, right: number, top: number, bottom: number;
let zoom: number;
const pageIndex = dest.GetPageIndex(doc);
// left,right,top,bottom,zoom只在一些特殊的缩放模式下有意义
const mode = dest.GetZoomMode();
switch (mode) {
case FoxitRDKNative.pdf.actions.Destination.e_ZoomXYZ:
left = dest.GetLeft();
top = dest.GetTop();
zoom = dest.GetZoomFactor();
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitPage:
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitHorz:
top = dest.GetTop();
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitVert:
left = dest.GetLeft();
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitRect:
left = dest.GetLeft();
bottom = dest.GetBottom();
right = dest.GetRight();
top = dest.GetTop();
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitBBox:
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitBHorz:
top = dest.GetTop();
break;
case FoxitRDKNative.pdf.actions.Destination.e_ZoomFitBVert:
left = dest.GetLeft();
break;
default:
break;
}
}
bookmark = bookmark.GetNextSibling();
if (bookmark == null || bookmark.IsEmpty()) {
break;
}
this.depthFistTravelBookmarkTree(bookmark.GetFirstChild(), doc);
}
} catch (e) {
console.error(e);
}
}
}