【发布时间】:2014-10-13 22:02:11
【问题描述】:
问题设置
我正在尝试准确确定字体的宽度(Ubuntu 斜体),尽管 iText 似乎忽略了水平前进后最后一个字形的部分,如下图所示。
我用来生成这个例子的代码如下:
Document document = new Document(PageSize.LETTER);
FileOutputStream out = new FileOutputStream(new File("test.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
String text = "ff";
Chunk chunk = new Chunk(text, FontFactory.getFont("Ubuntu-Italic.ttf", 72)
Phrase phrase = new Phrase(chunk);
float width = ColumnText.getWidth(phrase);
System.out.println(width + ", " + chunk.getWidthPoint());
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
table.setTotalWidth(width);
PdfPCell cell = table.getDefaultCell();
cell.setPadding(0);
cell.setUseDescender(true);
cell.setUseAscender(true);
table.addCell(phrase);
float height = table.calculateHeights();
PdfContentByte canvas = writer.getDirectContent();
ColumnText columnText = new ColumnText(canvas);
columnText.setSimpleColumn(36, 756 - height, 36 + width, 756);
columnText.addElement(table);
columnText.go();
document.close();
out.close();
如代码所示,我尝试了ColumnText.getWidth(phrase) 和chunk.getWidthPoint(),它们都返回相同的值,但有一点浮点差异。
问题
我上面写的代码模拟了 iText 中文本没有正确换行到下一行的情况。我遇到的问题是我正在使用的代码中的 ColumnText 被裁剪了。问题在于,由于 iText 测量文本的方式,ColumnText 认为右侧边缘有足够的空间容纳f,而实际上实际上没有,所以在我的情况下它被切断了。有没有办法强制 ColumnText 以不同的方式测量字体的宽度,以免发生这种情况?
【问题讨论】: