java (图片转PDF)

1.导入jar包

itextpdf-5.5.12.jar

2.写代码

 1 package com.util;
 2 
 3 import 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 com.itextpdf.text.Document;
10 import com.itextpdf.text.DocumentException;
11 import com.itextpdf.text.Image;
12 import com.itextpdf.text.PageSize;
13 import com.itextpdf.text.Paragraph;
14 import com.itextpdf.text.pdf.PdfWriter;
15 /**
16  * 
17  * @author Administrator
18  *            (图片转PDF)
19  */
20 public class PDFManage {
21       
22     /**
23      * 
24      * @param imageUrllist  图片路径集合
25      * @param mOutputPdfFileName  输出的pdf文件名
26      * @return
27      */
28      public static File Pdf(ArrayList<String> imageUrllist,String mOutputPdfFileName) {  
29 //            String TAG = "PdfManager";  
30             Document doc = new Document(PageSize.A4, 20, 20, 20, 20);  
31             try {  
32                 PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));  
33                 doc.open();  
34                 for (int i = 0; i < imageUrllist.size(); i++) {  
35                     doc.newPage();  
36 //                    doc.add(new Paragraph("简单使用iText"));  
37                     Image png1 = Image.getInstance(imageUrllist.get(i));  
38                     float heigth = png1.getHeight();  
39                     float width = png1.getWidth();  
40                     int percent = getPercent2(heigth, width);  
41                     png1.setAlignment(Image.MIDDLE);  
42                     png1.scalePercent(percent+3);// 表示是原来图像的比例;  
43                     doc.add(png1);  
44                 }  
45                 doc.close();  
46             } catch (FileNotFoundException e) {  
47                 e.printStackTrace();  
48             } catch (DocumentException e) {  
49                 e.printStackTrace();  
50             } catch (IOException e) {  
51                 e.printStackTrace();  
52             }  
53       
54             File mOutputPdfFile = new File(mOutputPdfFileName);  
55             if (!mOutputPdfFile.exists()) {  
56                 mOutputPdfFile.deleteOnExit();  
57                 return null;  
58             }  
59             return mOutputPdfFile;  
60         }  
61       
62         /** 
63          * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩 
64          * 
65          * @param h 
66          * @param w 
67          * @return 
68          */  
69       
70         public static int getPercent(float h, float w) {  
71             int p = 0;  
72             float p2 = 0.0f;  
73             if (h > w) {  
74                 p2 = 297 / h * 100;  
75             } else {  
76                 p2 = 210 / w * 100;  
77             }  
78             p = Math.round(p2);  
79             return p;  
80         }  
81       
82         /** 
83          * 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的,自我认为给客户的效果是最好的 
84          * 
85          * @param args 
86          */  
87         public static int getPercent2(float h, float w) {  
88             int p = 0;  
89             float p2 = 0.0f;  
90             p2 = 530 / w * 100;  
91             p = Math.round(p2);  
92             return p;  
93         }  
94         
95         
96 
97 }

测试代码:

 1 package org.test;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 
 7 import com.util.PDFManage;
 8 
 9 public class pdfTest {
10    
11      public static void main(String[] args) {  
12                 ArrayList<String> imageUrllist = new ArrayList<String>();  
13                 imageUrllist.add("D:\" + "1" + ".jpg");  
14                 String pdfUrl = "D:\2.pdf";  
15                 File file = PDFManage.Pdf(imageUrllist, pdfUrl);  
16                 try {  
17                     file.createNewFile();  
18                 } catch (IOException e) {  
19                     e.printStackTrace();  
20                 }  
21         }  
22 }

亲测可用

原文地址:https://www.cnblogs.com/jiliunyongjin/p/7527387.html