pdf转图片
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
/**
* pdf转图片
*/
public class PdfFileImageUtil {
/**
* pdfbox 1.8的方法
* @param pdfPath
* @param desDirectorty
* @throws IOException
*/
public void pdfToImage(String pdfPath, String desDirectorty) throws IOException {
ArrayList<BufferedImage> bis = toImage(pdfPath);
save(bis, desDirectorty);
}
private ArrayList<BufferedImage> toImage(String path) throws IOException{
ArrayList<BufferedImage> bis=null;
PDDocument doc = PDDocument.load(new File(path));
bis=new ArrayList<BufferedImage>();
//pdfbox 1.8
/*List<?> pages = doc.getDocumentCatalog().getAllPages();
for(int i=0;i<pages.size();i++){
PDPage page=(PDPage)pages.get(i);
bis.add(page.convertToImage());
}*/
//pdfbox2.0
int pages = doc.getNumberOfPages();
int dpi = 200;//分辨率
for(int i = 0; i < pages ; i++) {
BufferedImage image = new PDFRenderer(doc).renderImageWithDPI(i, dpi,ImageType.GRAY);
bis.add(image);
}
return bis;
}
private void save(ArrayList<BufferedImage> bis,String path) throws IOException{
for(int i=0;i<bis.size();i++){
ImageIO.write(bis.get(i), "jpg", new File(path+"\\"+(i+1)+".jpg"));
}
}
}
提取pdf中的图片
import java.awt.imag