• 使用pdfbox分页保存pdf为图片


    一、背景


      pdfbox作为Apache开源的PDF操作工具,允许创建新的PDF文档,操作现有文档,以及从文档中提取内容的能力。Apache PDFBox还包括一些命令行实用工具。本文楼主主要介绍其中的PDF转图片的功能,有其他功能需求的同学,可以去官网读读文档,https://pdfbox.apache.org/

    二、准备工作


      只需两个jar,pdfbox-2.0.7.jar,font-box-2.0.7.jar,当然用maven或gradle的同学,只需引入pdfbox就行了,依赖添加,楼主给大家准备在下面,直接取就OK。

      gradle添加依赖:

    1 //添加pdfbox的依赖
    2 compile('org.apache.pdfbox:pdfbox:2.0.7')

      maven添加依赖:

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

      其次,就是准备pdf文档一份,用来解析。

    三、代码实现


      代码不是很多,不超过100行,楼主给出了很全面的注释:

     1 package cn.apache.poi.pdf;
     2 
     3 import org.apache.pdfbox.io.RandomAccessBuffer;
     4 import org.apache.pdfbox.pdfparser.PDFParser;
     5 import org.apache.pdfbox.pdmodel.PDDocument;
     6 import org.apache.pdfbox.pdmodel.PDPage;
     7 import org.apache.pdfbox.rendering.PDFRenderer;
     8 
     9 import javax.imageio.IIOImage;
    10 import javax.imageio.ImageIO;
    11 import javax.imageio.ImageWriter;
    12 import javax.imageio.stream.ImageOutputStream;
    13 import java.awt.image.BufferedImage;
    14 import java.io.*;
    15 import java.util.Iterator;
    16 
    17 /**
    18  * Created by Jon_China on 2017/7/30.
    19  */
    20 public class Pdf2Image {
    21     public final  static String  IMG_TYPE_JPG = "jpg";
    22     public final  static String  IMG_TYPE_PNG = "png";
    23     public static void main( String[] args ) throws IOException{
    24         Pdf2Image pdf2Image = new Pdf2Image();
    25         pdf2Image.pdf2img("E:\java测试\java测试\程序1\待转换文件\待转换pdf.pdf", "D:",IMG_TYPE_PNG);
    26     }
    27 
    28 
    29     /**
    30      * PDF转图片
    31      * @param pdfPath pdf文件的路径
    32      * @param savePath 图片保存的地址
    33      * @param imgType 图片保存方式
    34      */
    35     public void pdf2img(String pdfPath,String savePath,String imgType){
    36         String fileName = pdfPath.substring(pdfPath.lastIndexOf("\")+1, pdfPath.length());
    37         fileName = fileName.substring(0,fileName.lastIndexOf("."));
    38         InputStream is = null;
    39         PDDocument pdDocument = null;
    40         try {
    41             is = new BufferedInputStream(new FileInputStream(pdfPath));
    42             //创建pdf文件解析器
    43             PDFParser parser = new PDFParser(new RandomAccessBuffer(is));
    44             parser.parse();
    45             //获取解析后的pdf文档
    46             pdDocument = parser.getPDDocument();
    47             //获取pdf渲染器,主要用来后面获取BufferedImage
    48             PDFRenderer renderer = new PDFRenderer(pdDocument);
    49             //获取pdf文件总页数
    50             int pageCount = pdDocument.getNumberOfPages();
    51             for (int i = 0; i < pageCount; i++) {
    52                 //构造保存文件名称格式
    53                 String saveFileName = savePath+"\"+fileName+"-"+i+"."+imgType;
    54                 //获取当前页对象
    55                 PDPage page =  pdDocument.getPage(i);
    56                 //图片转换
    57                 pdfPage2Img(page,saveFileName,imgType,renderer,i);
    58             }
    59         } catch (Exception e) {
    60             e.printStackTrace();
    61         }finally{
    62             if(pdDocument != null){
    63                 try {
    64                     pdDocument.close();
    65                 } catch (IOException e) {
    66                     e.printStackTrace();
    67                 }
    68             }
    69         }
    70     }
    71 
    72     /**
    73      * 将pdf单页转换为图片
    74      * @param page 当页对象
    75      * @param saveFileName 保存的图片名称
    76      * @param imgType 保存的图片类型
    77      * @param renderer 用于获取BufferedImage
    78      * @param index 页索引
    79      * @throws IOException
    80      */
    81     public void pdfPage2Img(PDPage page,String saveFileName,String imgType,PDFRenderer renderer,int index) throws IOException{
    82         //构造图片
    83         BufferedImage img_temp  = renderer.renderImage(index);
    84         //设置图片格式
    85         Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix(imgType);
    86         //将文件写出
    87         ImageWriter writer = (ImageWriter) it.next();
    88         ImageOutputStream imageout = ImageIO.createImageOutputStream(new FileOutputStream(saveFileName));
    89         writer.setOutput(imageout);
    90         writer.write(new IIOImage(img_temp, null, null));
    91     }
    92 }

      就是这么简单,源码地址,请戳https://github.com/LJunChina/MineKnowContainer/tree/master/pdf

  • 相关阅读:
    浅析嵌入式程序设计中的优化问题
    TCP粘包问题
    使用python 批量 配对t检验 医学 基础研究 数据分析
    Ubuntu误删系统文件修复办法
    飞思卡尔powerpc交叉编译环境的
    ubuntu packege下载网址
    数组对象里面对日期进行排序
    c# 字符串以逗号分割属性加上单引号
    Vue mysql 变量赋值, 获取数组
    Element vue Select 下拉框默认
  • 原文地址:https://www.cnblogs.com/qq503665965/p/7260069.html
Copyright © 2020-2023  润新知