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();
			}
		}
	}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM