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)文章
java8新特性之stream流中reduce()求和知識(shí)總結(jié)
今天帶大家回顧Java8的新特性,文中對(duì)stream流中reduce()求和的相關(guān)知識(shí)作了詳細(xì)的介紹,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
Java SpringSecurity入門案例與基本原理詳解
這篇文章主要介紹了java中Spring Security的實(shí)例詳解的相關(guān)資料,spring security是一個(gè)多方面的安全認(rèn)證框架,提供了基于JavaEE規(guī)范的完整的安全認(rèn)證解決方案,需要的朋友可以參考下2021-09-09
學(xué)習(xí)Java之如何對(duì)時(shí)間進(jìn)行格式化
當(dāng)我們?cè)谀J(rèn)情況下構(gòu)造出來的時(shí)間對(duì)象,它的時(shí)間格式并不適合我們閱讀,并且在開發(fā)時(shí),pc端、Android端、iOS端等展示的時(shí)間格式可能也并不完全一樣,本文就從這幾個(gè)問題給大家介紹如何對(duì)時(shí)間進(jìn)行格式化,感興趣的同學(xué)可以借鑒一下2023-05-05
SpringBoot使用AOP實(shí)現(xiàn)日志記錄功能詳解
這篇文章主要為大家介紹了SpringBoot使用AOP實(shí)現(xiàn)日志記錄功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Java解決線程的不安全問題之volatile關(guān)鍵字詳解
這篇文章主要介紹了Java解決線程的不安全問題之volatile關(guān)鍵字詳解,可見性指一個(gè)線程對(duì)共享變量值的修改,能夠及時(shí)地被其他線程看到,而 volatile 關(guān)鍵字就保證內(nèi)存的可見性,需要的朋友可以參考下2023-08-08

