技術文章

掌握 PDF 結構:XML 後設資料、書籤和註釋

· PDF 結構

瞭解 PDF XML 後設資料和書籤:技術指南

📖 15 分鐘閱讀 PDF 開發 🔧 中級-高階

PDF document structure showing metadata and bookmarks navigation panel

本綜合指南探討了四個與 PDF 文件的視覺外觀無關的關鍵主題,而是關注那些可能包含在文件中的輔助資料,這些資料可用於互動式螢幕顯示,以及用於攜帶額外資訊的後設資料,這些後設資料供 PDF 工作流程中的程式使用。

涵蓋的關鍵主題

📍 目的地

精確的位置標記,用於定義 PDF 文件中的特定位置。這些標記可實現書籤和超連結的精確導航,而文件大綱則提供分層目錄功能。

📄 XML 後設資料

結構化的 XML 資料流,使用標準化的 XMP 格式提供全面的文件後設資料,不僅包括基本的文件屬性,還包括豐富的描述性資訊。

📎 檔案附件

完整的嵌入檔案功能,將外部資源直接打包到 PDF 文件中,建立類似於電子郵件附件的自包含文件包。

📝 註釋

互動式覆蓋元素,可以在 PDF 頁面上新增文本、圖形和可點選功能,而無需修改底層內容。包括超連結,可實現無縫的文件導航,以及各種標記工具,以增強讀者的互動體驗。

書籤和目標

PDF bookmarks navigation panel showing document outline structure

文件導航依賴於分層書籤結構,技術上稱為。 文件大綱這種樹狀結構系統呈現可點選的條目,通常是章節標題、段落標題和子段落名稱,方便讀者快速跳轉到文件的特定部分。每個書籤條目都將顯示文本與目標資訊結合在一起,目標資訊指定連結應導航到的確切位置。

瞭解目標

PDF 目標在文件中充當精確的位置標記,指定要顯示的頁面、在該頁面上的位置以及要應用的縮放級別。您可以建立目標,方法有兩種:直接在行內定義它們(在我們的示例中,為了清晰起見,我們將使用這種方法),或者通過文件範圍的命名系統通過名稱引用它們。大多數 PDF 閱讀器會在導航面板中顯示書籤,該面板位於主文件內容旁邊。

每個目標都使用一種陣列結構,其中特定元素會根據您希望實現的檢視行為而有所不同。以下是可用的主要目標模式:

目標型別表

注意:“page”表示對頁面物件的間接引用。預設情況下,這些目標與頁面的裁剪框邊界一起使用,如果沒有定義裁剪框,則回退到媒體框。

Array Description
[page /Fit] Scales the page to fit completely within the viewer window, adjusting both width and height proportionally.
[page /FitH top] Positions the specified top coordinate at the window’s top edge while scaling horizontally to fit the full page width.
[page /FitV left] Aligns the specified left coordinate with the window’s left edge while scaling vertically to fit the full page height.
[page /XYZ left top zoom] Positions the coordinates (left, top) at the window’s upper-left corner and applies the specified zoom factor. Null values preserve current settings for those parameters.
[page /FitR left bottom right top] Zooms and positions the view to display the rectangular area defined by the left, bottom, right, and top coordinates.
[page /FitB] Similar to /Fit, but scales based on the actual content boundaries instead of the defined crop box area.
[page /FitBH top] Functions like /FitH but uses the content bounding box instead of the crop box for horizontal scaling calculations.
[page /FitBV left] Operates like /FitV but calculates vertical scaling based on the content bounding box rather than the crop box boundaries.

文件大綱結構

文件大綱建立了一種分層導航結構,該結構作為 PDF 瀏覽器的互動式目錄。這種樹狀結構有助於使用者快速瀏覽複雜文件,因為它提供了清晰的結構概覽。該系統依賴於兩種基本物件型別:

  • 大綱字典 – 大綱層級的根節點
  • 大綱項字典 – 大綱中的各個條目

大綱字典結構表

Key Value Type Value
/Type name If present, must be /Outlines.
/First indirect reference to dictionary References the initial top-level outline entry in the document hierarchy. This field is mandatory when outline entries exist.
/Last indirect reference to dictionary References the final top-level outline entry in the document hierarchy. This field is mandatory when outline entries exist.
/Count integer Specifies how many outline entries are currently expanded across the entire outline tree. Can be omitted when no entries are in an open state.

大綱項實現

每個大綱項都由一個字典組成,該字典指定其顯示標題、目標位置以及與其他層級中條目的關係。

