利用pdfbox和itext包将pdf转换为图片

先来itext的例子

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.color.ColorSpace;

public class PdfToMultiImage {

    public static void main(String args[]) throws IOException {

        File sourceFile = new File("C:\\Users\\luez\\Documents\\Tencent Files\\279436877\\FileRecv\\4172\\日本リビング保証:検査申込書.pdf");
        File destinationFile = new File(sourceFile.getPath().replace(".pdf", ""));
        List<String> result = new ArrayList<String>();
        RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
                channel.size());
        PDFFile pdf = new PDFFile(buf);
        int pageNumber = 1;
        for (int i = 1; i <= pdf.getNumPages(); i++) {
              PDFPage page = pdf.getPage(i);
            int width = 1200;
            int height = 1400;
            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
                    .getWidth(), (int) page.getBBox().getHeight());
            BufferedImage bufferedImage = new BufferedImage(width, height,
                    BufferedImage.TYPE_BYTE_GRAY);
            Image image = page.getImage(width, height, rect, null, true, true);
            Graphics2D bufImageGraphics = bufferedImage.createGraphics();
            bufImageGraphics.drawImage(image, 0, 0, null);
            String destpath=destinationFile + "" + i + ".png";

         //把生成图片放进集合
            result.add(destpath);
            File imageFile = new File(destpath);
            ImageIO.write(bufferedImage, "png", imageFile);
            pageNumber++;
        }
        System.err.println(result);
    }
}


下面是pdfbox的转换

public static List<String> convert2jpg(String filePath) throws Exception {
Properties properties=System.getProperties();
Object encoding = properties.get("file.encoding");
properties.put("file.encoding", "SHIFT-JIS");
List<String> result = new ArrayList<String>();
if(!filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase().equals("pdf")){
return result;
}

String destFormat = "jpg";
int resolution = 300;

File file = new File(filePath);
String destPath = null;
PDDocument doc = null;
try {
doc = PDDocument.load(file);
if (doc.isEncrypted()) {
// doc.decrypt("TU2016");
return result;
}
List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
if (pages.size() == 1) {
PDPage page = pages.get(0);
// BufferedImage image = page.convertToImage();
BufferedImage image = page.convertToImage(BufferedImage.TYPE_BYTE_GRAY, resolution);
destPath  = filePath.substring(0,filePath.lastIndexOf("."))+"."+destFormat;
File destFile = new File(destPath);
ImageIO.write(image, destFormat, destFile);
result.add(destPath);
} else {
String format = "_%0"+String.valueOf(pages.size()).length()+"d";
for (int i = 0; i < pages.size(); i++) {
PDPage page = pages.get(i);
BufferedImage image = page.convertToImage(BufferedImage.TYPE_BYTE_GRAY,resolution);
destPath  = filePath.substring(0,filePath.lastIndexOf("."))+String.format(format,i+1)+"."+destFormat;
File destFile = new File(destPath);
ImageIO.write(image, destFormat, destFile);
result.add(destPath);
}
}
} finally {
if (doc != null) {
doc.close();
}
properties.put("file.encoding", encoding);
}
return result;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值