java pdf生成图片

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

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

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

public class ExtractImages 
{
    public static void main(String[] args) throws IOException { 
        PDDocument doc = PDDocument.load("F:\\1.pdf");
        int pageCount = doc.getPageCount(); 
        System.out.println(pageCount); 
        List pages = doc.getDocumentCatalog().getAllPages(); 
        for(int i=0;i<pages.size();i++){
            PDPage page = (PDPage)pages.get(i); 
            BufferedImage image = page.convertToImage(); 
            Iterator iter = ImageIO.getImageWritersBySuffix("jpg"); 
            ImageWriter writer = (ImageWriter)iter.next(); 
            File outFile = new File("C:/"+i+".jpg"); 
            FileOutputStream out = new FileOutputStream(outFile); 
            ImageOutputStream outImage = ImageIO.createImageOutputStream(out); 
            writer.setOutput(outImage); 
            writer.write(new IIOImage(image,null,null)); 
        }
        doc.close(); 
        System.out.println("over"); 
    }

}

所依赖的jar:

commons-logging.jar

fontbox-0.8.0-incubating.jar

pdfbox-0.8.0-incubating.jar

 

本文转自:http://www.blogjava.net/sxyx2008/archive/2010/07/23/326890.html


 

你可能感兴趣的:(java,image,File,null,iterator,import)