• JAVA 实现PDF转图片(pdfbox版)


    依赖:

    pdf存放路径

    正文开始:

    pdf转换多张图片、长图

    @Test
    void pdf2Image() {
            String dstImgFolder = "";
            String PdfFilePath = "";
     
            String relativelyPath=System.getProperty("user.dir");
            PdfFilePath = relativelyPath + "/uploadTest/"+"文档.pdf";
            dstImgFolder = relativelyPath + "/uploadTest/";
            /* dpi越大转换后越清晰,相对转换速度越慢 */
            int dpi = 450;
            File file = new File(PdfFilePath);
            PDDocument pdDocument; // 创建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文件全部页数转PNG图片,若多张展示注释即可 工具类贴在下面
                   ImageMergeUtil.mergeImage(bufferedImages, 2, dstImgFolder);
                    System.out.println("PDF文档转PNG图片成功!");
                } else {
                    System.out.println("PDF文档转PNG图片失败:" + "创建" + 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();
            }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    // ImageMergeUtil 图片的合并,多张图片合成长图
    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 纵向拼接
         * (注意:必须两张图片长宽一致)
         */
        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     图片数组
         * @param type      1 横向拼接, 2 纵向拼接
         * (注意:必须两张图片长宽一致)
         */
        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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    展示效果:

    附加:小程序预览wxml代码

  • 相关阅读:
    RuoYi-App启动教程
    API经济下新物种的诞生
    Desthiobiotin衍生物Desthiobiotin-PEG4-Amine/Alkyne/Azide/DBCO
    LCR 157. 套餐内商品的排列顺序
    Error checking compiler version for cl: [WinError 2] 系统找 不到指定的文件[已解决]
    快速上手 TypeScript
    anaconda 安装 pytorch 和 tensorflow
    深度理解Synchronized及底层原理
    node笔记_koa框架是什么?
    kubernetes问题(一)-异常事件
  • 原文地址:https://blog.csdn.net/zhangjunli/article/details/134160345