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;
    }

}
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值