Java实现PDF转图片

旧城等待, 2022-04-24 04:30 419阅读 0赞

注意事项:转图片后需仔细核对,因PDF内的字体可能不是宋体等最常见字体,转图片后可能出现中文乱码现象,需要查看原PDF文件中对应字体,下载后安装在本地或服务器上。

需引入jar依赖

  1. <dependency>
  2. <groupId>org.apache.pdfbox</groupId>
  3. <artifactId>pdfbox</artifactId>
  4. <version>2.0.11</version>
  5. </dependency>

情景1:将PDF转成一页长图

  1. public static BufferedImage pdfToImage(InputStream inputStream) {
  2. //图像合并使用参数
  3. int width = 0; // 总宽度
  4. int[] singleImgRGB; // 保存一张图片中的RGB数据
  5. int shiftHeight = 0;
  6. BufferedImage imageResult = null;//保存每张图片的像素值
  7. try {
  8. //利用PdfBox生成图像
  9. PDDocument pdDocument = PDDocument.load(inputStream);
  10. PDFTextStripper text = new PDFTextStripper();
  11. String text1 = text.getText(pdDocument);
  12. logger.info("读取PDF文字:" + text1);
  13. PDFRenderer renderer = new PDFRenderer(pdDocument);
  14. //循环每个页码
  15. for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
  16. //dpi参数越大,越清晰
  17. BufferedImage image = renderer.renderImageWithDPI(i, 300, ImageType.RGB);
  18. int imageHeight = image.getHeight();
  19. int imageWidth = image.getWidth();
  20. if (i == 0) {//计算高度和偏移量
  21. width = imageWidth;//使用第一张图片宽度;
  22. //保存每页图片的像素值
  23. imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
  24. } else {
  25. shiftHeight += imageHeight; // 计算偏移高度
  26. }
  27. singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
  28. imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写入流中
  29. }
  30. pdDocument.close();
  31. } catch (Exception e) {
  32. logger.error(" 从PDF转换JPG格式异常!", e);
  33. }
  34. return imageResult;
  35. }
  36. public static void main(String[] args) {
  37. InputStream inputStream = null;
  38. BufferedImage bufferedImage = null;
  39. String url = "";
  40. try {
  41. //pdf转img
  42. bufferedImage = PDFUtils.pdfToImage(new URL(url));
  43. ByteArrayOutputStream os = new ByteArrayOutputStream();
  44. ImageIO.write(bufferedImage, "png", os);
  45. inputStream = new ByteArrayInputStream(os.toByteArray());
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }

得到流后就可以下载到本地或者转存其他空间

情景2:将PDF文件转为图片,一页PDF对应一张图片,最终将图片打成zip包

  1. public static ByteArrayOutputStream pdfToZip(String fileUrl, String fileName) {
  2. ByteArrayOutputStream bos = null;
  3. try {
  4. DataInputStream inputStream = new DataInputStream(new URL(fileUrl).openStream());
  5. PDDocument pdDocument = PDDocument.load(inputStream);
  6. PDFRenderer renderer = new PDFRenderer(pdDocument);
  7. bos = new ByteArrayOutputStream();
  8. ZipOutputStream zos = new ZipOutputStream(bos);
  9. byte[] bytes = null;
  10. //循环每个页码,一页都不能缺,所以内部不try-catch
  11. for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
  12. BufferedImage image = renderer.renderImageWithDPI(i, 300);
  13. ByteArrayOutputStream temp = new ByteArrayOutputStream();
  14. ImageIO.write(image, "png", temp);
  15. zos.putNextEntry(new ZipEntry(fileName + i + ".png"));
  16. bytes = temp.toByteArray();
  17. zos.write(bytes, 0, bytes.length);
  18. zos.closeEntry();
  19. temp.close();
  20. }
  21. zos.close();
  22. pdDocument.close();
  23. } catch (Exception e) {
  24. logger.error(" 从PDF转换PNG/ZIP格式异常!", e);
  25. }
  26. return bos;
  27. }
  28. public static void main(String[] args) {
  29. ByteArrayOutputStream os = pdfToZip(url, fileName);
  30. InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
  31. }

发表评论

表情:
评论列表 (有 0 条评论,419人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java实现PDF图片

    注意事项:转图片后需仔细核对,因PDF内的字体可能不是宋体等最常见字体,转图片后可能出现中文乱码现象,需要查看原PDF文件中对应字体,下载后安装在本地或服务器上。 需引入ja