在 Python 中创建 PDF 链接
Contents
[
Hide
]
PDF 文档中的链接
根据 PDF 1.7 规范(ISO 32000-1:2008),链接注释可以触发多种类型的动作,这些动作定义了注释激活时会发生什么。以下是支持的主要动作:
- GoTo – 导航到同一文档中的目标。
- GoToR – 跳转到另一个 PDF 文件中的目标(远程跳转)。
- URI – 打开统一资源标识符,通常是网页链接。
- Launch – 启动一个应用程序或打开一个文件(取决于平台,且通常出于安全原因受到限制)。
- Named – 执行预定义的操作,例如转到下一页或打印文档。
- JavaScript – 执行嵌入的 JavaScript 代码(因安全考虑需谨慎使用)。
- SubmitForm – 将表单数据提交到指定的 URL。
- ResetForm – 将表单字段重置为默认值。
- ImportData – 将数据从外部文件导入文档。
通过在文档中添加指向应用程序的链接,可以实现从文档链接到应用程序。这在例如希望读者在教程的特定位置执行某个操作,或创建功能丰富的文档时非常有用。
使用‘LaunchAction’创建应用程序链接:
import aspose.pdf as ap
from os import path
import sys
def create_link_annotation_launch_action(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
border = ap.annotations.Border(link)
border.width = 5
border.dash = ap.annotations.Dash(1, 1)
link.color = ap.Color.green
link.action = ap.annotations.LaunchAction(document, "sample.pdf")
page.annotations.append(link)
document.save(outfile)
在 PDF 文件中创建 PDF 文档链接
使用 GoToRemoteAction
此代码片段演示了如何使用 Python PDF 库向 PDF 文档的特定页面添加链接注释。
- 打开 PDF 文档
- 选择文档的第二页(索引 1)
- 创建链接注释:
- 位于坐标 (10, 580, 120, 600) 处
- 颜色为绿色
- 在第一页链接到 ‘sample.pdf’
- 将链接注释添加到页面
- 将修改后的文档保存到输出文件路径
使用 ‘GoToRemoteAction’ 创建 PDF 文档链接:
import aspose.pdf as ap
from os import path
import sys
def create_link_annotation_go_to_remote_action(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
link.color = ap.Color.green
link.action = ap.annotations.GoToRemoteAction("sample.pdf", 1)
page.annotations.append(link)
document.save(outfile)
使用 GoToAction
此代码演示了如何使用 Aspose.PDF for Python 向 PDF 文档的特定页面添加链接注释。该链接显示为绿色、虚线边框的矩形,并允许用户在同一 PDF 中导航到另一页。要使用 ‘GoToAction’ 创建 PDF 文档链接:
import aspose.pdf as ap
from os import path
import sys
def create_link_annotation_go_to_action(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
border = ap.annotations.Border(link)
border.width = 5
border.dash = ap.annotations.Dash(1, 1)
link.color = ap.Color.green
if document.pages.length >= 4:
link.action = ap.annotations.GoToAction(document.pages[4])
else:
link.action = ap.annotations.GoToAction(document.pages[document.pages.length])
page.annotations.append(link)
document.save(outfile)
应用 GoToURIAction
下一个示例演示如何使用 Aspose.PDF for Python 向 PDF 文档添加链接批注。该链接在首页上显示为绿色可点击区域,点击后会通过 GoToURIAction 在网页浏览器中打开 Aspose.PDF for Python 文档。
此功能可用于在 PDF 中直接嵌入有用的外部参考、文档或支持链接。
- 加载 PDF 文档。使用 ap.Document 打开现有的 PDF 文件。
- 访问第一页。使用 document.pages[1] 访问第一页(Aspose 使用基于 1 的索引)。
- 创建链接注释。创建一个 LinkAnnotation 对象,并使用 ap.Rectangle 指定可点击的矩形区域。
- 设置注释外观。使用 link.color = ap.Color.green 将注释的颜色设置为绿色。
- 分配一个 URI 操作。使用 GoToURIAction 将注释链接到外部 URL。
- 将注释添加到页面。将配置好的链接注释追加到第一页的注释集合中。
- 保存已修改的文档。将更新后的 PDF 文档保存到指定的输出路径。
import aspose.pdf as ap
from os import path
import sys
def create_link_annotation_go_to_URI_action(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
link.color = ap.Color.green
link.action = ap.annotations.GoToURIAction("https://docs.aspose.com/pdf/python")
page.annotations.append(link)
document.save(outfile)