java如何将图片转为pdf

作者: adm 分类: java 发布时间: 2021-05-21 15:29

引入pom依赖

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>

2.创建图片转pdf的实现类

package com.ran;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
* 图片转pdf工具类
*/
public class ImgToPdfUtil {
public static File Pdf(ArrayList<String> imageUrllist, String mOutputPdfFileName) {
Document doc = new Document(PageSize.A4, 0, 0, 0, 0); //new一个pdf文档
try {
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); //pdf写入
doc.open();//打开文档
for (int i = 0; i < imageUrllist.size(); i++) { //循环图片List,将图片加入到pdf中
doc.newPage(); //在pdf创建一页
Image png1 = Image.getInstance(imageUrllist.get(i)); //通过文件路径获取image
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent + 3);// 表示是原来图像的比例;
doc.add(png1);
}
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

File mOutputPdfFile = new File(mOutputPdfFileName); //输出流
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile; //反回文件输出流
}

public static int getPercent(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
} else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
}

public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}

/**
* @Description: 通过图片路径及生成pdf路径,将图片转成pdf
* @Author: zd
* @Date: 2019/9/29
*/
public void imgOfPdf(String filepath, String imgUrl) {
try {
ArrayList<String> imageUrllist = new ArrayList<String>(); //图片list集合
String[] imgUrls = imgUrl.split(“,”);
for (int i=0; i<imgUrls.length; i++) {
imageUrllist.add(imgUrls[i]);
}
String pdfUrl = filepath; //输出pdf文件路径
File file = this.Pdf(imageUrllist, pdfUrl);//生成pdf
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
ImgToPdfUtil img = new ImgToPdfUtil();
img.imgOfPdf(“D:\\test\\test.pdf”,”D:\\test\\7.jpg,D:\\test\\7.jpg”);
}
}

3.测试一下即可是否成功

public static void main(String[] args) {
ImgToPdfUtil img = new ImgToPdfUtil();
img.imgOfPdf(“D:\\test\\test.pdf”,”D:\\test\\7.jpg,D:\\test\\7.jpg”);
}

ok

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!