Get the font height of a character in PDFBox

Simon Bengtsson picture Simon Bengtsson · Jun 18, 2013 · Viewed 11.8k times · Source

There is a method in PDFBox's font class, PDFont, named getFontHeight which sounds simple enough. However I don't quite understand the documentation and what the parameters stand for.

getFontHeight This will get the font width for a character.

Parameters:

  • c - The character code to get the width for.
  • offset - The offset into the array. length
  • The length of the data.

Returns: The width is in 1000 unit of text space, ie 333 or 777

Is this method the right one to use to get the height of a character in PDFBox and if so how? Is it some kind of relationship between font height and font size I can use instead?

Answer

engilyin picture engilyin · Feb 3, 2017

I believe the answer marked right requires some additional clarification. There are no "error" per font for getHeight() and hence I believe it is not a good practice manually guessing the coefficient for each new font. Guess it could be nice for your purposes simply use CapHeight instead of Height.

float height = ( font.getFontDescriptor().getCapHeight()) / 1000 * fontSize;

That will return the value similar to what you are trying to get by correcting the Height with 0.865 for Helvetica. But it will be universal for any font.

PDFBox docs do not explain too much what is it. But you can look at the image in the wikipedia Cap_height article to understand better how it is working and choose the parameter fit to your particular task.

https://en.wikipedia.org/wiki/Cap_height