【发布时间】:2017-10-24 03:17:52
【问题描述】:
这是一个简单的冒犯pdf。
当我运行DrawPrintTextLocations 时,我看到了下面的内容..
但据我了解,边界框(上方蓝色)应代表您选择文本时在任何 pdf 阅读器中显示的灰色区域,如下所示。
如果 pdf 阅读器能够找出要突出显示的灰色区域,则应该能够找出相同的内容,从而获得字体大小(?)。这个问题是任何人都可以指出我正确的方向。
以下是“测试线”文本中“T”的详细信息。来自其TextPosition 对象变量text:
72.4801 //text.getXDirAdj()
83.7600 //text.getYDirAdj()
1.0 //text.getFontSize()
50.0 //text.getFontSizeInPt() ::I'm unable to decipher the 50.0
12.0 //text.getXScale() ::Can I assume this to be the font size
8.004 //text.getHeightDir()
7.8984 //text.getWidthOfSpace()
7.1160 //text.getWidthDirAdj()
950.0 //fontDesc.getAscent()
-222.0 //fontDesc.getDescent()
[x=72.4801,y=75.7560,w=7.1160,h=8.0040]
//Red Box boundaries
[x=72.4801,y=46.3560,w=7.1160,h=66.9600] //The height of 66.96 relates to 50 but not sure how?
//Blue Bounding Box boundaries
问题:
1. 边界框问题:当我调用font.getBoundingBox()时,这似乎不一致。有解决办法吗?
2. getFontSizeInPts(): 这个方法好像是受边界框影响。我这样想对吗?(因为 Pt 中的字体大小显示为 50)
3. FontSize 的获取方式是什么?
我需要字体大小,因为我的任务是使用不同的字体重新创建 pdf。
这也是一个正确 pdf 的例子,但字体大小显示为 16 而不是最初使用的 12。
对于类似的pdf 有适当的边界框,下面是详细信息:-
以下是“测试线”文本中“T”的详细信息。来自其TextPosition 对象变量text:
72.0605 //text.getXDirAdj()
83.3199 //text.getYDirAdj()
16.0 //text.getFontSize() :: Why is this showing 16 while my font is 12 in size
16.0 //text.getFontSizeInPt()
12.0101 //text.getXScale() ::Can I assume this to be the font size
6.6618 //text.getHeightDir()
2.6447 //text.getWidthOfSpace()
7.1193 //text.getWidthDirAdj()
778.808 //fontDesc.getAscent() :: There seems to be an issue with the ascent
-222.1680 //fontDesc.getDescent()
[x=72.0605,y=76.6581,w=7.1193,h=6.6618]
//Red Box boundaries
[x=72.0605,y=72.6176,w=7.1193,h=13.3237] //The height of 13.3237 relates to 12 the font size but not sure how?
//Blue Bounding Box boundaries
使用 MKL 的答案后更新
以下是对我有用的...
//Make Line
Line2D.Float line = new Line2D.Float(0,0,0,1f);
LOG.debug("Line<Before Transform>:" + line.getBounds2D());
s=myTextMatrix.createAffineTransform().createTransformedShape(line);
LOG.debug("Line after AT:"+s.getBounds2D());
s=pageFlipAffineTransform.createTransformedShape(s);
s=pageRotateAffineTransform.createTransformedShape(s);
rect2 = s.getBounds2D();
LOG.debug("Line<After Transform>:" + rect2);
//Font Size
double wi=rect2.getWidth();
double he=rect2.getHeight();
double total=Math.sqrt(wi*wi+he*he);//This is done in case of rotation
long fntSizeinPt = Math.round(total);
LOG.debug("deciphered Font Size is:" + fntSizeinPt);
【问题讨论】:
-
与您不同,我的两个示例文件都得到了过度扩展的边界框。难怪在这两个文件中,PDF 字体信息都声称有一个巨大的边界框......
-
@mkl 在这里有同样的行为。两种字体都有相同的巨大边界框。例如边缘也使用此信息进行文本选择。在我们的解决方案中,我们尝试通过分析所有字形边界框来重新创建边界框来解决这种情况。无论如何,例如Foxit 的 Adobe Reader 似乎以另一种方式做到这一点,如bulbus 的第二张截图所示。
-
@JanSlabon OP 声称两个屏幕截图都是使用 PDFBox
DrawPrintTextLocations工具创建的。我使用了那个工具,但两个 PDF 都有巨大的边界框。 -
@mkl 道歉,我已经用正确的 pdf 更新了第二个链接。
-
@bulbus 我检查了你的第三个文件,确实,其中的主要字体有一个合理的 FontBBox 值;因此,您得到的大多是合理的蓝框。其中用于零宽度空间的第二种字体不太合理,并导致
DrawPrintTextLocations输出中出现蓝色尖峰。我已经相应地扩展了我的答案。