如果段落在 PdfPCell 中换行,如何保持缩进?

How to maintain indentation if a paragraph takes new line in PdfPCell?

我的某些段落对象包含的文本长度可能大于单元格的宽度。这些段落也是缩进的,但问题是如果一个段落由于文本长度需要换行,换行时没有保持缩进,导致大部分文本都缩进了,那么剩下的文本包含新行不缩进。

有没有办法确定一个段落是否会因为超过 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) 缩进段落对象;但这并没有解决问题

请查看 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);

这是结果:

在第一行,我们不再有 Paragraph,而是使用 PdfPCell 的对齐和行距的 Phrase

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