Reduce paragraph line break height on iTextSharp

霸气de小男生 提交于 2019-12-20 01:39:10

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9094461/reduce-paragraph-line-break-height-on-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!