0

我编写了一个从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;
    }

谢谢你。

4

2 回答 2

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);
于 2013-07-10T09:04:15.143 回答
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);
于 2013-07-10T08:41:36.203 回答