讓我們來看看如何在 PDF 語法中構建一個簡單的文件大綱:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
8 0 obj
<</Type/Outlines/Count 4/First 9 0 R/Last 9 0 R>>
endobj
9 0 obj
<</Title(Chapter 1: Experiment A)/Count 3/Parent 8 0 R/First 12 0 R/Last 18 0 R>>
endobj
12 0 obj
<</Title(1: Introduction)/Count 0/Parent 9 0 R/Next 15 0 R>>
endobj
15 0 obj
<</Title(2: Methodology)/Count 0/Parent 9 0 R/Prev 12 0 R/Next 18 0 R>>
endobj
18 0 obj
<</Title(3: Result verification)/Count 0/Parent 9 0 R/Prev 15 0 R/>>
endobj

大綱專案字典結構表

* 表示必需條目

Key Value Type Value
/Title* text string Text to be displayed for this entry.
/Parent* indirect reference to dictionary References this item’s parent within the outline hierarchy, which can be either another outline item or the root outline dictionary.
/Prev indirect reference to dictionary References the preceding sibling item at the same hierarchical level, when applicable.
/Next indirect reference to dictionary References the following sibling item at the same hierarchical level, when applicable.
/First indirect reference to dictionary References the initial child item under this entry, when child items exist.
/Last indirect reference to dictionary References the final child item under this entry, when child items exist.
/Count integer When the entry is expanded, indicates the count of visible descendant entries. When collapsed, stores a negative value representing the total number of hidden descendants that would become visible upon expansion.
/Dest name, string or array The destination. Arrays are destinations, names are references to entries in the /Dests entry in the document catalog, strings are references to entries in the /Dests entry in the document’s name dictionary.

XML 後設資料

PDF XML metadata structure showing XMP format and RDF elements

現代 PDF 文件可以包含複雜的基於 XML 的後設資料流,這些流提供比傳統文件屬性更詳細和結構化的資訊。這種高階後設資料系統利用 Adobe 的 XMP(可擴充套件後設資料平臺)規範,提供標準化、可供機器讀取的文件描述,從而增強搜尋能力、組織能力和自動化處理能力。

XMP 後設資料結構

XMP 後設資料被打包成一個 XML 文件,該文件使用 RDF(資源描述框架)語法來組織和描述文件屬性,採用標準化的格式。此後設資料內容嵌入在專用的流物件中,該物件包含適當的型別標識,以便 PDF 處理程序能夠識別:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
6 0 obj
<</Length 1235/Type/Metadata/Subtype/XML>>stream
<?xpacket begin="锘xBF" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.2-c001 63.139439, 2010/09/27-13:37:26"
><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
><rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xmp="http://ns.adobe.com/xap/1.0/"
><pdf:Producer>losLab PDF Library</pdf:Producer>
<dc:creator>losLab.com</dc:creator>
<dc:title>Delphi PDF SDKs</dc:title>
<xmp:CreateDate>2025-06-29T10:46:27+08:00</xmp:CreateDate>
<xmp:ModifyDate>2025-06-29T10:58:57+08:00</xmp:ModifyDate>
<xmp:MetadataDate>2025-06-29T10:46:27+08:00</xmp:MetadataDate>
<dc:description>Delphi Development Library for PDF creation & editing</dc:description>
<xmp:CreatorTool>HotPDF Component</xmp:CreatorTool>
<dc:subject>PDF Developer Library for RAD Studio></dc:subject>
<pdf:Keywords>Delphi, PDF SDK, PDF Component</pdf:Keywords>
</rdf:Description>Robust Delphi PDF development library</rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>
endstream
endobj

標準後設資料模式

XMP 框架通過成熟的模式名稱空間組織後設資料,每個名稱空間用於特定資訊類別:

📋 Dublin Core (dc:)

基本書目資訊

  • dc:title – 文件標題
  • dc:creator – 文件作者
  • dc:subject – 文件主題/關鍵詞
  • dc:description – 文件描述
  • dc:format – MIME 型別

🏷️ XMP Basic (xmp:)

核心 XMP 屬性

  • xmp:CreateDate – 建立日期
  • xmp:ModifyDate – 修改日期
  • xmp:CreatorTool – 建立應用程式
  • xmp:MetadataDate – 後設資料修改日期

📄 PDF Schema (pdf:)

PDF 專有屬性

  • pdf:Producer – PDF生成器
  • pdf:Keywords – 文件關鍵詞
  • pdf:PDFVersion – PDF版本

與文件目錄的整合

XML後設資料流從文件目錄中引用:

1
2
3
1 0 obj
< < Type Catalog Pages 2 0 R Metadata 10 0 R Outlines 1 0 R>>
endobj

🎯 XML後設資料最佳實踐

  • 為了獲得最大的相容性,始終包含文件資訊字典和XMP後設資料。
  • 確保兩個位置的後設資料值一致。
  • 使用正確的 XML 編碼(UTF-8)來處理國際字元。
  • 以 ISO 8601 格式包含建立和修改日期。
  • 驗證 XML 結構以防止解析錯誤。

