Case of Java realizing pdf to picture

  • 2021-10-15 10:47:26
  • OfStack

Project add dependencies:


<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 file to picture:


    public static List<String> pdf2Img(File pdfFile) {
        if (pdfFile == null || !pdfFile.exists()) {
            throw new RuntimeException("pdf File cannot be empty ");
        }
        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);
            //  Number of pages 
            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(" File conversion exception ", e);
            throw new RuntimeException(" File conversion exception, err=" + e.getMessage());
        }

pdf to 1 picture:


    /**
     * pdf Turn into 1 A picture 
     *
     * @param pdfFile pdf Picture file 
     * @return  Picture address 
     */
    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(" Document " " + imgs.get(i) + " "Does not exist ");
                }
                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();
            //  Read from a picture RGB  Pixel 
            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();
        //  Composite picture pixels 
        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(" File synthesis failed, the height of the synthesized picture file =" + dst_height);
        }
        String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
        String targetPath = pdfFile.getParent() + File.separator + name;
        //  Output path 
        File outFile = new File(targetPath + File.separator + name + "-bigone.jpg");
        //  Generate a new picture 
        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(" Image merge exception =", e);
            throw new RuntimeException(e);
        }
        return outFile.getPath();
    }

Related articles: