iText5
我正在使用该库生成一个 PDF 文档(对于那些以前看过他的人,是的,仍然如此) 。赞扬布鲁诺·洛瓦吉;我发现它是一个非常棒的 API!
但是,我的部分任务是格式化一些短语。我使用嵌套PdfPTable
来格式化数据,但我遇到了奇怪的间距问题。这是我面临的问题的一个例子:
正如你所看到的,只有几个短语的行之间往往有很大的差距。我能理解为什么会这样;上面的行正在拉伸表格。有没有办法只使桌子尽可能大,而不是更大?
代码
生成价格集合
我正在使用此代码段创建价格:
Paragraph pricePara = new Paragraph();
pricePara.add(price);
pricePara.add(generateBreakLine());
pricePara.add(time);
pricePara.add(generateBreakLine());
allPrices.add(pricePara);
wheregenerateBreakLine()
返回一个空Paragraph
对象。
将它们添加到单元格中
我使用以下代码段将它们添加到单元格中:
// 5 is just the amount of prices we can fit on one line.
PdfPTable pricesCellValue = new PdfPTable(elements.size() > 5 ? 5 : elements.size());
// Make it appear that the prices are just horizontal, as opposed to in a table.
pricesCellValue.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
// Cycle through each price. Add to cell. Add cell to the table.
for (com.lowagie.text.Element elem : elements) {
PdfPCell cell = new PdfPCell((Paragraph) elem);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setPadding(2);
pricesCellValue.addCell(cell);
}
return pricesCellValue;
我相信,上面的片段是我可以确保桌子只有它需要的宽度的地方,而不是填满它周围的所有空间。我该怎么做呢?