获取PDFBox中字符的字体高度 [英] Get the font height of a character in PDFBox

查看:380
本文介绍了获取PDFBox中字符的字体高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PDFBox的字体类PDFont中有一个名为getFontHeight的方法,听起来很简单.但是,我不太了解文档及其参数的含义.

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 这将获取字符的字体宽度.

getFontHeight This will get the font width for a character.

参数:

  • c-要获取宽度的字符代码.
  • offset-数组的偏移量.长度
  • 数据长度.
  • c - The character code to get the width for.
  • offset - The offset into the array. length
  • The length of the data.

返回:宽度以1000个文本空间为单位,即333或777

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

此方法是用于获取PDFBox中字符高度的正确方法吗?我可以改用字体高度和字体大小之间的某种关系吗?

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?

推荐答案

我认为标记为正确的答案需要进一步说明. getHeight()的每种字体都没有错误",因此我认为手动猜测每种新字体的系数不是一个好习惯. 猜猜它可能对您的目的很好,只需使用CapHeight而不是Height.

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;

这将返回与您尝试通过将Helvetica的Height校正为0.865所获得的值相似的值.但这对于任何字体都是通用的.

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文档没有太多解释.但是您可以查看Wikipedia Cap_height文章中的图像,以更好地了解其工作原理,并选择适合您特定任务的参数.

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

这篇关于获取PDFBox中字符的字体高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