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();
		}
	}
}




你可能感兴趣的:(解析,图片,pdf,成)