1.依赖
<!--pdf转jpg--> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.4</version> </dependency>
2.代码
/** * pdf转jpg * PDDocument.load(file)有多个重载方法 */ public List<BufferedImage> convertToImage(byte[] file) throws IOException { PDDocument document = PDDocument.load(file); PDFRenderer pdfRenderer = new PDFRenderer(document); List<BufferedImage> bufferedImageList = new ArrayList<>(); for (int page = 0;page<document.getNumberOfPages();page++){ BufferedImage img = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); bufferedImageList.add(img); } document.close(); return bufferedImageList; } /** * 多页合并 */ public BufferedImage concat(List<BufferedImage> images) { int heightTotal = 0; for(int j = 0; j < images.size(); j++) { heightTotal += images.get(j).getHeight(); } int heightCurr = 0; BufferedImage concatImage = new BufferedImage(images.get(0).getWidth(), heightTotal, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = concatImage.createGraphics(); for(int j = 0; j < images.size(); j++) { g2d.drawImage(images.get(j), 0, heightCurr, null); heightCurr += images.get(j).getHeight(); } g2d.dispose(); return concatImage; }