檔案附件

PDF file attachments panel showing embedded files list

PDF 檔案附件提供了一種方便的方法,可以將外部檔案直接嵌入到 PDF 文件中,從而建立包含所有必要資源的自包含軟體包。 這些附件可以與整個文件相關聯,也可以連結到特定頁面,具體取決於您的需求。 大多數現代 PDF 閱覽器會將這些嵌入檔案顯示在專用的附件面板中,使使用者可以輕鬆訪問、檢視或儲存包含的內容。 此功能對於建立全面的文件包特別有用,例如包含補充資源的簡報或包含伴隨資料檔案的報告。

嵌入檔案結構

從根本上說,嵌入檔案由一個流物件組成,該物件包含實際的檔案資料,以及一個流字典條目,用於指定 /Type /EmbeddedFile。 這種簡單的方法允許儲存任何型別的檔案在 PDF 中。 以下是基本嵌入檔案結構的示例:

1
2
3
4
5
6
8 0 obj
< < Type EmbeddedFile Length 35>>
stream
This is a text file attachment...
endstream
endobj

PDF 支援兩種不同的方法來引用嵌入檔案,每種方法都適用於不同的用例:文件級別的附件,可在整個文件中全域性訪問;以及頁面級別的附件,作為特定頁面的互動元素顯示。

文件級別附件

對於文件範圍內的附件,您需要新增一個 /EmbeddedFiles 條目到名稱字典,該字典通過 /Names 條目在文件目錄中訪問。 這種方法使附件在整個 PDF 中全域性可用,無論使用者當前檢視的是哪個頁面。

1
2
3
4
5
6
9 0 obj
< < Names << EmbeddedFiles << Names [ (attachment.txt) << EF << F 8 0 R>> /F (attachment.txt) /Type /Filespec >> ] >>
     >>
   /Pages 1 0 R
   /Type /Catalog >>
endobj

程式碼結構說明

  • /Names – 包含文件的名稱字典。
  • /EmbeddedFiles – 專門處理嵌入的檔名
  • (attachment.txt) – 使用者看到的實際檔名
  • /EF – 包含實際檔案引用的嵌入檔案字典
  • /F 8 0 R – 對嵌入檔案流物件的引用
  • /Type /Filespec – 標識此檔案為檔案規範字典。

頁面級附件。

特定頁面的附件需要採用不同的方法,使用檔案附件批註來實現。這些批註會新增到。 /Annots 在目標頁面的字典中建立一個數組,從而在頁面上顯示一個可見的附件圖示,使用者可以直接與該圖示進行互動。

1
2
3
4
5
6
7
8
9 0 obj
< < Type Page (Other dictionary entries as usual) Annots [ << FS << EF << F 8 0 R>> /F (attachment.txt) /Type /Filespec >>
          /Subtype /FileAttachment
          /Contents (attachment.txt)
          /Rect [ 18 796.88976378 45 823.88976378 ]
       >> ]
>>
endobj

頁面附件屬性。

  • /FS - 檔案規範字典(與 /EF 相同)
  • /Subtype /FileAttachment – 標識此註釋為一個檔案附件。
  • /Contents – 當滑鼠懸停在附件圖示上時顯示的提示文本。
  • /Rect – 定義附件圖示在頁面上的位置和大小的矩形。

附件使用場景

📊 資料檔案

將電子表格、資料庫或原始資料檔案與報告和分析一起嵌入。

🎨 原始檔案

包含原始設計檔案、CAD圖紙或可編輯的模板。

📹 媒體資源

附加影片演示、音訊錄音或互動式內容。

📋 輔助文件

捆綁相關的PDF檔案、合同或參考資料。

批註

Various PDF annotation types including highlights, notes, and links

PDF 註解提供了一種強大的方法,可以在不更改原始頁面內容的情況下,向文件新增互動元素和視覺標記。這些疊加元素通過允許使用者突出顯示文本、新增評論或建立可點選連結,從而增強閱讀體驗。最實用的註釋型別之一是超連結,它可以在文件的不同部分之間或到外部資源之間實現無縫導航。

註解結構

儘管不同型別的註釋具有不同的用途,但它們都遵循一致的基礎結構,並根據需要新增特定於型別的屬性。PDF 頁面可以包含多個註釋,這些註釋以陣列的形式組織,並通過每個頁面的字典中的 /Annots 條目進行引用。每個註釋都實現為自己的字典物件,具有特定的屬性。

註解字典結構表

* 表示必需條目

