Java實(shí)現(xiàn)pdf轉(zhuǎn)圖片案例
工程加入依賴:
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.15</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-tools</artifactId> <version>2.0.15</version> </dependency>
pdf文件轉(zhuǎn)圖片:
public static List<String> pdf2Img(File pdfFile) {
if (pdfFile == null || !pdfFile.exists()) {
throw new RuntimeException("pdf文件不能為空");
}
String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
String targetPath = pdfFile.getParent() + File.separator + name;
List<String> imgList = new ArrayList<>();
try {
PDDocument doc = PDDocument.load(pdfFile);
// 頁(yè)數(shù)
int pageCount = doc.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(doc);
for (int i = 0; i < pageCount; i++) {
File targetFile = new File(targetPath + File.separator + name + "-" + (i + 1) + ".jpg");
if (!targetFile.getParentFile().exists()) {
FileUtil.mkdir(targetFile.getParentFile());
}
pdfRenderer.renderImage(i);
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 105, ImageType.RGB);
ImageIOUtil.writeImage(image, targetFile.getPath(), 105);
imgList.add(targetFile.getPath());
}
} catch (IOException e) {
log.error("文件轉(zhuǎn)換異常", e);
throw new RuntimeException("文件轉(zhuǎn)換異常,err=" + e.getMessage());
}
pdf轉(zhuǎn)成一張圖片:
/**
* pdf轉(zhuǎn)成一張圖片
*
* @param pdfFile pdf圖片文件
* @return 圖片地址
*/
public static String pdf2OneImg(File pdfFile) {
List<String> imgs = pdf2Img(pdfFile);
int len = imgs.size();
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
src[i] = new File(imgs.get(i));
if (!src[i].exists()) {
throw new RuntimeException("文件【" + imgs.get(i) + "】不存在");
}
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
log.error("", e);
throw new RuntimeException(e);
}
int width = images[i].getWidth();
int height = images[i].getHeight();
// 從圖片中讀取RGB 像素
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int dst_height = 0;
int dst_width = images[0].getWidth();
// 合成圖片像素
for (int i = 0; i < images.length; i++) {
dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
dst_height += images[i].getHeight();
}
if (dst_height < 1) {
throw new RuntimeException("文件合成失敗,合成后的圖片文件高度=" + dst_height);
}
String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
String targetPath = pdfFile.getParent() + File.separator + name;
// 輸出路徑
File outFile = new File(targetPath + File.separator + name + "-bigone.jpg");
// 生成新圖片
try {
dst_width = images[0].getWidth();
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}
ImageIO.write(ImageNew, "jpg", outFile);
} catch (Exception e) {
log.error("圖片合并異常=", e);
throw new RuntimeException(e);
}
return outFile.getPath();
}
到此這篇關(guān)于Java實(shí)現(xiàn)pdf轉(zhuǎn)圖片案例的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)pdf轉(zhuǎn)圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量
在Linux系統(tǒng)中,配置JDK環(huán)境變量是非常重要的,它可以讓你在終端中直接使用Java命令,這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下2024-01-01
Java實(shí)現(xiàn)雙鏈表互相交換任意兩個(gè)節(jié)點(diǎn)的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)雙鏈表互相交換任意兩個(gè)節(jié)點(diǎn)的方法,簡(jiǎn)單講述了雙鏈表的概念,并結(jié)合實(shí)例形式給出了java雙鏈表實(shí)現(xiàn)任意兩個(gè)節(jié)點(diǎn)交換的操作技巧,需要的朋友可以參考下2017-11-11
Java實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)的示例代碼
由于業(yè)務(wù)需要,后端需要返回一個(gè)樹(shù)型結(jié)構(gòu)給前端,包含父子節(jié)點(diǎn)的數(shù)據(jù)已經(jīng)在數(shù)據(jù)庫(kù)中存儲(chǔ)好。本文將為大家分享Java現(xiàn)樹(shù)形結(jié)構(gòu)的示例代碼,需要的可以參考下2022-05-05
SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版)
這篇文章主要介紹了SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
SpringMVC解析post請(qǐng)求參數(shù)詳解
今天小編就為大家分享一篇解決SpringMVC接收不到ajaxPOST參數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-08-08
Java程序去調(diào)用并執(zhí)行shell腳本及問(wèn)題總結(jié)(推薦)
這篇文章主要介紹了Java程序去調(diào)用并執(zhí)行shell腳本及問(wèn)題總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
springboot整合websocket最基礎(chǔ)入門(mén)使用教程詳解
這篇文章主要介紹了springboot整合websocket最基礎(chǔ)入門(mén)使用教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java實(shí)現(xiàn)五子棋網(wǎng)絡(luò)版
這篇文章主要為大家詳細(xì)介紹了基于Java編寫(xiě)的網(wǎng)絡(luò)五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

