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

Python实现PDF添加点击打开URL的隐藏按钮求助

解决PDF添加隐藏可点击URL区域的问题

问题分析

你的代码存在几个关键问题:

  • open_url函数没有返回Annotation对象,导致pagina.add_annotation接收的是None,无法正确添加注释
  • URL需要完整的协议头(http://或https://),否则PDF阅读器无法识别跳转目标
  • 若要实现完全隐藏的点击区域,需要调整注释的外观设置,或者使用JavaScript按钮控件

方案1:修复RemoteGoToAnnotation实现

这种方式更简单,调整原有代码,确保注释被正确添加并设置为隐藏:

import typing
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf import Document
from borb.pdf.page.page import Page
from borb.pdf import PDF
from borb.pdf.canvas.layout.annotation.remote_go_to_annotation import RemoteGoToAnnotation
from borb.pdf.canvas.color.color import Color

# 读取PDF文档
doc: typing.Optional[Document] = None
with open("path.pdf", "rb") as in_file_handle:
    doc = PDF.loads(in_file_handle)
assert doc is not None

# 创建添加隐藏URL注释的函数
def add_hidden_url_annotation(page: Page, url: str, rect: Rectangle):
    # 确保URL包含协议头
    if not url.startswith(("http://", "https://")):
        url = f"https://{url}"
    # 创建远程跳转注释
    annotation = RemoteGoToAnnotation(rect, url)
    # 设置注释为隐藏(无边框、无填充)
    annotation.set_border_width(0)
    annotation.set_fill_color(Color(0, 0, 0, 0))  # 完全透明填充
    annotation.set_stroke_color(Color(0, 0, 0, 0))  # 完全透明边框
    # 添加注释到页面
    page.add_annotation(annotation)
    return annotation

# 获取第一页
page: Page = doc.get_page(0)
# 定义点击区域(x, y, 宽度, 高度)
button_rect = Rectangle(64, 130, 146-64, 167-130)
target_url = "www.google.be"

# 添加隐藏URL注释
add_hidden_url_annotation(page, target_url, button_rect)

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

print("PDF处理完成")

方案2:使用JavaScriptPushButton实现隐藏按钮

如果阅读器对RemoteGoTo支持不佳,可以用JavaScript按钮,通过JS代码打开URL,同时设置按钮为隐藏:

import typing
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf import Document
from borb.pdf.page.page import Page
from borb.pdf import PDF
from borb.pdf.canvas.layout.annotation.javascript_push_button_annotation import JavaScriptPushButtonAnnotation
from borb.pdf.canvas.color.color import Color

# 读取PDF文档
doc: typing.Optional[Document] = None
with open("path.pdf", "rb") as in_file_handle:
    doc = PDF.loads(in_file_handle)
assert doc is not None

# 创建隐藏JavaScript按钮的函数
def add_hidden_js_button(page: Page, url: str, rect: Rectangle):
    # 确保URL包含协议头
    if not url.startswith(("http://", "https://")):
        url = f"https://{url}"
    # 编写打开URL的JS代码
    js_code = f"app.launchURL('{url}');"
    # 创建JS按钮注释
    button = JavaScriptPushButtonAnnotation(rect, js_code)
    # 设置按钮为隐藏
    button.set_border_width(0)
    button.set_fill_color(Color(0, 0, 0, 0))
    button.set_stroke_color(Color(0, 0, 0, 0))
    # 移除按钮的文本标签(如果有的话)
    button.set_caption("")
    # 添加按钮到页面
    page.add_annotation(button)
    return button

# 获取第一页
page: Page = doc.get_page(0)
button_rect = Rectangle(64, 130, 146-64, 167-130)
target_url = "www.google.be"

# 添加隐藏JS按钮
add_hidden_js_button(page, target_url, button_rect)

# 保存PDF
with open("modified_path_with_js_button.pdf", "wb") as output_file:
    PDF.dumps(output_file, doc)

print("PDF处理完成")

关键注意事项

  • 必须使用完整的URL(带http://或https://),否则PDF阅读器无法正确解析跳转地址
  • 保存文件时建议使用新的文件名,避免覆盖原文件
  • 部分PDF阅读器可能限制JavaScript执行,若使用方案2,需要确保阅读器允许JS运行

内容的提问来源于stack exchange,提问作者Pieter-Jan Roex

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 20:03:22