2 bing1987jing bing1987jing 于 2017.09.05 16:56 提问

java 使用itext拆分pdf出错

使用main方法手动调用没问题,放入项目中动态调用出错

    public static void main(String[] args) {  
        partitionPdfFile("C:\\mb\\111.pdf","C:\\mb\\Chapter04.pdf", 2,2); 

    }  

    /** 
     * 截取pdfFile的第from页至第end页,组成一个新的文件名 
     * @param pdfFile 
     * @param subfileName 
     * @param from 
     * @param end 
     * 
     */  
    public static void partitionPdfFile(String pdfFile,  
            String newFile, int from, int end) {  
        System.out.println("aaa");
        Document document = null;  
        PdfCopy copy = null;          
        try {  
            PdfReader reader = new PdfReader(pdfFile);            
            int n = reader.getNumberOfPages();            
            if(end==0){  
                end = n;  
            }  
            ArrayList<String> savepaths = new ArrayList<String>();  
//            String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1);  
//            String savepath = staticpath+ newFile;  
            String savepath =  newFile; 
            savepaths.add(savepath);  
            document = new Document(reader.getPageSize(1));  
            copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));  
            document.open();  
            for(int j=from; j<=end; j++) {  
                document.newPage();   
                PdfImportedPage page = copy.getImportedPage(reader, j);  
                copy.addPage(page);  
            }  
            document.close();  

        } catch (IOException e) {  
            e.printStackTrace();  
        } catch(DocumentException e) {  
            e.printStackTrace();  
        }  
    }  

使用其他方法调用 partitionPdfFile("C:\mb\111.pdf","C:\mb\Chapter04.pdf", 2,2); 出错
错误内容:

 java.io.EOFException
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:261)
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:253)
    at com.itextpdf.text.pdf.PdfReader.getStreamBytesRaw(PdfReader.java:2254)
    at com.itextpdf.text.pdf.PdfReader.getStreamBytesRaw(PdfReader.java:2291)
    at com.itextpdf.text.pdf.PRStream.toPdf(PRStream.java:215)
    at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:157)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:396)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:376)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:368)
    at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:806)
    at com.itextpdf.text.pdf.PdfCopy.copyIndirect(PdfCopy.java:214)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:282)
    at com.itextpdf.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:236)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:280)
    at com.itextpdf.text.pdf.PdfCopy.copyIndirect(PdfCopy.java:213)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:282)
    at com.itextpdf.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:236)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:280)
    at com.itextpdf.text.pdf.PdfCopy.copyIndirect(PdfCopy.java:213)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:282)
    at com.itextpdf.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:236)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:280)
    at com.itextpdf.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:236)
    at com.itextpdf.text.pdf.PdfCopy.copyObject(PdfCopy.java:280)
    at com.itextpdf.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:233)
    at com.itextpdf.text.pdf.PdfCopy.addPage(PdfCopy.java:362)

3个回答

qq_32763061
qq_32763061   2017.09.05 17:36

你是不是放在web项目中了

bing1987jing
bing1987jing   2017.09.06 08:38

对放到web项目中。在同一个class文件中,用main执行就行可,用当前class中的其他放大调用就出错

bing1987jing
bing1987jing   2017.09.06 08:56

使用main方法手动调用没问题,动态动态调用出错图片说明

Csdn user default icon
上传中...
上传图片
插入图片