/**
* 图片组合生成PDF
* @param pics
* @param targetPDF
* @throws Exception
*/
//list中为图片的绝对路径集合
public static void toPDF(List<String> pics, String targetPDF) throws Exception {
//创建新的PDF文档:A4大小,左右上下边框均为0
Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
try {
//获取PDF书写器
PdfWriter.getInstance(doc, new FileOutputStream(targetPDF));
//打开文档
doc.open();
//图片对象
Image img = null;
//遍历
for (int i = 0; i < pics.size(); i++) {
//获取图片
img = Image.getInstance(new URL(pics.get(i)));
//使图片与A4纸张大小自适应
img.scaleToFit(new Rectangle(PageSize.A4));
//添加到PDF文档
doc.add(img);
//下一页,每张图片一页
doc.newPage();
}
} catch (Exception e) {
throw e;
}finally{
//关闭文档
doc.close();
}
}
利用itextpdf将多图片合成PDF
猜你喜欢
转载自blog.csdn.net/qq_32090861/article/details/80949005
今日推荐
周排行