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为存储位置

 

posted @ 2018-09-13 11:00  衣兜里  阅读(9453)  评论(0编辑  收藏  举报