Java实现多张图片转pdf

依赖包

<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>

jar 包下载:https://download.csdn.net/download/yilia_jia/10619073

代码为将目标文件夹下的文件夹中名为large.jpg的图片全部转为pdf 没用该图片则跳过

可根据需求修改图片路径

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageToPdfUtil {
    /**
     *
     * @param imageFolderPath
     *            图片文件夹地址
     * @param pdfPath
     *            PDF文件保存地址
     *
     */
    public static void createPdf(String imageFolderPath, String pdfPath) {
        //创建一个文档对象
        Document doc = new Document();
        try {
            //定义输出文件的位置
            PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));
            //开启文档
            doc.open();
            //设定字体 为的是支持中文
            //BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

            // 获取图片文件夹对象
            File file = new File(imageFolderPath);
            File[] files = file.listFiles();
            // 循环获取图片文件夹内的图片
            for (File file1 : files) {
                // 获取large.jpg图片文件
                File file2 = new File(imageFolderPath + file1.getName() + "/large.jpg");
                // 判断文件是否存在
                if (file2.exists()) {
                    //图片的路径
                    Image  img = Image.getInstance(imageFolderPath + file1.getName() + "/large.jpg");

                    //获得图片的宽高
                    Float height = img.getHeight();
                    Float width = img.getWidth();
                    //统一按照宽度压缩
                    Integer percent = getPercent(height, width);
                    //设置图片居中显示
                    img.setAlignment(Image.MIDDLE);
                    //按百分比显示图片的比例
                    img.scalePercent(percent);
                    //可设置图像高和宽的比例
                    //img.scalePercent(50, 100);
                    doc.add(img);
                }
            }
            // 关闭文档
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        createPdf("C:/Users/admin/Desktop/images/", "C:/Users/admin/Desktop/test.pdf");
    }

    /**
     * 按照宽度压缩
     * @param
     */
    public static Integer getPercent(Float h,Float w)
    {
        Integer p=0;
        Float p2=0.0f;
        p2=530/w*100;
        System.out.println("--"+p2);
        p=Math.round(p2);
        return p;
    }

}

猜你喜欢

转载自blog.csdn.net/yilia_jia/article/details/81938709