4

当段落长度对于 ColumnText 的宽度而言太长时,如何减少换行符的高度?

我已经尝试了以下方法,因为我看到了其他问题的答案:

p.Leading = 0

但这并没有产生任何影响。

我还尝试增加Leading100查看是否添加了更大的换行符,但都不起作用。

SpacingBefore/SpacingAfter 也无济于事:

p.SpacingBefore = 0
p.SpacingAfter = 0

我怎样才能减少这种情况?

4

2 回答 2

8

使用表格时,您需要在单元格本身上设置前导。但是,您会看到该Leading属性是只读的,因此您需要使用SetLeading()采用两个值的方法,第一个是固定前导,第二个是相乘前导。根据这里的这篇文章

相乘基本上意味着,字体越大,前导越大。固定意味着任何字体大小的前导相同。

要将领先优势缩小到 80%,您可以使用:

Dim P1 As New Paragraph("It was the best of times, it was the worst of times")
Dim C1 As New PdfPCell(P1)
C1.SetLeading(0, 0.8)

编辑

抱歉,我看到了“专栏”,我没喝咖啡的大脑去了桌子。

对于 aColumnText你应该能够很好地使用段落的主要值。

Dim cb = writer.DirectContent
Dim ct As New ColumnText(cb)

ct.SetSimpleColumn(0, 0, 200, 200)
Dim P1 As New Paragraph("It was the best of times, it was the worst of times")
''//Disable fixed leading
P1.Leading = 0
''//Set a font-relative leading
P1.MultipliedLeading = 0.8
ct.AddElement(P1)
ct.Go()

在我运行 iTextSharp 5.1.2.0 的机器上,这会产生两行稍微挤在一起的文本。

于 2012-02-01T13:59:45.613 回答
4

好吧,您似乎偶然发现了文本模式复合模式之间的区别:

  • 文本模式 =>ColumnText.AddText()使用“内联”块和短语对象调用。
  • 复合模式 =>ColumnText.AddText()使用“容器”对象(如段落、图像等)调用

当您处于文本模式ColumnText时,您可以通过设置属性在“段落”之间添加空格。

当您处于复合模式时,您可以像往常一样在“容器”对象之间添加空间 - 即如果不使用ColumnText.

这是一个显示两种模式之间差异的示例:

int status = 0;
string paragraph ="iText ® is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.";
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  ColumnText ct = new ColumnText(writer.DirectContent);
  ct.SetSimpleColumn(36, 36, 400, 792);
/*
 * "composite mode"; use AddElement() with "container" objects
 * like Paragraph, Image, etc
 */
  for (int i = 0; i < 4; ++i) {
    Paragraph p = new Paragraph(paragraph);
// space between paragraphs
    p.SpacingAfter = 0;
    ct.AddElement(p);
    status = ct.Go();
  }

/*
 * "text mode"; use AddText() with the "inline" Chunk and Phrase objects
 */
  document.NewPage();
  status = 0;
  ct = new ColumnText(writer.DirectContent);
  for (int i = 0; i < 4; ++i) {
    ct.AddText(new Phrase(paragraph));
// Chunk and Phrase are "inline"; explicitly add newline/break
    ct.AddText(Chunk.NEWLINE);
  }
// set space between "paragraphs" on the ColumnText object!
  ct.ExtraParagraphSpace = 6;
  while (ColumnText.HasMoreText(status)) {
    ct.SetSimpleColumn(36, 36, 400, 792);
    status = ct.Go();
  }  
}

因此,现在您已经更新了代码并使用了复合模式AddElement()p.SpacingAfter = 0 删除段落之间的间距。或将其设置为您想要的任何内容,而不是Paragraph.Leading.

于 2012-02-01T18:00:24.750 回答