pdf reading via pdfbox in java

前端 未结 1 377
时光说笑
时光说笑 2020-12-20 06:00

I have encountered a problem while reading the pdf using pdfbox. My actual pdf is partially unreadable so when i copy and paste the unreadable part in an editor it shows lit

1条回答
  •  囚心锁ツ
    2020-12-20 06:30

    The first file "PnL_500010_0314.pdf"

    Indeed, actually the whole line "Statement of Profit and Loss for the year ended March 31, 2014" and much more cannot be extracted; inspecting the contents the reason becomes obvious: This text is written using a composite font which neither has an Encoding nor a ToUnicode entry to allow identifying the character in question.

    The org.apache.pdfbox.text.PDFTextStreamEngine (from which PDFTextStripper is derived) method showGlyph shortly before calling processTextPosition (which PDFTextStripper implements and from which it retrieves its text information) contains this code:

    // 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;
        }
    }
    

    The font in question does not offer any clues for text extraction. Thus, unicode here is null.

    Furthermore, the font is composite, not simple. Thus, the else clause is executed and processTextPosition is not even called.

    PDFTextStripper, therefore, is not informed at all that the line "Statement of Profit and Loss for the year ended March 31, 2014" even exists!

    If you replace that

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

    in PDFTextStreamEngine.showGlyph by some code setting unicode, e.g. using the Unicode replacement character

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

    you'll get

    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
    �������������������������������������������������������������
    

    Unfortunately that PDFTextStreamEngine.showGlyph method uses some private class members. Thus, one cannot simply override it in one's own PDFTextStripper class using the original method code with the change indicated above. One either has to replicate nearly all functionality of PDFTextStreamEngine in one's own class, or one has to resort to Java reflection, or one has to patch PDFBox classes themselves.

    This architecture is not exactly perfect.

    The second file "Bal_532935_0314.pdf"

    The case of the second file is caused by the same piece of PDFBox code quoted above. As this time, though, the font is simple, the other code block is executed:

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

    What happens here is pure guesswork: If there is no information for mapping glyph code to Unicode, let's assume the mapping is Latin-1 which embeds trivially into char. As becomes visible in the OP's second file, this assumption does not always hold.

    If you don't want PDFBox to make assumptions like these here, also replace the if block above by

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

    This results in

    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.
    

    0 讨论(0)
提交回复
热议问题