C# iTextSharp-段落行高

C# iTextSharp-段落行高,c#,pdf,pdf-generation,itextsharp,typography,C#,Pdf,Pdf Generation,Itextsharp,Typography,我目前正在制作PDF,但我在尝试增加段落的行距时遇到了一些问题,这是我现在拥有的代码: var tempTable = new PdfPTable(1); cell = new PdfPCell(new Paragraph("My Account", GetInformationalTitle())); cell.Border = Rectangle.NO_BORDER; tempTable.AddCell(cell); cell = new PdfPCell(new Paragraph("

我目前正在制作PDF,但我在尝试增加
段落的行距时遇到了一些问题,这是我现在拥有的代码:

var tempTable = new PdfPTable(1);

cell = new PdfPCell(new Paragraph("My Account", GetInformationalTitle()));
cell.Border = Rectangle.NO_BORDER;
tempTable.AddCell(cell);

cell = new PdfPCell(new Paragraph("http://www.google.com/", GetInformationalOblique()));
cell.Border = Rectangle.NO_BORDER;
cell.PaddingBottom = 10f;
tempTable.AddCell(cell);

var para = new Paragraph("Login to 'My Account' to access detailed information about this order. " +
"You can also change your email address, payment settings, print invoices & much more.", GetInformationalContent());
 para.SetLeading(0f, 2f);

 cell = new PdfPCell(para);
 cell.Border = Rectangle.NO_BORDER;
 tempTable.AddCell(cell);
从上面可以看到,我试图增加
para
的行高,我尝试了
para.SetLeading(0f,2f)
,但它仍然没有增加行高或行距,这就是所谓的行距


这里可能有什么问题?

您正在文本模式下添加
para
,而不是在复合模式下添加它。文本模式意味着
PdfPCell
的前导将优先于为
段落定义的前导。对于复合模式,情况正好相反

您可以通过一个小改动来解决此问题:

cell = new PdfPCell();
cell.addElement(para);
tempTable.AddCell(cell);

使用
addElement()
方法使
cell
从文本模式切换到复合模式。

这个问题不是完全重复的,但答案是一样的:您是在文本模式下添加
段落,而不是在复合模式下添加它。在您的例子中,
PdfPCell
的前导优先于
段落的前导(正如您所注意到的,在文本模式中前导被忽略)。太棒了!我通过使用
AddElement(para)
修复了它,而不是在构造函数中初始化它。你可能想把它贴在一个答案中,这样我就可以把它标记为已回答,再一次,谢谢!