Apache PDFBox 无法提取文本,但 PDFBox 调试器正确显示文本?

问题描述 投票:0回答:1

我无法从 pdf 中提取文本,它显示垃圾字符,但当我使用调试器检查时,它显示正确的文本。

这是 pdf 链接

假设我正在尝试使用

PDFTextStripper
提取第 9 页,我得到如下输出:

hhhhhhhhhhhhhhhhhhhhhhhIhhIhhhhhhhhhIh]hhhhhIhhIIhhIIIhhhh...

尝试使用 Adobe Reader 复制或使用其他库 Itext、MuPdf 提取时,我也会得到相同的结果。

但是,当通过 PDFBox 调试器检查时,我看到文本正确可见,如下所示(突出显示为红色):

enter image description here

我不了解上面的 PDF 流语法,但如果 PDFBox 调试器显示正确的文本,为什么 PDFBox 无法正确提取文本?

此外,使用的字体是一种古老的Gurmukhi脚本字体,其中的字形大多在ASCII范围内绘制,因此文本是英文字母。

java pdf itext pdfbox
1个回答
0
投票

问题是字体的字符代码映射到 unicode 不正确...

但是,直接在

processTextPosition
中使用字符代码对我来说很有效:

@Override
protected void processTextPosition(TextPosition text)
{
    super.processTextPosition(text);

    // following gives incorrect mapping
    // String unicode = text.getUnicode();

    int[] cc = text.getCharacterCodes();
    String unicode = new String(cc, 0, cc.length);
}

有关 processTextPosition 使用的更多信息,请参阅官方

example

© www.soinside.com 2019 - 2024. All rights reserved.