Java:PDF图片抽取的两种方法

        图片和PDF是我们日常生活和工作中经常接触到的文档格式。PDF是人们日常使用最多的跨平台文档,是一种用独立于应用程序、硬件、操作系统的方式呈现文档的文件格式。每个PDF文件包含固定布局的平面文档的完整描述,包括文本、字形、图形及其他需要显示的信息。然而,从图片或PDF中提取出关键内容并不是一件简单的事情。

        最近开发项目需要抽取PDF中的图片,做了一些研究,记录一下!PDF中的图片分为两种,一种是传统意义上的图片,可以直接进行抽取;另一种是各种图形的组合,这种图片不能够直接进行抽取。

1、方法一:可以直接从PDF中抽取的图片

1.1 Maven引入

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.9</version>
    <type>jar</type>
</dependency>

1.2 代码
 


    public static List<String> extractImage(String pdfPath,Integer pdfPage,String picPath) {
        FileInputStream fis = null;
        PDDocument document = null;
        List<String> imageUrls = new ArrayList<>();
        try {
            File file = new File( picPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            // 打开pdf文件流
            fis = new FileInputStream(pdfPath);
            // 加载 pdf 文档,获取PDDocument文档对象
            document = PDDocument.load(fis);
            PDPage page = document.getPage(pdfPage);
            PDResources resources = page.getResources();
            Iterable<COSName> xObjectNames = resources.getXObjectNames();
            int i = 1;
            if (xObjectNames != null){
                Iterator<COSName> iterator = xObjectNames.iterator();
                while (iterator.hasNext()){
                    COSName key =  iterator.next();
                    if (resources.isImageXObject(key)){
                        PDImageXObject image = (PDImageXObject) resources.getXObject(key);
                        BufferedImage bImage = image.getImage();
                        String imageUrl = picPath + File.separator + pdfPage + "-" + i + "."+ image.getSuffix();
                        ImageIO.write(bImage, image.getSuffix(), new File(imageUrl));
                        imageUrls.add(imageUrl);
                    }
                    i++;
                }
            }
            fis.close();
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("有异常图片");
        }
        return imageUrls;
    }

2、方法二:图形的组合,截图

2.1 Maven引入

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.17</version>
</dependency>

2.2 代码 

 /**
     * 将PDF文档拆分成多张图片,并返回所有图片的路径
     *
     * @param pdfPath
     * @param pictureFolderPath
     * @return
     * @throws Exception
     */
    public static List<String> pdfSwitchToPicture(String pdfPath, String pictureFolderPath,Integer startPage,Integer endPage) {
        List<String> picUrlList = new ArrayList<>();
        File file = new File(pictureFolderPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            List<byte[]> imageList = handlePdf(pdfPath,startPage,endPage);
            AtomicInteger pictureNameNumber = new AtomicInteger(startPage);
            for (byte[] image : imageList) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byteArrayOutputStream.write(image);
                String pictureUrl = file.getAbsolutePath() + File.separator + pictureNameNumber.getAndIncrement() + ".jpg";
                byteArrayOutputStream.writeTo(new FileOutputStream(pictureUrl));
                picUrlList.add(pictureUrl);
                byteArrayOutputStream.close();
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return picUrlList;
    }

    /**
     * 处理PDF文档
     *
     * @param pdfPath
     * @return
     * @throws Exception
     */
    public static List<byte[]> handlePdf(String pdfPath,Integer startPage,Integer endPage) throws Exception {
        File pdfFile = new File(pdfPath);
        //加载PDF文档
        PDDocument pdDocument = PDDocument.load(pdfFile);
        //创建PDF渲染器
        PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
        int pageNum = endPage!=null ? endPage : pdDocument.getNumberOfPages();
        List<byte[]> list = new ArrayList<>();
        for (int i = startPage-1; i < pageNum; i++) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            //将PDF的每一页渲染成一张图片
            BufferedImage image = pdfRenderer.renderImage(i);
            ImageIO.write(image, "jpg", outputStream);
            list.add(outputStream.toByteArray());
            outputStream.close();
        }
        pdDocument.close();
        return list;
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值