java 获取PDFBox中字符的字体高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17171815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:11:50  来源:igfitidea点击:

Get the font height of a character in PDFBox

javafontsfont-sizepdfbox

提问by Simon Bengtsson

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.

PDFBox 的字体类 PDFont 中有一个方法,名为 getFontHeight,听起来很简单。但是我不太了解文档和参数代表什么。

getFontHeightThis 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

getFontHeight这将获得字符的字体宽度。

参数:

  • c - 要获取宽度的字符代码。
  • offset - 数组中的偏移量。长度
  • 数据的长度。

返回:宽度以1000个文本空间为单位,即333或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?

这种方法是否适合用于获取 PDFBox 中字符的高度,如果是,如何使用?我可以使用字体高度和字体大小之间的某种关系吗?

回答by engilyin

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.

我相信标记为正确的答案需要一些额外的说明。getHeight() 的每种字体都没有“错误”,因此我认为手动猜测每种新字体的系数不是一个好习惯。猜猜它可以很好地满足您的目的,只需使用 CapHeight 而不是 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.

这将返回与您尝试通过使用 0.865 为 Helvetica 校正高度来获得的值类似的值。但它对任何字体都是通用的。

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.

PDFBox 文档没有过多解释它是什么。但是您可以查看 wikipedia Cap_height 文章中的图像以更好地了解它的工作原理并选择适合您特定任务的参数。

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

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

回答by Simon Bengtsson

EDIT: Cap height was what I was looking for. See the accepted answer.

编辑:帽高是我一直在寻找的。请参阅已接受的答案。

After digging through the source of PDFBox I found that this should do the trick of calculating the font height.

在挖掘了 PDFBox 的源代码后,我发现这应该可以解决计算字体高度的问题。

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

The method isn't perfect though. If you draw a rectangle with the height 200 and a Y with the font size 200 you get the font height 231.2 calculated with the above method even though it actually is printed smaller then the rectangle.

虽然方法并不完美。如果您绘制一个高度为 200 的矩形和一个字体大小为 200 的 Y,您会得到使用上述方法计算出的字体高度 231.2,即使它实际上打印得比矩形小。

Every font has a different error but with helvetica it is close to 13.5 precent too much independently of font size. Therefore, to get the right font height for helvetica this works...

每种字体都有不同的错误,但对于 helvetica,它接近 13.5%,与字体大小无关。因此,要为 helvetica 获得正确的字体高度,这很有效……

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

回答by Manish Patel

Maybe use this?

也许用这个?

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

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

Seems like a wrap-around util for text. I haven't looked in the source if it accounts for font error though.

似乎是文本的环绕式实用程序。不过,如果它说明字体错误,我还没有查看源代码。

回答by Cugomastik

this is a working method for splitting the text and finding the 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;
}

回答by Clifford Dann

For imported True Type Fonts the total height of the font is

对于导入的 True Type 字体,字体的总高度为

(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)

(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 a 20% leading on the above value, but if you take the top value and remove 20%, the font will be right next too each other

你会发现它font.getFontDescriptor().getFontBoundingBox().getHeight()比上面的值大 20%,因为它包括在上面的值前面的 20%,但是如果你取最高值并删除 20%,字体将彼此相邻