依賴:

pdf存放路徑

正文開始:
pdf轉(zhuǎn)換多張圖片、長(zhǎng)圖
@Test
void pdf2Image() {
String dstImgFolder = "";
String PdfFilePath = "";
String relativelyPath=System.getProperty("user.dir");
PdfFilePath = relativelyPath + "/uploadTest/"+"文檔.pdf";
dstImgFolder = relativelyPath + "/uploadTest/";
? ? ? ? /* dpi越大轉(zhuǎn)換后越清晰,相對(duì)轉(zhuǎn)換速度越慢 */
int dpi = 450;
File file = new File(PdfFilePath);
PDDocument pdDocument; // 創(chuàng)建PDF文檔
try {
String imgPDFPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
String imagePDFName = file.getName().substring(0, dot); // 獲取圖片文件名
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
imgFolderPath = imgPDFPath + File.separator;// 獲取圖片存放的文件夾路徑
} else {
imgFolderPath = dstImgFolder + File.separator;
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath = null;
BufferedImage[] bufferedImages = new BufferedImage[pages];
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append(i + 1);
imgFilePath.append(".png");
// File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
bufferedImages[i] = image;
// ImageIO.write(image, "png", dstFile);
}
dstImgFolder = dstImgFolder + imagePDFName + ".png";
? ? ? ? ? ? ? ? // PDF文件全部頁數(shù)轉(zhuǎn)PNG圖片,若多張展示注釋即可 工具類貼在下面
ImageMergeUtil.mergeImage(bufferedImages, 2, dstImgFolder);
System.out.println("PDF文檔轉(zhuǎn)PNG圖片成功!");
} else {
System.out.println("PDF文檔轉(zhuǎn)PNG圖片失?。? + "創(chuàng)建" + imgFolderPath + "失敗");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
// ImageMergeUtil 圖片的合并,多張圖片合成長(zhǎng)圖
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageMergeUtil {
public static void main(String[] args) throws IOException {
String filePath = "D:\\temp\\ImageMergeUtil\\";
String path1 = filePath + "a.png";
String path2 = filePath + "b.png";
mergeImage(path1, path2, 2, filePath+"c.png");
}
/**
* 圖片拼接
* @param path1 圖片1路徑
* @param path2 圖片2路徑
* @param type 1 橫向拼接, 2 縱向拼接
* (注意:必須兩張圖片長(zhǎng)寬一致)
*/
public static void mergeImage( String path1, String path2, int type, String targetFile) throws IOException {
File file1 = new File(path1);
File file2 = new File(path2);
//兩張圖片的拼接
int len = 2;
BufferedImage[] images = new BufferedImage[len];
images[0] = ImageIO.read(file1);
images[1] = ImageIO.read(file2);
mergeImage(images, type, targetFile);
}
/**
* 圖片拼接
* @param images 圖片數(shù)組
* @param type 1 橫向拼接, 2 縱向拼接
* (注意:必須兩張圖片長(zhǎng)寬一致)
*/
public static void mergeImage(BufferedImage[] images, int type, String targetFile) throws IOException {
int len = images.length;
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
int width = images[i].getWidth();
int height = images[i].getHeight();
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int newHeight = 0;
int newWidth = 0;
for (int i = 0; i < images.length; i++) {
// 橫向
if (type == 1) {
newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
newWidth += images[i].getWidth();
} else if (type == 2) {// 縱向
newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
newHeight += images[i].getHeight();
}
}
if (type == 1 && newWidth < 1) {
return;
}
if (type == 2 && newHeight < 1) {
return;
}
// 生成新圖片
try {
BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
int width_i = 0;
for (int i = 0; i < images.length; i++) {
if (type == 1) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
images[i].getWidth());
width_i += images[i].getWidth();
} else if (type == 2) {
ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
height_i += images[i].getHeight();
}
}
//輸出想要的圖片
ImageIO.write(ImageNew, "png", new File(targetFile));
} catch (Exception e) {
e.printStackTrace();
}
}
展示效果:文章來源:http://www.zghlxwxcb.cn/news/detail-738346.html

附加:小程序預(yù)覽wxml代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-738346.html

到了這里,關(guān)于JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!