pdfbox pdf转图片

public void pdf2Image(File sourceFile, File destFile) {
		if (sourceFile.exists()) {
			try {
				PDDocument doc = PDDocument.load(sourceFile);
				PDFRenderer renderer = new PDFRenderer(doc);
				int pageCount = doc.getNumberOfPages();
				BufferedImage imageNew = null;
				for (int i = 0; i < pageCount; i++) {
					BufferedImage image = renderer.renderImageWithDPI(i, 284);
					int width = image.getWidth();
					int height = image.getHeight();
					if (imageNew == null) {
						imageNew = new BufferedImage(width, (height + SEPARATE_DISTANCE) * pageCount,
								BufferedImage.TYPE_INT_RGB);
					}
					int[] imageRgbArray = new int[width * height];
					imageRgbArray = image.getRGB(0, 0, width, height, imageRgbArray, 0, width);
					imageNew.setRGB(0, (height + SEPARATE_DISTANCE) * i, width, height, imageRgbArray, 0, width);// SEPARATE_DISTANCE表示两张图片的间隔距离
				}
				ImageIO.write(imageNew, "PNG", destFile);
		  
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

  

猜你喜欢

转载自www.cnblogs.com/moonlignt/p/9233763.html