如果段落在PdfPCell中占用新行,如何保持缩进? [英] How to maintain indentation if a paragraph takes new line in PdfPCell?

查看:1199
本文介绍了如果段落在PdfPCell中占用新行,如何保持缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Some of my paragraph objects contain text that can be greater than the width of the cell in length. These paragraphs are also indented, but the problem is if a paragraph needs to take a new line due to text length, the indenting is not maintained on the new line, causing most of the text to be indented, then the remainder of the text containing no indenting on the new line.

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

Is there a way to determine if a paragraph will take a new line due to exceeding PdfPCell width, and to indent the remainder of the text if this is true?

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);但这并没有固定的问题。

I have also tried indenting the paragraph object as well using p1.setIndentationLeft(20); but this hasn't fixed the issue

推荐答案

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

Please take a look at the SimpleTable4 example. In this example, I add an indented paragraph to a cell the wrong way (your way) and I add a paragraph to a cell the correct way (as explained in the documentation):

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);

结果如下:

< img src =https://i.stack.imgur.com/q1lhc.pngalt =在此处输入图像说明>

在第一行,我们不再有段落,我们有短语,它使用对齐和 PdfPCell 。

In the first row, we no longer have a Paragraph, we have a Phrase that uses the alignment and the leading of the PdfPCell.

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

In the second row, the Paragraph is preserved. If an alignment or a leading was defined at the level of the PdfPCell, it is ignored in favor of the alignment and the leading of the Paragraph. All the other properties that are defined at the level of the Paragraph (and that do not exist for Phrase objects) are preserved.

这篇关于如果段落在PdfPCell中占用新行,如何保持缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