2014-01-09 5 views
0

변환 이미지가 PDF 파일에 약간의 문제가 있습니다.어떻게 자바 서블릿에서 이미지를 pdf로 변환 할 수 있습니까?

나는 하나의 이미지를 pdf로 변환 했으므로 여러 개의 이미지를 하나의 pdf 파일로 변환하려고합니다.

pdf 작성을 위해 itext 라이브러리를 사용하고 있습니다.

이것은 하나의 이미지로 하나의 pdf를 작성하는 코드입니다.

Document convertJpgToPdf=new Document(); 
     //Create PdfWriter for Document to hold physical file 
     PdfWriter.getInstance(convertJpgToPdf, new  FileOutputStream("D:\\PDF_Path\\ConvertImagetoPDF.pdf")); 
     convertJpgToPdf.open(); 
     // Write Somethinf into that File 
     convertJpgToPdf.add(new Paragraph("Welcome, Your Input Image is Converted to PDF, Save the File")); 
     convertJpgToPdf.add(new Paragraph("PDF Produced by Converting Image to PDF as Servlet"));  
     //Get the input image to Convert to PDF 
     Image convertJpg=Image.getInstance(uploadPath); 
     //Add image to Document 
     convertJpgToPdf.add(convertJpg); 
     //Close Document 
     convertJpgToPdf.close(); 

내 문제는 그 폴더 내가 하나의 PDF 파일로 모든 이미지,

 PDF File Header 

     image1 

     image2 

     .......... 
     .......... 

     imageN 

처럼하는 것이 가능하다는 것을 변환 할 수있는 방법을 10 개 개의 이미지를 포함, 내가 하나 개의 폴더가 있었다. 도와주세요. 미리 감사드립니다.

+0

이를 참조하십시오 작업의 : http://bigsnowball.com/content/create-multi-page-pdf-scanned-fax-images-java-using -itext – Susai

답변

3
Document document = new Document(); 
    // step 2 
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); 
    // step 3 
    document.open(); 
    // step 4 
    // Adding a series of images 
    Image img; 
    for (int i = 0; i < RESOURCES.length; i++) { 
     img = Image.getInstance(String.format("resources/img/%s", RESOURCES[i])); 
     if (img.getScaledWidth() > 300 || img.getScaledHeight() > 300) { 
      img.scaleToFit(300, 300); 
     } 
     document.add(new Paragraph(
       String.format("%s is an image of type %s", RESOURCES[i], img.getClass().getName()))); 
     document.add(img); 
    } 

하여 itext의 튜토리얼을 읽어 보시기 바랍니다 미세 click here

+3

+1이 스레드에 대한 유일한 진정한 대답 인 "read the manual"에 +1하지만 복사/붙여 넣기가 가능한 코드를 제공하는 것은 매우 친숙합니다. – Gimby

+0

이 링크에서 직접 이미지를 제공하지만 해당 이미지를 가져 오기 위해 폴더를 열고 싶습니다. 서블릿을 사용하고 있습니다. – selvam

관련 문제