7

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

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

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

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

4

1 回答 1

18

您可以使用具有 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);

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

于 2012-10-30T06:49:15.053 回答