PDFBox 文本提取、旋转和字体名称、大小
PDFBox text extraction, rotation and font name, size
我正在使用最新的 PDFBOX 库来提取文本,为此,我编写了自定义 PDFStreamEngine(显示了部分代码,但其余代码应该类似):
else if ("Tf".equals(operation) && parsingTextObject) {
if (operands.size() < 2) {
throw new MissingOperandException(operator, operands);
}
COSBase base0 = operands.get(0);
COSBase base1 = operands.get(1);
if (!(base0 instanceof COSName)) {
return;
}
if (!(base1 instanceof COSNumber)) {
return;
}
COSName fontName = (COSName) base0;
float fontSize = ((COSNumber) base1).floatValue();
getGraphicsState().getTextState().setFontSize(fontSize);
PDFont font = getResources().getFont(fontName);
getGraphicsState().getTextState().setFont(font);
}
但是,我遇到了 3 个问题:
第一个:"Tf" 运算符 - 在 PDF /F1 1 Tf 中:当我显示 fontName 和大小时,它显示:EVMANJ+MyriadPro-Regular,大小 1;然而,illustrator 和 adobe acrobat 上的实际字体名称:Myriad Pro,大小 8 pt
第二期:文字是竖着的,如何提取文字的旋转?
第三期:如何正确对待TJ运营商?
P.S: 我可以私下提供pdf
这个问题的答案并不直接,我不得不扩展 showFontGlyph
protected void showFontGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
..do you logic here}
为了找到旋转,我不得不将 PDFBox 1.8 中的代码复制粘贴到我的 class
/**
* Return the direction/orientation of the string in this object based on
* its text matrix.
*
* @return The direction of the text (0, 90, 180, or 270)
*/
public int getDir() {
int direction = -1;
if (direction < 0) {
float a = getTextMatrix().getScaleY();
float b = getTextMatrix().getShearY();
float c = getTextMatrix().getShearX();
float d = getTextMatrix().getScaleX();
// 12 0 left to right
// 0 12
if (a > 0 && Math.abs(b) < d && Math.abs(c) < a && d > 0) {
direction = 0;
}
// -12 0 right to left (upside down)
// 0 -12
else if (a < 0 && Math.abs(b) < Math.abs(d) && Math.abs(c) < Math.abs(a) && d < 0) {
direction = 180;
}
// 0 12 up
// -12 0
else if (Math.abs(a) < Math.abs(c) && b > 0 && c < 0 && Math.abs(d) < b) {
direction = 90;
}
// 0 -12 down
// 12 0
else if (Math.abs(a) < c && b < 0 && c > 0 && Math.abs(d) < Math.abs(b)) {
direction = 270;
} else {
direction = 0;
}
}
return direction;
}
并且我在处理流时检测到TJ运算符时调用了getDir函数。
我正在使用最新的 PDFBOX 库来提取文本,为此,我编写了自定义 PDFStreamEngine(显示了部分代码,但其余代码应该类似):
else if ("Tf".equals(operation) && parsingTextObject) {
if (operands.size() < 2) {
throw new MissingOperandException(operator, operands);
}
COSBase base0 = operands.get(0);
COSBase base1 = operands.get(1);
if (!(base0 instanceof COSName)) {
return;
}
if (!(base1 instanceof COSNumber)) {
return;
}
COSName fontName = (COSName) base0;
float fontSize = ((COSNumber) base1).floatValue();
getGraphicsState().getTextState().setFontSize(fontSize);
PDFont font = getResources().getFont(fontName);
getGraphicsState().getTextState().setFont(font);
}
但是,我遇到了 3 个问题: 第一个:"Tf" 运算符 - 在 PDF /F1 1 Tf 中:当我显示 fontName 和大小时,它显示:EVMANJ+MyriadPro-Regular,大小 1;然而,illustrator 和 adobe acrobat 上的实际字体名称:Myriad Pro,大小 8 pt
第二期:文字是竖着的,如何提取文字的旋转?
第三期:如何正确对待TJ运营商?
P.S: 我可以私下提供pdf
这个问题的答案并不直接,我不得不扩展 showFontGlyph
protected void showFontGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
..do you logic here}
为了找到旋转,我不得不将 PDFBox 1.8 中的代码复制粘贴到我的 class
/**
* Return the direction/orientation of the string in this object based on
* its text matrix.
*
* @return The direction of the text (0, 90, 180, or 270)
*/
public int getDir() {
int direction = -1;
if (direction < 0) {
float a = getTextMatrix().getScaleY();
float b = getTextMatrix().getShearY();
float c = getTextMatrix().getShearX();
float d = getTextMatrix().getScaleX();
// 12 0 left to right
// 0 12
if (a > 0 && Math.abs(b) < d && Math.abs(c) < a && d > 0) {
direction = 0;
}
// -12 0 right to left (upside down)
// 0 -12
else if (a < 0 && Math.abs(b) < Math.abs(d) && Math.abs(c) < Math.abs(a) && d < 0) {
direction = 180;
}
// 0 12 up
// -12 0
else if (Math.abs(a) < Math.abs(c) && b > 0 && c < 0 && Math.abs(d) < b) {
direction = 90;
}
// 0 -12 down
// 12 0
else if (Math.abs(a) < c && b < 0 && c > 0 && Math.abs(d) < Math.abs(b)) {
direction = 270;
} else {
direction = 0;
}
}
return direction;
}
并且我在处理流时检测到TJ运算符时调用了getDir函数。