Project join dependencies:
<dependency> <groupId></groupId> <artifactId>pdfbox</artifactId> <version>2.0.15</version> </dependency> <dependency> <groupId></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 || !()) { throw new RuntimeException("The pdf file cannot be empty"); } String name = ().substring(0, ().lastIndexOf(".")); String targetPath = () + + name; List<String> imgList = new ArrayList<>(); try { PDDocument doc = (pdfFile); // Number of pages int pageCount = (); PDFRenderer pdfRenderer = new PDFRenderer(doc); for (int i = 0; i < pageCount; i++) { File targetFile = new File(targetPath + + name + "-" + (i + 1) + ".jpg"); if (!().exists()) { (()); } (i); BufferedImage image = (i, 105, ); (image, (), 105); (()); } } catch (IOException e) { ("File conversion exception", e); throw new RuntimeException("File conversion exception,err=" + ()); }
Convert pdf to a picture:
/** * pdf to a picture * * @param pdfFile pdf image file * @return Image address */ public static String pdf2OneImg(File pdfFile) { List<String> imgs = pdf2Img(pdfFile); int len = (); 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((i)); if (!src[i].exists()) { throw new RuntimeException("document【" + (i) + "】Don't exist"); } images[i] = (src[i]); } catch (Exception e) { ("", e); throw new RuntimeException(e); } int width = images[i].getWidth(); int height = images[i].getHeight(); // Read RGB pixels from the picture 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(); // Synthesize image pixels for (int i = 0; i < ; 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 composite image file=" + dst_height); } String name = ().substring(0, ().lastIndexOf(".")); String targetPath = () + + name; // Output path File outFile = new File(targetPath + + name + "-"); // Generate new pictures 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 < ; i++) { (0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width); height_i += images[i].getHeight(); } (ImageNew, "jpg", outFile); } catch (Exception e) { ("Image merging exception=", e); throw new RuntimeException(e); } return (); }
This is the article about Java implementation of pdf to image conversion. For more related Java implementation of pdf to image conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!