日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

java 图片合并成pdf_Java多张图片合成PDF

發(fā)布時(shí)間:2023/12/20 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 图片合并成pdf_Java多张图片合成PDF 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

importcom.itextpdf.text.Document;importcom.itextpdf.text.Image;importcom.itextpdf.text.Rectangle;importcom.itextpdf.text.pdf.PdfWriter;importjava.io.File;importjava.io.FileOutputStream;importorg.apache.log4j.Logger;/*** 圖片轉(zhuǎn)pdf工具類

*

*@authorAdministrator

**/

public classImg2PdfUtil {private static Logger logger = Logger.getLogger(Img2PdfUtil.class);/***

*@paramoutPdfFilepath 生成pdf文件路徑

*@paramimageFiles 需要轉(zhuǎn)換的圖片F(xiàn)ile類Array,按array的順序合成圖片*/

public static void imagesToPdf(String outPdfFilepath, File[] imageFiles) throwsException {

logger.info("進(jìn)入圖片合成PDF工具方法");

File file= newFile(outPdfFilepath);//第一步:創(chuàng)建一個(gè)document對(duì)象。

Document document = newDocument();

document.setMargins(0, 0, 0, 0);//第二步://創(chuàng)建一個(gè)PdfWriter實(shí)例,

PdfWriter.getInstance(document, newFileOutputStream(file));//第三步:打開(kāi)文檔。

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=Image.getInstance(temp);

img.setAlignment(Image.ALIGN_CENTER);

img.scaleAbsolute(597, 844);//直接設(shè)定顯示尺寸//根據(jù)圖片大小設(shè)置頁(yè)面,一定要先設(shè)置頁(yè)面,再newPage(),否則無(wú)效//document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));

document.setPageSize(new Rectangle(597, 844));

document.newPage();

document.add(img);

}

}//第五步:關(guān)閉文檔。

document.close();

logger.info("圖片合成PDF完成");

}public static voidmain(String[] args) {

String outPdfPath= "C:\\Users\\Administrator\\Desktop\\廣東\\需求\\20190104已確認(rèn)需求\\Img2PdfTest\\Img2pdf.pdf";

String imagesPath= "C:\\Users\\Administrator\\Desktop\\廣東\\需求\\20190104已確認(rèn)需求\\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);

}

}

總結(jié)

以上是生活随笔為你收集整理的java 图片合并成pdf_Java多张图片合成PDF的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。