如果段落在PdfPCell中占用新行,如何保持缩进?

时间:2015-01-21 17:22:26

标签: java itext

我的某些段落对象包含的文本长度可能大于单元格的宽度。这些段落也是缩进的,但问题是如果段落由于文本长度需要采用新行,则不会在新行上保留缩进,导致大部分文本缩进,然后包含文本的其余部分没有在新线上缩进。

有没有办法确定一个段落是否因为超过PdfPCell宽度而采用新行,如果这是真的则缩进文本的其余部分?

Paragraph p1 = new Paragraph("some text that exceeds cell width", mCustomFont);

PdfPCell c1 = new PdfPCell(p1);
c1.setIndent(20);

PdfPTable t1 = new PdfPTable();
// various styling on t1
t1.addCell(c1);

编辑:我也尝试使用p1.setIndentationLeft(20)缩进段落对象;但这还没有解决问题

1 个答案:

答案 0 :(得分:2)

请查看SimpleTable4示例。在这个例子中,我以错误的方式(你的方式)向一个单元格添加一个缩进的段落,并以正确的方式向一个单元格添加一个段落(如文档中所述):

PdfPTable table = new PdfPTable(1);
Paragraph wrong = new Paragraph("This is wrong, because an object that was originally a paragraph is reduced to a phrase due to the fact that it's put into a cell that uses text mode.");
wrong.setIndentationLeft(20);
PdfPCell wrongCell = new PdfPCell(wrong);
table.addCell(wrongCell);
Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");
right.setIndentationLeft(20);
PdfPCell rightCell = new PdfPCell();
rightCell.addElement(right);
table.addCell(rightCell);
document.add(table);

结果如下:

enter image description here

在第一行中,我们不再有Paragraph,我们有Phrase使用PdfPCell的对齐和前导。

在第二行中,保留Paragraph。如果在PdfPCell的级别定义了对齐或前导,则会忽略它,以支持Paragraph的对齐和前导。在Paragraph级别定义的所有其他属性(以及Phrase个对象不存在的属性)将被保留。