Calculation String-Width in PDFbox seems only to count characters?

那年仲夏 提交于 2019-12-23 07:29:22

问题


I've the following issue. I try to calculate the width of a string in PDFbox to center it in a rectangle. Now my problem is, that I get the same width for 12 as for 32, but the 1 is smaller than the 3 so that my text is for 12 not centered. How can I solve this issue?

Thanks!


回答1:


From this question:

PDFont font = PDType1Font.HELVETICA_BOLD; // Or whatever font you want.
int fontSize = 16; // Or whatever font size you want.
float width = font.getStringWidth(text.substring(start,i)) / 1000 * fontSize;



回答2:


In some fonts the numbers do all have the same width. Just try it for different fonts in MSWord for example. So the fact that you get the same width for 12 and 32 could be correct.



来源:https://stackoverflow.com/questions/13701017/calculation-string-width-in-pdfbox-seems-only-to-count-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!