如何正确定位和激活 PDF 中的超链接?
How to properly position and activate a hyperlink in PDF?
我正在尝试编写将 hyperlinks 添加到现有 PDF 文档的代码。
我试图通过遵循 PDF 32000 规范来做到这一点,但似乎我遗漏了一些东西。
PDF 查看器显示我的 link 注释并将鼠标指针悬停在它们上方时将鼠标指针更改为 "finger",但是 没有显示 URL,点击它们也不起作用任何东西.
PDF内容如下所示。
页数:
/Type/Page
/Parent 1 0 R
/MediaBox[0 0 594.75 841.5]
/Contents 7 0 R
/Resources 8 0 R
/Annots [5 0 R 6 0 R] % added by me
第一个注释("Google",#5):
/Type /Annot
/Subtype /Link
/Rect [60.0 779.25 150.0 767.25]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/Subtype /URI
/URI (https://google.com)
>>
第二个注释("DuckDuckGo",#6):
/Type /Annot
/Subtype /Link
/Rect [342.0 694.5 432.0 682.5]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/Subtype /URI
/URI (https://duckduckgo.com)
>>
内容流:
1 0 0 -1 36.0 841.5 cm
-100 Tz
q
/Font3 -9.0 Tf
0.0 0.0 0.0 rg
24.0 62.25 90.0 12.0 re W n
BT
1 0 0 1 0 0 Tm
24.75 69.75 Td
(Google) Tj
ET
Q
q
/Font3 -9.0 Tf
0.0 0.0 0.0 rg
306.0 147.0 90.0 12.0 re W n
BT
1 0 0 1 0 0 Tm
306.75 154.5 Td
(DuckDuckGo) Tj
ET
Q
此代码产生以下结果:
Generated PDF file is available here.
我查看了 Microsoft Word 为 link 生成的代码,它看起来几乎相同(除了缩小和结构父级;此代码有效,但我的无效):
/Subtype/Link/Rect[ 69.75 697.51 113.16 720] /BS
<<
/W 0
>>
/F 4/A
<<
/Type/Action/S/URI/URI(https://google.com/)
>>
/StructParent 0
任何建议将不胜感激。
您必须解析页面内容并处理所有影响文本位置的运算符,才能计算出 Google 和 DuckDuckGo 的确切位置。然后将 link 注释放在计算出的位置。
您的动作字典如下所示:
<<
/Type /Action
/Subtype /URI
/URI (https://google.com)
>>
如果您查找 ISO 32000-1 第 12.6.2 节 Action Dictionaries,您会发现动作类型不是 的值子类型 但不是 S:
S
name
(Required) The type of action that this dictionary describes; see Table 194 for specific values.
(ISO 32000-1 Table 193 – 所有动作词典通用的条目)
因此,在您的 Action 字典中(not 在周围 Annot 字典,不过!) 将 /Subtype
替换为 /S
:
/Type /Annot
/Subtype /Link %<------------- not here
/Rect [342.0 694.5 432.0 682.5]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/S /URI %<------------- but here
/URI (https://duckduckgo.com)
>>
我正在尝试编写将 hyperlinks 添加到现有 PDF 文档的代码。 我试图通过遵循 PDF 32000 规范来做到这一点,但似乎我遗漏了一些东西。
PDF 查看器显示我的 link 注释并将鼠标指针悬停在它们上方时将鼠标指针更改为 "finger",但是 没有显示 URL,点击它们也不起作用任何东西.
PDF内容如下所示。
页数:
/Type/Page
/Parent 1 0 R
/MediaBox[0 0 594.75 841.5]
/Contents 7 0 R
/Resources 8 0 R
/Annots [5 0 R 6 0 R] % added by me
第一个注释("Google",#5):
/Type /Annot
/Subtype /Link
/Rect [60.0 779.25 150.0 767.25]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/Subtype /URI
/URI (https://google.com)
>>
第二个注释("DuckDuckGo",#6):
/Type /Annot
/Subtype /Link
/Rect [342.0 694.5 432.0 682.5]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/Subtype /URI
/URI (https://duckduckgo.com)
>>
内容流:
1 0 0 -1 36.0 841.5 cm
-100 Tz
q
/Font3 -9.0 Tf
0.0 0.0 0.0 rg
24.0 62.25 90.0 12.0 re W n
BT
1 0 0 1 0 0 Tm
24.75 69.75 Td
(Google) Tj
ET
Q
q
/Font3 -9.0 Tf
0.0 0.0 0.0 rg
306.0 147.0 90.0 12.0 re W n
BT
1 0 0 1 0 0 Tm
306.75 154.5 Td
(DuckDuckGo) Tj
ET
Q
此代码产生以下结果:
Generated PDF file is available here.
我查看了 Microsoft Word 为 link 生成的代码,它看起来几乎相同(除了缩小和结构父级;此代码有效,但我的无效):
/Subtype/Link/Rect[ 69.75 697.51 113.16 720] /BS
<<
/W 0
>>
/F 4/A
<<
/Type/Action/S/URI/URI(https://google.com/)
>>
/StructParent 0
任何建议将不胜感激。
您必须解析页面内容并处理所有影响文本位置的运算符,才能计算出 Google 和 DuckDuckGo 的确切位置。然后将 link 注释放在计算出的位置。
您的动作字典如下所示:
<<
/Type /Action
/Subtype /URI
/URI (https://google.com)
>>
如果您查找 ISO 32000-1 第 12.6.2 节 Action Dictionaries,您会发现动作类型不是 的值子类型 但不是 S:
S name (Required) The type of action that this dictionary describes; see Table 194 for specific values.
(ISO 32000-1 Table 193 – 所有动作词典通用的条目)
因此,在您的 Action 字典中(not 在周围 Annot 字典,不过!) 将 /Subtype
替换为 /S
:
/Type /Annot
/Subtype /Link %<------------- not here
/Rect [342.0 694.5 432.0 682.5]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/S /URI %<------------- but here
/URI (https://duckduckgo.com)
>>