Key Value Type Value
/Type name When specified, this value must be set to /Annot to properly identify the dictionary type.
/Subtype* name Specifies the specific annotation category (e.g., Link, Text, Highlight).
/Rect* rectangle Defines the annotation’s position and dimensions using standard PDF coordinate units.
/Contents text string Contains the annotation’s text content or provides an alternative descriptive label for accessibility purposes.

基礎註釋字典示例:

1
2
3
12 0 obj
< < Type Annot Subtype Link Rect [100 200 300 250] Border [0 0 1] C [0.0 0.0 1.0] Dest [5 0 R XYZ null null null]>>
endobj

常用註釋型別

🔗 連結註釋

建立可點選的區域,用於導航到文件中的其他位置或外部資源。

  • /Subtype /Link – 標識為連結註釋
  • /Dest – 目標陣列或命名目標
  • /A – 動作字典,用於更復雜的行為

📝 文本註釋

顯示點選時出現的彈出式註釋和評論。

  • /Subtype /Text – 標識為文本註釋。
  • /Contents – 註釋的文本內容。
  • /Open – 註釋是否最初是開啟狀態。

🖍️ 標記註釋。

突出顯示、下劃線或刪除文本內容。

  • /Subtype /Highlight – 文本高亮
  • /Subtype /Underline – 文本下劃線
  • /Subtype /StrikeOut – 文本刪除線

高階連結操作

連結註釋可以執行各種操作,而不僅僅是簡單的導航。

1
2
3
13 0 obj
< < Type Annot Subtype Link Rect [50 50 200 100] A << Type Action S URI URI (https: www.example.com)>> >>
endobj

操作型別

  • /S /GoTo – 導航至文件內的目標位置
  • /S /GoToR – 導航至另一個文件的目標位置
  • /S /URI – 開啟網頁連結
  • /S /Launch – 啟動外部應用程式
  • /S /JavaScript – 執行 JavaScript 程式碼

註釋外觀

自定義視覺樣式是通過外觀流實現的,這使得可以精確控制註釋如何向用戶顯示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
14 0 obj
< < Type Annot Subtype Square Rect [100 100 200 150] C [1.0 0.0 0.0] BS << W 2 S S>>
   /AP < < N 15 0 R>> >>
endobj
 
15 0 obj
< < Type XObject Subtype Form BBox [0 0 100 50] Length 85>>
stream
q
1.0 0.0 0.0 RG
2 w
10 10 80 30 re
S
Q
endstream
endobj

實用實施指南

文件結構整合

成功的實施需要理解這些元素如何在更廣泛的PDF文件架構中協同工作。

1
2
3
4
1 0 obj
< < Type Catalog Pages 2 0 R Outlines 3 0 R Names << EmbeddedFiles 4 0 R>>
   /Metadata 5 0 R >>
endobj

✅ 實施檢查清單

  1. 文件目錄設定 – 確保正確引用大綱、名稱和後設資料。
  2. 物件編號 – 保持物件編號和交叉引用的一致性。
  3. 流編碼 – 為流應用適當的過濾器和編碼。
  4. 驗證。 – 使用驗證工具檢查 PDF 結構。
  5. 相容性測試。 - 在不同的 PDF 閱覽器和版本上進行測試。

常見問題和解決方案。

❌ 收藏夾未顯示。

解決方案: 確認文件目錄是否包含一個。 /Outlines 條目,並且確保大綱層級結構正確,父子關係也正確。

❌ 未識別的後設資料。

解決方案: 確保 XML 後設資料流格式正確,使用正確的名稱空間,並在文件目錄中進行引用。 /Type /Metadata/Subtype /XML.

❌ 附件無法訪問

解決方案: 檢查嵌入檔案是否在文件級別的名稱字典或頁面級別的註釋字典中正確引用,並且檔案規範字典的結構是否正確。

結論。

掌握 PDF 後設資料和書籤的實現對於開發專業級文件至關重要,這些文件提供卓越的使用者體驗和功能。這些強大的功能包括:

  • 增強的導航 – 通過結構良好的書籤和目標點
  • 豐富的後設資料 – 提高文件管理和可搜尋性。
  • 檔案整合 – 將相關資源打包到文件中
  • 互動元素 – 通過註釋建立引人入勝的使用者體驗

通過正確實施這些功能,您可以建立超出簡單文本和圖形的 PDF 文件,使其成為全面、互動式的資源,能夠有效地服務於人類讀者和自動化系統。

🚀 下一步

  1. 練習在 PDF 建立工作流程中實施這些結構
  2. 嘗試不同的註釋型別和書籤層級結構
  3. 在多個PDF檢視器上測試您的實現。
  4. 在這些基礎上,探索高階PDF功能。

技術參考。

本指南基於PDF規範,涵蓋與PDF 1.5及更高版本相容的功能。有關最新規範詳情,請參考官方ISO 32000 PDF標準。