如何为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
相关产品推荐
相关产品推荐

