You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于borb库为PDF页面添加指向嵌入xlsx文件的注释链接

用borb库给PDF添加打开嵌入XLSX的注释链接

要实现点击注释打开已嵌入的XLSX文件,你需要在指定PDF页面添加文件附件注释,并将注释与嵌入的文件关联。以下是修改后的完整代码:

from borb.pdf import Document
from borb.pdf import PDF
from borb.pdf import Page
from borb.pdf import FileAttachmentAnnotation
from borb.pdf import Rectangle
from borb.pdf import EmbeddedFileAction
import typing

# 加载现有PDF文档
doc: typing.Optional[Document] = None
with open("input.pdf", "rb") as fh:
    doc = PDF.loads(fh)

# 嵌入XLSX文件到PDF
with open("example.xlsx", "rb") as xlsx_file:
    xlsx_contents = xlsx_file.read()
    doc.add_embedded_file("example.xlsx", xlsx_contents)

# 获取需要添加注释的目标页面(索引从0开始,此处为第一页)
target_page: Page = doc.get_page(0)

# 定义注释在页面上的位置与大小:(x坐标, y坐标, 宽度, 高度)
# x从左到右递增,y从下到上递增
annotation_rectangle: Rectangle = Rectangle(100, 100, 50, 50)

# 创建打开嵌入文件的动作,文件名需与嵌入时的名称完全一致
embedded_file_action: EmbeddedFileAction = EmbeddedFileAction("example.xlsx")

# 创建文件附件注释,设置显示图标(可选值:Paperclip、PushPin、Graph等)
file_annotation: FileAttachmentAnnotation = FileAttachmentAnnotation(
    rectangle=annotation_rectangle,
    action=embedded_file_action,
    icon="Paperclip"
)

# 将注释添加到目标页面
target_page.add_annotation(file_annotation)

# 保存修改后的PDF
with open("output.pdf", "wb") as fh:
    PDF.dumps(fh, doc)

关键参数说明

  • doc.get_page(0):根据需求修改索引,指定要添加注释的页面
  • Rectangle:调整坐标和尺寸,控制注释在页面上的显示位置和大小
  • EmbeddedFileAction:必须传入与add_embedded_file一致的文件名,确保关联正确
  • icon:可选不同的注释图标,让注释更直观

内容的提问来源于stack exchange,提问作者Alan Canales

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 11:07:55