Java将PDF转换成图片

这里介绍两种将PDF转换成图片的方式

一、使用icepdf

     下载导入jar包,jar包地址:http://download.csdn.net/download/u010782875/10041519

      代码实现:

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

/**
 * @ClassName:PdfToImageIcePdf
 * @Description:
 * @author:
 * @data:2017/10/25
 */
public class PdfToImageIcePdf {
    public static void main(final String[] args) {
        String filePath = "D://Test/xxx.pdf"
        List<String> imageList = getPdfToImage(filePath);
        Iterator<String> iterator = imageList.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    //返回生成图片的路径
    public static List<String> getPdfToImage(String filePath) {
        String fileName = filePath.substring(0,filePath.lastIndexOf("."));//获取去除后缀的文件路径
        List<String> list = new ArrayList<>();
        String imagePath;
        Document document = new Document();
        try{
            document.setFile(filePath);
        }catch (Exception e){
            e.printStackTrace();
        }

        float scale = 1.3f;// 缩放比例
        float rotation = 0f;// 旋转角度

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage) document.getPageImage(i,
                    GraphicsRenderingHints.SCREEN,
                    org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation,
                    scale);
            RenderedImage rendImage = image;
            try {
                int n = i + 1;
                File f = new File(fileName);
                if(!f.exists()){
                    f.mkdir();
                }
                imagePath = fileName + "/image"+ n + ".jpg";//生成图片的路径
                File file = new File(imagePath);
                ImageIO.write(rendImage, "jpg", file);
                // 这里png作用是:格式是jpg但有png清晰度
                // ImageIO.write(rendImage, "png", file);
                list.add(imagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
        }
        document.dispose();
        return list;
    }

         该方法在main()可以正常运行,但是放入action中会报错 “Java.lang.NoClassDefFoundError:org/icepdf/core/util/content/b”

二、使用pdfbox

        下载导入jar包,jar包地址:http://download.csdn.net/download/u010782875/10041527

         代码实现:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName:PdfToImagePdfBox
 * @Description:
 * @author:
 * @data:2017/10/27
 */
public class PdfToImagePdfBox {
    public static void main(String[] args){
        String filePath = "D://Test/xxx.pdf";
        List<String> imageList = pdfToImagePath(filePath);
        Iterator<String> iterator = imageList.iterator();
        while(iterator.hasNext()){

            System.out.println(iterator.next());
        }
//        pdfToImage(filePath);
    }
    public static List<String> pdfToImagePath(String filePath){
        List<String> list = new ArrayList<>();
        String fileDirectory = filePath.substring(0,filePath.lastIndexOf("."));//获取去除后缀的文件路径

        String imagePath;
        File file = new File(filePath);
        try {
            File f = new File(fileDirectory);
            if(!f.exists()){
                f.mkdir();
            }
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for(int i=0; i<pageCount; i++){
                // 方式1,第二个参数是设置缩放比(即像素)
                // BufferedImage image = renderer.renderImageWithDPI(i, 296);
                // 方式2,第二个参数是设置缩放比(即像素)
                BufferedImage image = renderer.renderImage(i, 1.25f);  //第二个参数越大生成图片分辨率越高,转换时间也就越长
                imagePath = fileDirectory + "/"+i + ".jpg";
                ImageIO.write(image, "PNG", new File(imagePath));
                list.add(imagePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

该方法在action中可以正常运行




  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值