PDF 解析成图片

package test;

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

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
 * @author zas
 * Convert PDF to image tools
 */
public class PDFConvertToImage {
	
	static String filePath = "D://java_test_/2.pdf";
	static String outputFilePath = "D://java_test_";

	/**
	 * The input PDF files into a picture
	 * @Param inputFile PDF input file
	 * @Param outputFolder image output directory
	 * @throws IOException
	 */
	public static void change(File inputFile, File outputFolder) throws IOException {
		//TODO check the input file exists and is PDF
		//TODO for the treatment of PDF encrypted
		PDDocument doc = null;
		try {
			doc = PDDocument.load(inputFile);
			List<PDPage> allPages = doc.getDocumentCatalog().getAllPages();
			for (int i = 0; i <allPages.size(); i++) {
				PDPage page = allPages.get(i);
				page.convertToImage();
				BufferedImage image = page.convertToImage();
				ImageIO.write(image, "jpg", new File(outputFolder.getAbsolutePath() + File.separator + (i + 1) + ".jpg"));
			}
		} finally {
			if (doc != null) {
				doc.close();
			}
		}
	}
	
	public static void main(String[] args) {
		File inputFile = new File(PDFConvertToImage.filePath);
		File outputFolder = new File(PDFConvertToImage.outputFilePath);
		if(!outputFolder.exists()){
			outputFolder.mkdirs();
		}
		try {
			PDFConvertToImage.change(inputFile, outputFolder);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

package test;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

import javax.imageio.ImageIO;

public class test1 {

	private void convert() throws Exception {

		// load a pdf from a file
		File file = new File("D://java_test_/2.pdf");
		RandomAccessFile raf = new RandomAccessFile(file, "r");
		ReadableByteChannel ch = Channels.newChannel(new FileInputStream(file));

		FileChannel channel = raf.getChannel();
		ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
				channel.size());
		PDFFile pdffile = new PDFFile(buf);

		// get number of pages
		int jumlahhalaman = pdffile.getNumPages();
		int n = 6;
		// iterate through the number of pages
		for (int i = 1; i <= jumlahhalaman; i++) {
			PDFPage page = pdffile.getPage(i);
			// create new image
			Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
					.getWidth(), (int) page.getBBox().getHeight());
			Image img = page.getImage(rect.width*n, rect.height*n, // width & height
					rect, // clip rect
					null, // null for the ImageObserver
					true, // fill background with white
					true // block until drawing is done
					);
			BufferedImage bufferedImage = new BufferedImage(rect.width*n,rect.height*n, BufferedImage.TYPE_INT_RGB);
			Graphics g = bufferedImage.createGraphics();
			g.drawImage(img, 2, 2, null);
			g.dispose();

			File asd = new File("D://java_test_/" + "page_" + i + ".jpg");
			if (asd.exists()) {
				asd.delete();
			}
			ImageIO.write(bufferedImage, "jpg", asd);
		}
	}
	public static void main(String[] args) {
		test1 converter = new test1();
		try {
			converter.convert();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

 

 

 

转载于:https://my.oschina.net/wxpi/blog/598800

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值