Get character font height in PDFBox

There is a method in the PDFBox font class, PDFont, named getFontHeight, which sounds simple enough. However, I do not quite understand the documentation and what the parameters are worth.

getFontHeight This will get the font width for the character.

Options:

  • c - The code of the character to get the width.
  • offset - offset to the array. length
  • Data length.

Returns: Width is in 1000 units of text space, i.e. 333 or 777

Is this method correct to use the character height in a PDFBox, and if so, how? Is this some kind of connection between font growth and font size that I can use instead?

+6
source share
5 answers

I believe that the answer, indicated correctly, requires some further clarification. "No errors" for each font for getHeight (), and therefore I think this is not a good practice, manually guessing the coefficient for each new font. Guess this might be nice for your purposes, just use CapHeight instead of Height.

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

This will return a value similar to what you are trying to get by correcting the height from 0.865 for Helvetica. But it will be universal for any font.

PDFBox docs don't explain too much what it is. But you can see the image in the Cap_height wikipedia article to better understand how it works and choose the option that suits your specific task.

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

+6
source

EDIT: The lid height was what I was looking for. See Accepted Answer.

After you made your way through the PDFBox source, I found that this should do the trick of calculating the font height.

 int fontSize = 14; PDFont font = PDType1Font.HELVETICA; font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize 

The method is not perfect. If you draw a rectangle with a height of 200 and Y with a font size of 200, you will get a font height of 231.2 calculated using the method described above, even if it is actually printed smaller than the rectangle.

Each font has a different error, but with helvetica it is too close to 13.5 percent, regardless of font size. So to get the right font height for helvetica, this works ...

 font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize * 0.865 
+7
source

Maybe use this?

http://pdfbox.apache.org/apidocs/org/apache/pdfbox/util/TextPosition.html

It looks like using covers for text. I did not look at the source if it takes into account the font error.

0
source

this is a working method for separating text and finding height

 public float heightForWidth(float width) throws IOException { float height = 0; String[] split = getTxt().split("(?<=\\W)"); int[] possibleWrapPoints = new int[split.length]; possibleWrapPoints[0] = split[0].length(); for (int i = 1; i < split.length; i++) { possibleWrapPoints[i] = possibleWrapPoints[i - 1] + split[i].length(); } float leading = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; int start = 0; int end = 0; for (int i : possibleWrapPoints) { float w = font.getStringWidth(getTxt().substring(start, i)) / 1000 * fontSize; if (start < end && w > width) { height += leading; start = end; } end = i; } height += leading; return height + 3; } 
0
source

For imported True Type Fonts, the overall font height

(org.apache.pdfbox.pdmodel.font.PDFont.getFontDescriptor().getDescent() + org.apache.pdfbox.pdmodel.font.PDFont.getFontDescriptor().getAscent() + org.apache.pdfbox.pdmodel.font.PDFont.getFontDescriptor().getLeading()) * point size * org.apache.pdfbox.pdmodel.font.PDFont.getFontMatrix().getValue(0, 0)

You will find that font.getFontDescriptor().getFontBoundingBox().getHeight() is 20% larger than the above value, as it includes 20% leading to the above value, but if you take the top value and delete 20 %, the font will be on the right further also different

0
source

Source: https://habr.com/ru/post/947561/


All Articles