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

标签 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); 缩进段落对象;但这并没有解决问题

最佳答案

请看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 对象不存在的属性)都将保留。

关于java - 如果段落在 PdfPCell 中换行,如何保持缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28073190/

相关文章:

spring - J2SE中 "shutdown" Spring 上下文的正确方法

java - 集成层如何与业务层交互?

带有 boolean 值的 java.lang.NullPointerException

svg - 如何使用 Itext 7 将 SVG 添加到 PDF?

java - iText 数据矩阵生成问题

android - itext android 印地语 pdf 创建

itext - 对齐 itextsharp 表

java - java中默认可以并发执行实例方法吗?

c# - 创建 PDF 服务器端并在新的浏览器窗口或选项卡中打开

Java Combo Box 和 MySql 数据库