减少 iTextSharp 上的段落换行高度

发布于 2025-01-02 03:59:47 字数 354 浏览 2 评论 0原文

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

我已经尝试了以下操作,因为我看到了其他回答此问题的问题:

p.Leading = 0

但这没有产生任何影响。

我还尝试将 Leading 增加到 100 以查看是否添加了更大的换行符,但这两种方法都不起作用。

SpacingBefore/SpacingAfter 也没有帮助:

p.SpacingBefore = 0
p.SpacingAfter = 0

我怎样才能减少这个?

How can I reduce the height of a line break which occurs when a paragraph length is too long for the width of the ColumnText?

I've tried the following, as I've seen other questions which answered with this:

p.Leading = 0

But this has made no affect.

I've also tried increasing the Leading to 100 to see if a larger line break is added, but neither work.

SpacingBefore/SpacingAfter doesn't help either:

p.SpacingBefore = 0
p.SpacingAfter = 0

How can I reduce this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆消瘦 2025-01-09 03:59:48

使用表格时,您需要设置单元格本身的行距。但是,您会看到 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)

编辑

抱歉,我看到“Column”,我的咖啡缺乏大脑去了桌子。

对于ColumnText,您应该能够很好地使用段落的前导值。

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 的机器上,这会生成两行稍微挤压在一起的文本。

When using a table, you need to set the leading on the cell itself. However, you'll see that the Leading property is read-only so instead you'll need to use the SetLeading() method which takes two values, the first is the fixed leading and the second is the multiplied leading. According to this post here:

Multiplied basically means, the larger the font, the larger the leading. Fixed means the same leading for any font size.

To shrink the leading to 80% you'd use:

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)

EDIT

Sorry, I saw "Column" and my coffee-lacking brain went to tables.

For a ColumnText you should be able to use the Paragraph's leading values just fine.

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()

On my machine running iTextSharp 5.1.2.0 this produces two lines of text that are slightly squished together.

梦里南柯 2025-01-09 03:59:48

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

  • 文本模式=>使用“内联”Chunk 和 Phrase 对象调用 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

Well it seems you've stumbled upon the difference between text mode and composite mode:

  • text mode => call ColumnText.AddText() using the "inline" Chunk and Phrase objects.
  • composite mode => call ColumnText.AddText() using "container" objects like Paragraph, Image, etc

When you're in text mode, you add space between "paragraphs" by setting ColumnText properties.

When you're in composite mode, you add space between "container" objects as you normally would - i.e. as you would if not using ColumnText.

Here's an example to show the difference between the two modes::

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();
  }  
}

So now that you've updated your code and are using composite mode with AddElement(), p.SpacingAfter = 0 WILL remove spacing between paragraphs. Or set it to whatever you want instead of Paragraph.Leading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文