[PDF][JAVA]PDF转图片(二)


*******************************************************************************

**原文转自 https://www.cnblogs.com/songdongdong6/p/8627098.html

*******************************************************************************

package com.sdd.Test.PDFTest;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.util.PDFImageWriter;


/**
 * This example converts a PDF to a multi-page TIFF or a series of bitmap
 * images, using the PDF Viewer extension.
 * 
 * Usage is:PDFBox
 * 
 */
public class PDFToImage {   
    public static void main(String[] args) {
        convertPdf2Jpg("a.pdf","D:/image/");
}

/**
 * 
 * @param pdfFile
 * @param jpdDir
 */
    @SuppressWarnings("unchecked")
    private static void convertPdf2Jpg(String pdfFile,String jpdDir) {
        try {
            InputStream is = new FileInputStream(pdfFile);
            PDDocument pdf = PDDocument.load( is, true );
            List<PDPage> pages = pdf.getDocumentCatalog().getAllPages();
            int i=0;
            for ( PDPage page : pages )
            {    ++i;
                BufferedImage image = page.convertToImage();
                ImageIO.write(image, "jpg", new FileOutputStream(new File(jpdDir+i+".jpg")));
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unused")
    private static void pdf2jpg() {
        String destinationImageFormat = "jpg";
        boolean success = false;
        PDDocument pdf;
        try {
            InputStream is = new FileInputStream("D:481f389b-ec59-4b1a-94cb-e396cd2a990b.pdf");
            pdf = PDDocument.load( is, true );

        int resolution = 256;
        String password = "";
        String outputPrefix = "D:481f389b-ec59-4b1a-94cb-e396cd2a990b\\myImageFile";

        PDFImageWriter imageWriter = new PDFImageWriter();    

        success = imageWriter.writeImage(pdf, 
                            destinationImageFormat, 
                            password, 
                            1, 
                            2, 
                            outputPrefix, 
                            BufferedImage.TYPE_INT_RGB, 
                            resolution);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.sdd.Test</groupId>
  <artifactId>PDFTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>PDFTest</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.jpedal/OpenViewerFX -->
<dependency>
    <groupId>org.jpedal</groupId>
    <artifactId>OpenViewerFX</artifactId>
    <version>7.10.24</version>
</dependency>
 
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>1.6.0</version>
</dependency>
     
  </dependencies>
</project>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值