使用iText阅读pdf时出错

时间:2013-07-10 08:39:26

标签: java itext pdf-reader

我写了一个简单的程序,它从pdf文件中读取,代码如下图所示,我上传了两个pdf文件,这些文件读取了pdf并以字符串格式返回数据,问题是第一个pdf文件被正确读取当我尝试再次阅读相同的PDF时会导致以下错误:

//Fetch media from media1
InputStream is;
is = media1.getStreamData();
PdfReaderUtility pru = new PdfReaderUtility();
String s1 = pru.readPdfFile(is);
tb1.setText(s1);
//Fetch media from media2

InputStream ins;
ins = media2.getStreamData();
PdfReaderUtility pru2 = new PdfReaderUtility();
pru2.readPdfFile(ins);
String s2 = pru.readPdfFile(ins);
tb2.setText(s2);

错误::

12:31:18,763 SEVERE [org.zkoss] (http-localhost-127.0.0.1-8080-5) >>org.zkoss.zk.ui.UiException: com.lowagie.text.exceptions.InvalidPdfException: PDF header signature not found.
>>com.lowagie.text.exceptions.InvalidPdfException: PDF header signature not found.
>>  at com.lowagie.text.pdf.PRTokeniser.checkPdfHeader(PRTokeniser.java:204)
>>  at com.lowagie.text.pdf.PdfReader.readPdf(PdfReader.java:492)
>>  at com.lowagie.text.pdf.PdfReader.<init>(PdfReader.java:233)
>>  at com.lowagie.text.pdf.PdfReader.<init>(PdfReader.java:243)
>>  at com.abc.utility.PdfReaderUtility.readPdfFile(PdfReaderUtility.java:15)
>>  at com.abc.controller.ComparisonFormController.onClick$submitButton(ComparisonFormController.java:99)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>>  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>>  at java.lang.reflect.Method.invoke(Method.java:606)
>>  at org.zkoss.zk.ui.event.GenericEventListener.onEvent(GenericEventListener.java:82)
>>  at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2742)
>>  at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2713)
>>  at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2654)
>>  at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136)
>>  at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534)
>>  at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:126)
>>  at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:338)
>>  at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:348)
>>  at org.zkoss.zk.ui.AbstractComponent$ForwardListener.onEvent(AbstractComponent.java:3180)
>>  at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2742)
>>  at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2713)
>>  at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2654)
>>  at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136)
>>  at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534)
>>  at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:461)

任何提示都会很好。

这是我的PdfReaderUtility

public String readPdfFile(InputStream is) throws IOException {

        // loads the pdf file.
        PdfReader pr = new PdfReader(is);

        // Used for the reading purpose
        PdfTextExtractor pte = new PdfTextExtractor(pr);

        int noOfPages = pr.getNumberOfPages();
        String wholeText="";
        String page="";
        for(int i = 1 ; i <= noOfPages;i++) {
            page = pte.getTextFromPage(i);
            wholeText+=page;
        }

        wholeText.trim();

        return wholeText;
    }

谢谢。

2 个答案:

答案 0 :(得分:0)

在第二段代码中,您使用pru代替pru2

String s2 = pru.readPdfFile(ins);

应该是:

String s2 = pru2.readPdfFile(ins);

完整资源

//Fetch media from media1
InputStream is;
is = media1.getStreamData();
PdfReaderUtility pru = new PdfReaderUtility();
String s1 = pru.readPdfFile(is);
tb1.setText(s1);
//Fetch media from media2

InputStream ins;
ins = media2.getStreamData();
PdfReaderUtility pru2 = new PdfReaderUtility();
pru2.readPdfFile(ins);
String s2 = pru2.readPdfFile(ins); //replaced pru with pru2
tb2.setText(s2);

答案 1 :(得分:0)

对于第二个文件,您使用输入流两次,而在第二次使用时,它必须失败:

ins = media2.getStreamData();
PdfReaderUtility pru2 = new PdfReaderUtility();
pru2.readPdfFile(ins);
String s2 = pru.readPdfFile(ins);

看看最后两行。

我想这无论如何都是一个复制和粘贴错误,这两行应该是一个:

ins = media2.getStreamData();
PdfReaderUtility pru2 = new PdfReaderUtility();
String s2 = pru2.readPdfFile(ins);