如何在单元格itext 7中拟合文本 [英] How to fit the text in a cell itext 7
问题描述
仅供参考
如何通过使用带有itext 7库的Cell来获取上述内容
How to get the content as above, by using Cell with itext 7 library
我使用 cell.setPadding(0f);
但它没有帮助我按预期得到结果。
I used the cell.setPadding(0f);
but it didn't helped me to get the results as expected.
推荐答案
您似乎忘记将要添加的内容的前导设置为 Cell
。我试图模仿您分享的屏幕截图:
It seems that you forgot to set the leading of the content you are adding to the Cell
. I tried to mimic the screen shot you shared:
这是我以前使用的代码:
This is the code I used to do so:
public void createPdf(String dest) throws IOException {
// step 1
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));
pdfDocument.setDefaultPageSize(PageSize.A4.rotate());
// step 2
Document document = new Document(pdfDocument);
// step 3
Table table = new Table(new float[]{ 50 , 50 });
table.setWidthPercent(30);
table.addCell(new Cell().setPadding(0).setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("4.0").setMultipliedLeading(1.2f).setItalic()));
table.addCell(new Cell().setPadding(0).setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("0.14").setMultipliedLeading(1.2f).setItalic()));
document.add(table);
// step 4
document.close();
}
在这种情况下,我使用了乘以的前导1.2F
。随意更改该值。
In this case, I used a multiplied leading of 1.2f
. Feel free to change that value.
注意:非常烦人的是您无法在父级上设置级别的值(例如表
,文档
,...)。我已经创建了关于JIRA的功能请求,希望能够在下一个版本。
Note: it is very annoying that you can't set the value of the level on a parent (e.g. the table
, the document
,...). I have created a feature request on JIRA in the hope that this will be supported in the next version.
这篇关于如何在单元格itext 7中拟合文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!