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

如何为borb PDF表格单元格内的元素添加链接注释?

使用borb库创建带跳转页码的PDF目录时的问题

我是Python新手,目前用borb PDF库生成PDF文档,想制作带可跳转至指定页面的页码的目录页,现有代码如下:

doc = Document()
toc_page = Page(*self.page_size)

doc.add_page(toc_page)

layout = SingleColumnLayoutWithOverflow(toc_page)
layout.vertical_margin = Decimal(0)
layout.horizontal_margin = Decimal(0)

toc_table = FixedColumnWidthTable(
    number_of_columns=2,
    number_of_rows=self.toc_row_count,
    column_widths=[Decimal(3), Decimal(1)],
)

section_index = 1

for section_title, value in self.toc_sections.items():
    page_number = value.get('page_number')
    
    # 添加章节标题
    toc_table.add(
        TableCell(
            Paragraph(
                f'Section {section_index}: {section_title}',
                font_size=Decimal(11),
                padding_top=Decimal(5),
                padding_bottom=Decimal(5)
            )
        )
    )
    
    # 添加页码
    page_num_p = Paragraph(
        f'{int(page_number)}',
        font_size=Decimal(11),
        text_alignment=Alignment.RIGHT,
        horizontal_alignment=Alignment.RIGHT,
        padding_top=Decimal(5),
        padding_bottom=Decimal(5)
    )

    toc_table.add(TableCell(page_num_p))
    section_index += 1

toc_table.no_borders()
layout.add(toc_table)

尝试给表格单元格添加LinkAnnotation时,出现错误:'NoneType' object has no attribute 'get_x',出错代码片段如下:

toc_page_num_cell = TableCell(page_num_p)
toc_table.add(toc_page_num_cell)

toc_table.add(LinkAnnotation(
   toc_page_num_cell.get_previous_layout_box(),
   page=Decimal(page_number),
   destination_type=DestinationType.X_Y_Z,
   top=Decimal(0),
   left=Decimal(0),
   zoom=Decimal(1)
))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 10:02:35