【问题标题】:How can I increase the spacing within a paragraph with iTextSharp?如何使用 iTextSharp 增加段落内的间距?
【发布时间】:2015-06-23 14:30:21
【问题描述】:

来自this,我认为我可以通过执行以下操作之一来增加段落间距:

par.SetLeading(15, 2.5f); // doesn't do anything
par.SetLeading(0, 2);     // doesn't do anything

在上下文中:

Chunk boldpart = new Chunk("Important: ", helvetica9BoldRed);
Chunk ini = new Chunk("Form must be filled out in ", helvetica9Red);

Anchor anchor = new Anchor("Adobe Reader", LinkFont);
anchor.Reference = "http://www.adobe.com";

Chunk middlePart = new Chunk(" or Acrobat Professional 8.1 or above. To save completed forms, Acrobat Professional is required. For technical and accessibility assistance, contact the ", helvetica9Red);

Anchor anchorCCO = new Anchor("Campus Controller's Office", LinkFont);
anchor.Reference = "mailto:dplatypus@ucsc.edu";

PdfPTable tbl = new PdfPTable(1);
tbl.WidthPercentage = 55;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
//par.SetLeading(15, 2.5f); // doesn't do anything
par.SetLeading(0, 2);       // doesn't do anything

par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);                          
doc.Add(tbl);

正如 cmets 所指出的,两者都不做任何事情。这是我看到的:

如何增加段落中的行间距?

更新

当我尝试这样做时(响应答案):

PdfPCell chunky = new PdfPCell();
chunky.AddCell(par);

我明白了,“'iTextSharp.text.pdf.PdfPCell' 不包含'AddCell' 的定义,并且没有扩展方法'AddCell' 接受'iTextSharp.text.pdf.PdfPCell' 类型的第一个参数可以找到"

【问题讨论】:

  • AddCell - 我认为它只是Add
  • 原来是AddElement()
  • ;) 好的,几乎正确...

标签: pdf-generation itextsharp paragraph


【解决方案1】:

您的问题与 StackOverflow 上已回答的许多问题重复。以下是其中一些:

还有很多,但上面提到的那些是为免费电子书The Best iText Questions on StackOverflow 选择的,这本书回答了您在过去一周提出的许多问题。

当你这样做时:

PdfPCell chunky = new PdfPCell(par);

您不会将par 视为ParagraphPdfPCellpar 视为PhrasePhraseParagraph 的超类)。正如许多人喜欢的那样,您正在文本模式中创建PdfPCell文本模式意味着所有在段落级别定义的属性都被忽略,有利于单元格级别的属性。

换句话说:如果您为par 定义leading,它将被忽略。相反,将使用chunkyleading。这是基于经验做出的设计选择:当您主要对添加文本感兴趣时,在单元格级别定义属性更有意义。

在某些情况下,您不想在单元格级别定义属性。例如:也许您有一个单元格,其内容由不同的 Paragraph 对象组成,这些对象具有不同的前导、对齐等...

在这种情况下,您将从文本模式切换到复合模式。你可以这样做:

PdfPCell chunky = new PdfPCell();
chunky.AddCell(par);

在单元格级别定义的前导和对齐等属性将被忽略。相反,parleadingalignment 将被考虑(并且par 将被视为真正的Paragraph,而不是Phrase)。

所有这些都在iText in ActionThe Best iText Questions on StackOverflow 和许多其他地方进行了解释。

【讨论】:

    【解决方案2】:

    这行得通:

    var par = new Paragraph();
    par.SetLeading(0, 1.2f);
    par.Add(boldpart);
    par.Add(ini);
    par.Add(anchor);
    par.Add(middlePart);
    par.Add(anchorCCO);
    PdfPCell chunky = new PdfPCell();
    chunky.AddElement(par);
    chunky.BorderWidth = PdfPCell.NO_BORDER;
    tbl.AddCell(chunky);                          
    

    根据口味增加或减少“1.2f”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 2015-06-13
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多