iTextSharp) - 将节标题与文本的第一行保持在一起会导致令人不快的书签

iText(Sharp) - Keeping section heading with first line of text causes unpleasant bookmarks

我正在使用章节 objects 创建 PDF 文档,所以我得到了漂亮的 tree-structure 书签,这很好,但我还想将 "keep with next" 应用到章节和章节标题,这样标题后的第一段就不会被推送到与标题不同的页面。

我通过用一个段落定义每个部分来做到这一点:

void AddSection(Section parentSection, string newSectionTitle)
{
   m_heading = new Paragraph(new Chunk(newSectionTitle));
   m_section = parentSection.AddSection(indentation, m_heading);
}

然后当我将第一个块添加到该部分时,我将其添加到该标题:

void AddTextToSection(string text)
{
   if (m_heading != null)
   {
      m_heading.Add(new Chunk("\n"));
      m_heading.Add(new Chunk(text));
      m_heading = null;
   }
   else
   {
      m_section.Add(new Chunk(text));
   }
}

除了书签包含标题和第一段外,效果很好。

有没有办法告诉章节或部分它应该在文档的 body 中显示文本 X,但使用文本 Y 定义书签?

原来 Section 有一个 属性 名为 BookmarkTitle 的 BookmarkTitle,如果设置它,指定将用于书签标题的文本。

因此,在我创建该部分之后,但在我开始向其中添加文本之前(通过我的 AddTextToSection() 函数),我从 m_section.Title.Chunks 中提取文本并将其设置为 m_section.BookmarkTitle。