Java多张图片合成PDF
Java多张图⽚合成PDF
import Document;
import Image;
import Rectangle;
我再也不愿见你在深夜里买醉
import pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.log4j.Logger;
/**
* 图⽚转pdf⼯具类
*
* @author Administrator
*
*/
public class Img2PdfUtil {
private static Logger logger = Logger(Img2PdfUtil.class);
/**
*
* @param outPdfFilepath ⽣成pdf⽂件路径
* @param imageFiles 需要转换的图⽚File类Array,按array的顺序合成图⽚
*/
public static void imagesToPdf(String outPdfFilepath, File[] imageFiles) throws Exception {
简单有内涵的网名logger.info("进⼊图⽚合成PDF⼯具⽅法");
File file = new File(outPdfFilepath);
// 第⼀步:创建⼀个document对象。
Document document = new Document();
document.setMargins(0, 0, 0, 0);
// 第⼆步:
// 创建⼀个PdfWriter实例,
// 第三步:打开⽂档。
document.open();
// 第四步:在⽂档中增加图⽚。
int len = imageFiles.length;
for (int i = 0; i < len; i++) {
if (imageFiles[i].getName().toLowerCase().endsWith(".bmp")
未在网络上注册
|| imageFiles[i].getName().toLowerCase().endsWith(".jpg")
春天的风能否吹来夏天的雨是什么歌
|| imageFiles[i].getName().toLowerCase().endsWith(".jpeg")
|| imageFiles[i].getName().toLowerCase().endsWith(".gif")
|| imageFiles[i].getName().toLowerCase().endsWith(".png")) {
String temp = imageFiles[i].getAbsolutePath();
logger.info("图⽚路径:"+temp);
Image img = Instance(temp);
img.setAlignment(Image.ALIGN_CENTER);
农业银行贷款利率表
img.scaleAbsolute(597, 844);// 直接设定显⽰尺⼨
// 根据图⽚⼤⼩设置页⾯,⼀定要先设置页⾯,再newPage(),否则⽆效
//document.setPageSize(new Width(), Height()));
document.setPageSize(new Rectangle(597, 844));
document.add(img);
}
}
// 第五步:关闭⽂档。
document.close();
logger.info("图⽚合成PDF完成");
}
public static void main(String[] args) {
String outPdfPath = "C:\\Users\\Administrator\\Desktop\\⼴东\\需求\\20190104已确认需求\\Img2PdfTest\\Img2pdf.pdf";        String imagesPath = "C:\\Users\\Administrator\\Desktop\\⼴东\\需求\\20190104已确认需求\\Img2PdfTest";专升本的大学
String[] imgNameArray = new String[] { "1_0.png", "1_1.png", "1_2.png", "1_3.png", "1_4.png", "1_5.png",
"1_6.png", "1_7.png", "1_8.png", "1_9.png", "1_10.png", "1_11.png", };
// imagesToPdf(outPdfPath, imagesPath, imgNameArray);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。