java img圖片轉pdf 工具類


  1import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.util.ArrayList;
  8 
  9 import javax.servlet.http.HttpServletRequest;
 10 
 11 import com.itextpdf.text.Document;
 12 import com.itextpdf.text.DocumentException;
 13 import com.itextpdf.text.Image;
 14 import com.itextpdf.text.PageSize;
 15 import com.itextpdf.text.pdf.PdfWriter;
 16 
 17 public class PdfUtilImg {
 18      public static File Pdf(ArrayList<String> imageUrllist,String mOutputPdfFileName) {  
 19             Document doc = new Document(PageSize.A4, 20, 20, 20, 20); //new一個pdf文檔 
 20             try {  
 21                 PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); //pdf寫入 
 22                 doc.open();//打開文檔  
 23                 for (int i = 0; i < imageUrllist.size(); i++) {  //循環圖片List,將圖片加入到pdf中
 24                     doc.newPage();  //在pdf創建一頁
 25                     Image png1 = Image.getInstance(imageUrllist.get(i)); //通過文件路徑獲取image 
 26                     float heigth = png1.getHeight();  
 27                     float width = png1.getWidth();  
 28                     int percent = getPercent2(heigth, width);  
 29                     png1.setAlignment(Image.MIDDLE);  
 30                     png1.scalePercent(percent+3);// 表示是原來圖像的比例;  
 31                     doc.add(png1);  
 32                 }  
 33                 doc.close();  
 34             } catch (FileNotFoundException e) {  
 35                 e.printStackTrace();  
 36             } catch (DocumentException e) {  
 37                 e.printStackTrace();  
 38             } catch (IOException e) {  
 39                 e.printStackTrace();  
 40             }  
 41       
 42             File mOutputPdfFile = new File(mOutputPdfFileName);  //輸出流
 43             if (!mOutputPdfFile.exists()) {  
 44                 mOutputPdfFile.deleteOnExit();  
 45                 return null;  
 46             }  
 47             return mOutputPdfFile; //反回文件輸出流
 48         }  
 57       
 58         public static int getPercent(float h, float w) {  
 59             int p = 0;  
 60             float p2 = 0.0f;  
 61             if (h > w) {  
 62                 p2 = 297 / h * 100;  
 63             } else {  
 64                 p2 = 210 / w * 100;  
 65             }  
 66             p = Math.round(p2);  
 67             return p;  
 68         }  
 75         public static int getPercent2(float h, float w) {  
 76             int p = 0;  
 77             float p2 = 0.0f;  
 78             p2 = 530 / w * 100;  
 79             p = Math.round(p2);  
 80             return p;  
 81         }  
 94     public void imgOfPdf(String filepath,HttpServletRequest request) {
 95             boolean result = false;
 96             try {
 97                   ArrayList<String> imageUrllist = new ArrayList<String>(); //圖片list集合
 98                   imageUrllist.add(request.getSession()
 99                        .getServletContext().getRealPath("\\" + filepath));  //添加圖片文件路徑
100                   String fles = filepath.substring(0, filepath.lastIndexOf("."));
101                   String pdfUrl = request.getSession().getServletContext()
102                             .getRealPath("\\" +fles+".pdf");  //輸出pdf文件路徑          
103                 result = true;
104                 if (result == true) {
105                     File file = PdfUtilImg.Pdf(imageUrllist, pdfUrl);//生成pdf  
106                    file.createNewFile();  
107                 }
108             } catch (IOException e) {
109                 e.printStackTrace();
110             }
111         }
112 }
使用方法:

PdfUtilImg img = new PdfUtilImg();
img.imgOfPdf(filePaths, request);//filePaths為存儲位置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM