Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

pdfbox

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!

like image 442
Gerrit Avatar asked Dec 04 '12 10:12

Gerrit


1 Answers

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;
like image 151
Lukas Avatar answered Oct 10 '22 19:10

Lukas