当段落长度超过ColumnText的宽度时,如何减少换行的高度?
我尝试了以下内容,因为我看到了其他问题的答案:
p.Leading = 0
但这并没有产生任何影响。
我还尝试将Leading
增加到100
,看看是否添加了更大的换行符,但都不起作用。
SpacingBefore/SspacingAfter也没有帮助:
p.SpacingBefore = 0
p.SpacingAfter = 0
我该如何减少?
使用表时,需要在单元格本身设置前导。但是,您会看到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)
编辑
对不起,我看到了"专栏",我缺咖啡的大脑都到桌子上了。
对于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的机器上,这会产生两行稍微挤在一起的文本。
您似乎偶然发现了文本模式和复合模式:之间的区别
- text mode=>使用"inline"Chunk和Phrase对象调用
ColumnText.AddText()
- composite mode=>使用Paragraph、Image等"容器"对象调用
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
。