【问题标题】:Reduce paragraph line break height on iTextSharp减少 iTextSharp 上的段落换行符高度
【发布时间】:2012-02-24 01:15:59
【问题描述】:

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

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

p.Leading = 0

但这没有任何影响。

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

SpacingBefore/SpacingAfter 也无济于事:

p.SpacingBefore = 0
p.SpacingAfter = 0

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

【问题讨论】:

    标签: itextsharp line-breaks paragraph


    【解决方案1】:

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

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

    要将领先优势缩小到 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)
    

    编辑

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

    对于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 的机器上,这会产生两行文本,它们会稍微挤在一起。

    【讨论】:

    • 为您的回复干杯,但我没有使用PdfCell。该段落正在添加到ColumnText
    • 干杯克里斯,但这仍然对我不起作用:/我也在使用 5.1.2.0,所以我对它为什么不起作用感到有点困惑。还有其他想法吗?
    • 嗯,我已经复制了您的代码,并且可以看到它被压扁了,而将它添加到我的代码中时它不起作用。嗯,我得看看我做错了什么。
    • 啊,你使用的是AddElement,而我一直在使用AddText。我已经改了,加油!有用!唯一的问题是,这似乎弄乱了ct.YLine 的值,我用它来在它下面放置另一个列文本。
    • 其实我觉得这不是问题。显然,如果我猜领导改变了,YLine 将会改变。克里斯干杯,帮了大忙!
    【解决方案2】:

    看来您已经发现了文本模式复合模式之间的区别:

    • 文本模式 => 使用“内联”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

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 2016-07-26
      • 1970-01-01
      • 2013-09-14
      • 2020-01-23
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多