English 中文(简体)
java PdfTextExtractor.getTextFromPage(Unknown Source)
原标题:

Hello i have trouble with parsing a pdf when the iterator reaches page 11 an exception is throw.

Any ideas? Thanks

Here s my code:

import java.io.*;
import java.nio.charset.Charset;
import java.util.regex.*;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.hyphenation.TernaryTree.Iterator;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

public class PdfParser {
    /** 
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int index = 0;
        try {
            PdfReader readerN = new PdfReader("C:\Documents and Settings\stefan.stere\hibernateWorkspace\PdfParser\src\monitor3.pdf");
            OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(new File("C:\Documents and Settings\stefan.stere\hibernateWorkspace\PdfParser\src\pdf2txt.rtf")),"Cp1252");

            PdfTextExtractor parse = new PdfTextExtractor(readerN);
            int nrPages = readerN.getNumberOfPages();

            for (int i=1; i<nrPages ; i++) {
                index++;
                String page = parse.getTextFromPage(i);
                if(page != null){
                    page = page.replace(new StringBuffer("null"), new StringBuffer("??"));
                    page = page.replaceAll("Comercial.", "Comerciala");
                    page = page.replaceAll("ACT ADI..IONAL", "ACT ADITIONAL");
                    page = page.replaceAll("HOT.R..E", "HOTARARE");
                    page = page.replaceAll("HOT.R..EA", "HOTARAREA");
                    page = page.replaceAll("HOT.R..I", "HOTARARI");
                    page = page.replaceAll("..cheiat.", "incheiata");
                    page = page.replaceAll("ANUN..", "ANUNT");
                    out.write(page);
                    System.out.println(page);
                }
            }
            out.close();
            readerN.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println(index);
        }
    }
}

and the exception stack:

java.lang.ArrayIndexOutOfBoundsException: Invalid index: 62
at com.lowagie.text.pdf.CMapAwareDocumentFont.decodeSingleCID(Unknown Source)
at com.lowagie.text.pdf.CMapAwareDocumentFont.decode(Unknown Source)
at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.decode(Unknown Source)
at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.displayPdfString(Unknown Source)
at com.lowagie.text.pdf.parser.PdfContentStreamProcessor$ShowTextArray.invoke(Unknown Source)
at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(Unknown Source)
at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent(Unknown Source)
at com.lowagie.text.pdf.parser.PdfTextExtractor.getTextFromPage(Unknown Source)
at PdfParser.main(PdfParser.java:32)
最佳回答

No answer but it seems a lot of people have the same problem, there s another related question on SO. And if you search with ArrayIndexOutOfBoundsException and getTextFromPage on google you see the same problem but no solution...

BTW, your loop will stop before processing the last page as the first page has index 1...

问题回答

As info, now iText 5.0.6 works perfect with more PDF versions, I ve tested PDF generated with many different programs and 2.1.7 had problems on many.

Get the last version from iText download





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签