Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

java - How to maintain indentation if a paragraph takes new line in 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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

This is the result:

enter image description here

In the first row, we no longer have a Paragraph, we have a Phrase that uses the alignment and the leading of the 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...