1. 程式人生 > >從inputstream中讀pdf生成pdf檔案

從inputstream中讀pdf生成pdf檔案

public static void main(String[] args) {
        System.out.println(FileUtils.exists("invoice/2017011817151260118001.pdf"));
System.out.println(FileUtils.exists("invoice/20170118/2017011817151260118001.pdf"));
InputStream inputStream=FileUtils.getFileStream("invoice/20170118/2017011817151260118001.pdf");//從伺服器上讀dpf檔案
        byte
[] b = new byte[1024]; int a=0; try { //FileOutputStream fo = new FileOutputStream("D:/test/a.pdf"); FileOutputStream fo = new FileOutputStream("D:/test/wj.pdf"); // String str=convertStreamToString(inputStream); //Document document = new Document(); // while((a = inputStream.read(b)) != -1){
// fo.write(a); // // } ByteArrayOutputStream baos = new ByteArrayOutputStream(); for(int i;(i=inputStream.read())!=-1;) { baos.write(i); } baos.flush(); Document doc = new Document(); PdfStream pdfStream=new PdfStream(baos.toByteArray());
PdfWriter pw =PdfWriter.getInstance(doc, fo); // pdfStream.toPdf(pw,fo); System.out.println(doc.newPage()); pw.flush(); baos.close(); pw.close(); fo.close(); //doc.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } System.out.println(); }