如何向表格单元格内的元素添加注释

问题描述 投票:0回答:1

我是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')
            
            # Add section title
            toc_table.add(
                TableCell(
                    Paragraph(
                        f'Section {section_index}: {section_title}',
                        font_size=Decimal(11),
                        padding_top=Decimal(5),
                        padding_bottom=Decimal(5)
                    )
                )
            )
            
            # Add page number
            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)

我尝试将链接注释添加到 toc_table 布局元素中,但它给了我一个关于

'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)
))
python pdf borb
1个回答
0
投票

免责声明:我是

borb

的作者

您可以尝试访问

LayoutElement
内的
TableCell
吗?
这可能有用。这是一个私有财产
_layout_element

© www.soinside.com 2019 - 2024. All rights reserved.