連結管理¶
LinkManager 管理 PDF 中所有可點擊的連結(Link Annotation)。連結可指向文件內部頁面(內部連結)或外部 URI(外部連結),並可覆蓋在任何頁面區域之上。
內部連結¶
use NextPDF\Navigation\LinkManager;
use NextPDF\Navigation\Destination;
use NextPDF\ValueObjects\Rectangle;
$links = $document->links();
// 在矩形區域建立內部連結(跳至第 5 頁)
$links->addInternal(
area: Rectangle::fromXY(x: 20.0, y: 100.0, width: 80.0, height: 8.0),
destination: Destination::page(pageNumber: 5),
pageNumber: 1, // 連結所在的頁面
);
外部 URL 連結¶
// 開啟外部網址
$links->addUri(
area: Rectangle::fromXY(x: 20.0, y: 80.0, width: 60.0, height: 8.0),
uri: 'https://nextpdf.dev',
pageNumber: 1,
);
// 電子郵件連結
$links->addUri(
area: Rectangle::fromXY(x: 20.0, y: 70.0, width: 60.0, height: 8.0),
uri: 'mailto:support@nextpdf.dev',
pageNumber: 1,
);
// 電話連結
$links->addUri(
area: Rectangle::fromXY(x: 20.0, y: 60.0, width: 60.0, height: 8.0),
uri: 'tel:+886-2-1234-5678',
pageNumber: 1,
);
跨文件連結¶
// 連結至外部 PDF 文件的特定頁面
$links->addRemote(
area: Rectangle::fromXY(x: 20.0, y: 50.0, width: 80.0, height: 8.0),
filePath: './appendix.pdf',
pageNumber: 1,
remotePage: 3, // 目標 PDF 的第 3 頁
);
文字連結(自動偵測)¶
// 在文字渲染後自動為 URL 加上可點擊連結
$document->links()->autoDetect(
pageNumber: 1,
patterns: [
AutoDetect::Url, // http:// https://
AutoDetect::Email, // foo@bar.com
],
);
連結外觀設定¶
use NextPDF\Navigation\LinkBorderStyle;
// 有顯示外框的連結
$links->addUri(
area: $area,
uri: 'https://nextpdf.dev',
pageNumber: 1,
borderStyle: LinkBorderStyle::create(
width: 1.0,
color: Color::rgb(0, 0, 255),
style: BorderType::Solid, // Solid | Dashed | Underline
),
);
// 無外框連結(常見的隱形點擊區域)
$links->addUri(
area: $area,
uri: 'https://nextpdf.dev',
pageNumber: 1,
borderStyle: LinkBorderStyle::invisible(),
);
具名目標¶
// 定義具名目標(Named Destination)
$document->links()->defineNamedDestination(
name: 'conclusion',
destination: Destination::page(pageNumber: 42),
);
// 在書籤或連結中引用具名目標
Destination::named(name: 'conclusion');
參見¶
- 書籤 — 文件大綱導航
- 目錄 — 自動生成頁面連結
- JavaScript — 進階互動動作