JAVA 实现PDF转图片(pdfbox版)

超、凢脫俗 2024-02-20 11:39 192阅读 0赞

依赖:

826344e6064479f486f57cadc0abe313.png

pdf存放路径

ef317d765fa305f313141b311431aeee.png

正文开始:

pdf转换多张图片、长图

  1. @Test
  2. void pdf2Image() {
  3. String dstImgFolder = "";
  4. String PdfFilePath = "";
  5. String relativelyPath=System.getProperty("user.dir");
  6. PdfFilePath = relativelyPath + "/uploadTest/"+"文档.pdf";
  7. dstImgFolder = relativelyPath + "/uploadTest/";
  8. /* dpi越大转换后越清晰,相对转换速度越慢 */
  9. int dpi = 450;
  10. File file = new File(PdfFilePath);
  11. PDDocument pdDocument; // 创建PDF文档
  12. try {
  13. String imgPDFPath = file.getParent();
  14. int dot = file.getName().lastIndexOf('.');
  15. String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
  16. String imgFolderPath = null;
  17. if (dstImgFolder.equals("")) {
  18. imgFolderPath = imgPDFPath + File.separator;// 获取图片存放的文件夹路径
  19. } else {
  20. imgFolderPath = dstImgFolder + File.separator;
  21. }
  22. if (createDirectory(imgFolderPath)) {
  23. pdDocument = PDDocument.load(file);
  24. PDFRenderer renderer = new PDFRenderer(pdDocument);
  25. PdfReader reader = new PdfReader(PdfFilePath);
  26. int pages = reader.getNumberOfPages();
  27. StringBuffer imgFilePath = null;
  28. BufferedImage[] bufferedImages = new BufferedImage[pages];
  29. for (int i = 0; i < pages; i++) {
  30. String imgFilePathPrefix = imgFolderPath + File.separator;
  31. imgFilePath = new StringBuffer();
  32. imgFilePath.append(imgFilePathPrefix);
  33. imgFilePath.append("_");
  34. imgFilePath.append(i + 1);
  35. imgFilePath.append(".png");
  36. // File dstFile = new File(imgFilePath.toString());
  37. BufferedImage image = renderer.renderImageWithDPI(i, dpi);
  38. bufferedImages[i] = image;
  39. // ImageIO.write(image, "png", dstFile);
  40. }
  41. dstImgFolder = dstImgFolder + imagePDFName + ".png";
  42. // PDF文件全部页数转PNG图片,若多张展示注释即可 工具类贴在下面
  43. ImageMergeUtil.mergeImage(bufferedImages, 2, dstImgFolder);
  44. System.out.println("PDF文档转PNG图片成功!");
  45. } else {
  46. System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
  47. }
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. private static boolean createDirectory(String folder) {
  53. File dir = new File(folder);
  54. if (dir.exists()) {
  55. return true;
  56. } else {
  57. return dir.mkdirs();
  58. }
  59. }
  60. // ImageMergeUtil 图片的合并,多张图片合成长图
  61. import javax.imageio.ImageIO;
  62. import java.awt.image.BufferedImage;
  63. import java.io.File;
  64. import java.io.IOException;
  65. public class ImageMergeUtil {
  66. public static void main(String[] args) throws IOException {
  67. String filePath = "D:\\temp\\ImageMergeUtil\\";
  68. String path1 = filePath + "a.png";
  69. String path2 = filePath + "b.png";
  70. mergeImage(path1, path2, 2, filePath+"c.png");
  71. }
  72. /**
  73. * 图片拼接
  74. * @param path1 图片1路径
  75. * @param path2 图片2路径
  76. * @param type 1 横向拼接, 2 纵向拼接
  77. * (注意:必须两张图片长宽一致)
  78. */
  79. public static void mergeImage( String path1, String path2, int type, String targetFile) throws IOException {
  80. File file1 = new File(path1);
  81. File file2 = new File(path2);
  82. //两张图片的拼接
  83. int len = 2;
  84. BufferedImage[] images = new BufferedImage[len];
  85. images[0] = ImageIO.read(file1);
  86. images[1] = ImageIO.read(file2);
  87. mergeImage(images, type, targetFile);
  88. }
  89. /**
  90. * 图片拼接
  91. * @param images 图片数组
  92. * @param type 1 横向拼接, 2 纵向拼接
  93. * (注意:必须两张图片长宽一致)
  94. */
  95. public static void mergeImage(BufferedImage[] images, int type, String targetFile) throws IOException {
  96. int len = images.length;
  97. int[][] ImageArrays = new int[len][];
  98. for (int i = 0; i < len; i++) {
  99. int width = images[i].getWidth();
  100. int height = images[i].getHeight();
  101. ImageArrays[i] = new int[width * height];
  102. ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
  103. }
  104. int newHeight = 0;
  105. int newWidth = 0;
  106. for (int i = 0; i < images.length; i++) {
  107. // 横向
  108. if (type == 1) {
  109. newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
  110. newWidth += images[i].getWidth();
  111. } else if (type == 2) {// 纵向
  112. newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
  113. newHeight += images[i].getHeight();
  114. }
  115. }
  116. if (type == 1 && newWidth < 1) {
  117. return;
  118. }
  119. if (type == 2 && newHeight < 1) {
  120. return;
  121. }
  122. // 生成新图片
  123. try {
  124. BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
  125. int height_i = 0;
  126. int width_i = 0;
  127. for (int i = 0; i < images.length; i++) {
  128. if (type == 1) {
  129. ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
  130. images[i].getWidth());
  131. width_i += images[i].getWidth();
  132. } else if (type == 2) {
  133. ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
  134. height_i += images[i].getHeight();
  135. }
  136. }
  137. //输出想要的图片
  138. ImageIO.write(ImageNew, "png", new File(targetFile));
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. }
  142. }

展示效果:

4d5842289b49aa77324ba22d9f0ba563.png

附加:小程序预览wxml代码

16cbbf6022d04288cdf95001a3785cec.png

发表评论

表情:
评论列表 (有 0 条评论,192人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java实现PDF图片

    注意事项:转图片后需仔细核对,因PDF内的字体可能不是宋体等最常见字体,转图片后可能出现中文乱码现象,需要查看原PDF文件中对应字体,下载后安装在本地或服务器上。 需引入ja