java 在单行中对齐 iText 中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13130407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:36:13  来源:igfitidea点击:

Aligning text in iText in a single row

javaitext

提问by Karlovsky120

What is the best way to create a row of text that will have two elements aligned to an imaginary line? Like this (four rows given to illustrate the point better):

创建一行文本的最佳方法是什么,其中两个元素与一条假想线对齐?像这样(给出四行以更好地说明这一点):

   1. some random text
  34. some more random text
 764. here's even more random text
4594. it just never ends

Imaginary line would go trough the dots, or the space after them. The numbers have right alignment, and the text has left alignment.

假想线将穿过点,或它们之后的空间。数字右对齐,文本左对齐。

I do not want to use a list because elements may not be in order, and it has certain limitation with setting the line spacing.

我不想使用列表,因为元素可能不按顺序排列,并且在设置行距方面有一定的限制。

回答by Pier Luigi

You can use a PdfPTable with 2 columns, the first right aligned and the last left aligned. Then set the designer padding on the cells content. For example:

您可以使用具有 2 列的 PdfPTable,第一列右对齐,最后一列左对齐。然后在单元格内容上设置设计器填充。例如:

PdfPTable tbl = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Phrase("1."));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("some random text"));
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("34."));
cell.disableBorderSide(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("some more random text"));
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);

You can see that cell border are disabled (disableBorderSidemethod). You can also adjust the minimum height of cells using setMinimumHeightmethod.

您可以看到单元格边框已禁用(disableBorderSide方法)。您还可以使用setMinimumHeight方法调整单元格的最小高度。