itext如何在ColumnText中添加章节标题?
请问,我如何才能在专栏文本中添加章节标题? 我需要这样制作PDF:
| ColumnText column1 | ColumnText column2 |
| PdfPTable with content | PdfPTable with content |
| | Chapter 2 title |
| Chapter 1 title | |
然后将TOC添加到此文档中。 我用列文本和表格制作文档。但无法在表中添加章节。 我只能将章节添加到文档正文中,但在本例中,章节标题不在ColumnText中
# 1 楼答案
你的问题并不清楚,因为你没有告诉我们你是否想要这样的TOC:
如果是这种情况,则说明您使用了错误的术语,因为您在“书签”面板中看到的内容可能被称为大纲或书签
如果你说你想要TOC,你想要这样的东西:
我提到这两个,因为您谈到
Chapter
(一个您不应该再使用的类),该类创建书签/大纲,而不是TOC我已经创建了一个PDF文件,其中既有书签,也有TOC:columns_with_toc.pdf。请看一下CreateTOCinColumn示例,了解它是如何完成的
与您一样,我创建了一个带有标题和表格的
ColumnText
对象:结果如下所示:
尽管有在StackOverflow上发布问题的规则,但您没有发布代码示例,但您的代码和我的代码至少有一个区别:
在这一行中,我们声明一个泛型标记。该标记由
TOCEntry
类使用,如下所示:如您所见,我们根据标题的位置创建一个
PdfDestination
:如果需要书签,可以创建如下
PdfOutline
:如果需要TOC,可以将
String
和PdfAction
存储在List
中:现在我们已经了解了
TOCCreation
类,我们来看看如何使用它:我们创建一个
event
对象,将其传递给writer
,打开文档后,将大纲树的根传递给事件。书签将自动创建,TOC不会。如果要添加TOC,您需要以下内容:现在您有了一个标题列表,可以单击该列表跳转到相应的表