问题描述
我正在按XML数据生成pdf文件。
我将段落元素的高度计算为:
float paraWidth = 0.0f;
for(Object o:el.getChunks()){
paraWidth + =((Chunk)o)。getWidthPoint();
}
float paraHeight = paraWidth / PageSize.A4.getWidth();
但此方法无效。
你的问题很奇怪。根据你问题的标题,你想知道字符串的高度,但你的代码显示你要求字符串的宽度。
请带查看示例。
如果 bf
是一个 BaseFont
实例,那么你可以使用:
float ascent = bf.getAscentPoint(Some String,12);
float descent = bf.getDescentPoint(Some String,12);
当我们使用字体大小时,这将返回高于基线的高度和高于基线的高度您可能知道,字体大小表示平均高度。这不是实际的高度。这只是我们合作的一个数字。
总高度将是:
浮动高度=上升 - 下降;
或许你想知道段落的行数
并将其乘以前导
。在这种情况下,有不同的可能性。由于你的问题不清楚你想要什么(块的高度,块的宽度,基线的垂直位置,......),你将得到比已经给出的更好的答案。如果Chunk中的字形高度不符合您的预期,请重新解释您的问题。
I am generating pdf files by XML data.
I calculate the height of a paragraph element as :
float paraWidth = 0.0f;
for (Object o : el.getChunks()) {
paraWidth += ((Chunk) o).getWidthPoint();
}
float paraHeight = paraWidth/PageSize.A4.getWidth();
But this method does not works correctly.
Can you give me an idea?
Your question is strange. According to the header of your question, you want to know the height of a string, but your code shows that you are asking for the width of a String.
Please take a look at the FoobarFilmFestival example.
If bf
is a BaseFont
instance, then you can use:
float ascent = bf.getAscentPoint("Some String", 12);
float descent = bf.getDescentPoint("Some String", 12);
This will return the height above the baseline and the height below the baseline, when we use a font size of 12. As you probably know, the font size is an indication of the average height. It's not the actual height. It's just a number we work with.
The total height will be:
float height = ascent - descent;
Or maybe you want to know the number of lines taken by a Paragraph
and multiply that with the leading
. In that case, there are different possibilities. As it's not clear from your question what you want (height of chunks, width of chunks, vertical position of the baseline,...), you won't get any better answers than the ones that are already given. Please rephrase your question if the height of the glyphs in a Chunk wasn't what you expected.
这篇关于如何计算元素的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!