在 java 中通过 pdfbox 阅读 pdf

pdf reading via pdfbox in java

我在使用 pdfbox 阅读 pdf 时遇到问题。我的实际 pdf 是部分不可读的,所以当我在编辑器中复制和粘贴不可读的部分时,它会显示小框符号,但是当我尝试通过 pdfbox 读取相同的文件时,这些字符不会被读取(我不希望它们待读)。我期望的是我至少得到一些符号或一些随机字符而不是实际字符。有什么办法可以做到这一点。该行正在被选中,因此它不是图像。有没有人找到任何解决方法?

有一个 pdfbox 示例,我们在 pdfTextStripper class 下重写 writeString 方法以获得一些额外的字体属性。我正在使用该方法来获取我的文本和一些字体属性。 所以我的问题是为什么 pdfbox 不读取每个字符(它可能打印出乱码)。但就我而言,我数了数。该方法被调用的次数(每个方法调用对应于每个字符)并看到没有。的方法调用与输出文本中的 no.of 个字符匹配,但与总数不匹配。 pdf中的字符。这是一个示例 pdf,单词 "Profit" 不可读,pdf 甚至不显示该单词的乱码,它只是完全跳过它。 这是 link。 https://drive.google.com/file/d/0B_Ke2amBgdpedUNwVTR3RVlRTFE/view?usp=sharing

第一个文件"PnL_500010_0314.pdf"

确实,实际上整行 "Statement of Profit and Loss for the year ended March 31, 2014" 以及更多内容都无法提取;检查内容,原因很明显:该文本是使用复合字体编写的,既没有 Encoding 也没有 ToUnicode 条目来识别字符有问题。

org.apache.pdfbox.text.PDFTextStreamEngine(从中导出 PDFTextStripper)方法 showGlyph 在调用 processTextPositionPDFTextStripper 实现并从中检索其文本信息)包含此代码:

// use our additional glyph list for Unicode mapping
unicode = font.toUnicode(code, glyphList);

// when there is no Unicode mapping available, Acrobat simply coerces the character code
// into Unicode, so we do the same. Subclasses of PDFStreamEngine don't necessarily want
// this, which is why we leave it until this point in PDFTextStreamEngine.
if (unicode == null)
{
    if (font instanceof PDSimpleFont)
    {
        char c = (char) code;
        unicode = new String(new char[] { c });
    }
    else
    {
        // Acrobat doesn't seem to coerce composite font's character codes, instead it
        // skips them. See the "allah2.pdf" TestTextStripper file.
        return;
    }
}

有问题的字体没有提供任何文本提取线索。因此,unicode 这里是 null.

另外,字体是复合的,不是简单的。因此,else 子句被执行并且 processTextPosition 甚至没有被调用。

PDFTextStripper,因此根本不知道行 "Statement of Profit and Loss for the year ended March 31, 2014" 甚至存在!

如果你替换那个

    else
    {
        // Acrobat doesn't seem to coerce composite font's character codes, instead it
        // skips them. See the "allah2.pdf" TestTextStripper file.
        return;
    }

PDFTextStreamEngine.showGlyph 中通过某些代码设置 unicode,例如使用 Unicode 替换字符

    else
    {
        // Use the Unicode replacement character to indicate an unknown character
        unicode = "\uFFFD";
    }

你会得到

57
THIRTY SEVENTH ANNUAL REPORT 2013-14
STANDALONE FINANCIAL STATEMENTS
�������������������������������������������������������������
As per our report attached. Directors
For Deloitte Haskins & Sells LLP Deepak S. Parekh Nasser Munjee R. S. Tarneja
Chartered Accountants �������� B. S. Mehta J. J. Irani
D. N. Ghosh Bimal Jalan
Keki M. Mistry S. A. Dave D. M. Sukthankar
Sanjiv V. Pilgaonkar ���������������
Partner �����������������������
Renu Sud Karnad V. Srinivasa Rangan Girish V. Koliyote
������, May 6, 2014 Managing Director ������������������ �����������������
Notes Previous Year
� in Crore � in Crore
INCOME
����������������������� 23  23,894.03  20,796.95 
���������������������������� 24  248.98  315.55 
������������ 25  54.66  35.12 
Total Revenue  24,197.67  21,147.62 
EXPENSES
Finance Cost 26  16,029.37  13,890.89 
�������������� 27  279.18  246.19 
���������������������� 28  86.98  75.68 
�������������� 29  230.03  193.43 
������������������������������ 11 & 12  31.87  23.59 
Provision for Contingencies  100.00  145.00 
Total Expenses  16,757.43  14,574.78 

PROFIT BEFORE TAX  7,440.24  6,572.84 
�����������
�������������  1,973.00  1,727.68 
�������������� 14  27.00  (3.18)
PROFIT FOR THE YEAR 3  5,440.24  4,848.34 
EARNINGS PER SHARE��������������� 2) 31
- Basic 34.89 31.84
- Diluted 34.62 31.45
�������������������������������������������������������������

不幸的是,PDFTextStreamEngine.showGlyph 方法使用了一些私有 class 成员。因此,不能简单地使用具有上述更改的原始方法代码在自己的 PDFTextStripper class 中覆盖它。要么必须在自己的 class 中复制 PDFTextStreamEngine 的几乎所有功能,要么必须求助于 Java 反射,或者必须自己修补 PDFBox classes。

这个架构并不完美。

第二个文件"Bal_532935_0314.pdf"

第二个文件的情况是由上面引用的同一段PDFBox代码引起的。不过这一次,字体很简单,执行另一个代码块:

    if (font instanceof PDSimpleFont)
    {
        char c = (char) code;
        unicode = new String(new char[] { c });
    }

这里发生的事情纯属猜测:如果没有将字形代码映射到 Unicode 的信息,我们假设映射是 Latin-1,它可以平凡地嵌入到 char 中。正如在 OP 的第二个文件中可见的那样,这个假设并不总是成立。

如果您不希望 PDFBox 在这里做出这样的假设,也可以将上面的 if 块替换为

    if (font instanceof PDSimpleFont)
    {
        // Use the Unicode replacement character to indicate an unknown character
        unicode = "\uFFFD";
    }

这导致

Aries Agro Care Private Limited
1118th Annual Report 2013-14
Balance Sheet as at 31st March, 2014
Particulars Note
No.
 As at 
31 March, 2014
Rupees
 As at
31 March, 2013
Rupees
I. EQUITY AND LIABILITIES
(1) Shareholder's Funds
(a) ������������� 3  100,000  100,000
(b) Reserves and Surplus 4  (2,673,971) ������������
 (2,573,971) ������������
(2) Current Liabilities
(a) Short Term Borrowings 5  5,805,535 �����������
(b) Trade Payables 6  159,400 ���������
(c) ������������������������� 7  2,500  22,743 
 5,967,435  5,934,756 
TOTAL  3,393,464 �����������
II. ASSETS
(1) Non-Current Assets
(a) �������������������� �  - -
 - -
(2) Current Assets
(a) ����������������������� 9  39,605 �������
(b) ����������������������������� 10  3,353,859 ����������
 3,393,464 ����������
TOTAL  3,393,464 ����������
��������������������������������
The Notes to Accounts 1 to 23 form part of these Financial Statements
As per our report of even date For and on behalf of the Board
For Kirti D. Shah & Associates 
��������������������� 
�����������������������������
Dr. Jimmy Mirchandani
Director
Kirti D. Shah 
Proprietor 
Membership No 32371
Dr. Rahul Mirchandani 
Director
Place : Mumbai. 
Date :- 26th May, 2014.