PDF通过Java的PDFBox读取



我在使用pdfbox读取PDF时遇到了问题。我的实际PDF部分不可读,因此当我复制并粘贴不可读的部分时,它显示了很小的框符号,但是当我尝试通过PDFBox读取相同的文件时,这些字符没有读取(我不希望它们被阅读)。我期望的是,我至少会得到一些符号或一些随机字符,而不是实际字符。有什么办法做到这一点。那条线正在被选中,因此它不是图像。有人发现这个解决方法吗?

有一个pdfbox示例,我们在pdftextstripper类下覆盖写作方法,以获取一些额外的字体属性。我正在使用该方法获取文本和一些字体属性。因此,我的问题是为什么PDFBox不会读取每个字符(它可能会打印出Gibberish)。但是就我而言,我算了否。在时间中调用该方法(每个方法调用对应于每个字符),并看到NO。方法调用的确与输出文本中的字符编号确实匹配,但与总数不匹配。PDF中的字符。这是一个示例PDF,"利润"一词是不可读的,PDF甚至没有显示此词的Gibberish,它完全会跳过它。这是链接。https://drive.google.com/file/d/0b_ke2ambgdpedpedunwvtr3rvlrtfe/view?usp = sharing

第一个文件" pnl_500010_0314.pdf"

的确,实际上,整个行是"截至2014年3月31日的年度损益表",而无法提取更多内容;检查内容的原因很明显:此文本是使用复合字体编写的,该复合字体都不具有编码也不是 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根本没有告知"截至2014年3月31日的年度的损益表"行,甚至存在!

如果您更换了

    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方法使用一些私人类成员。因此,使用原始方法代码和上面指示的更改,不能简单地将其在自己的PDFTextStripper类中覆盖。一个人必须在自己的类中复制PDFTextStreamEngine的所有功能,或者必须求助于Java反射,或者必须自己修补PDFBox类。

此体系结构并不完全完美。

第二个文件" 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.

最新更新