引入icebox jar包
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
切图:
public static void cutting(String url1, String url2, String contractId) throws IOException{
//将pdf按页生成图片
String filePath = url1;
Document document = new Document();
Rectangle tempReader = null;
PdfReader pdfReader = null;
float scale = 1.3f;
float rotation = 0f;
int height = 0;
int width = 0;
try {
String urls = url2+contractId+"/";
File files = new File(urls);
if(!files.exists()){
files.mkdirs();
}
document.setFile(filePath);
pdfReader = new PdfReader(filePath);
tempReader = pdfReader.getPageSize(1);
height = (int)tempReader.getHeight();
width = (int)tempReader.getWidth();
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX, rotation, scale);
BufferedImage tempImage = resize(image,width,height);
RenderedImage rendImage = tempImage;
ImageIO.write(rendImage, "PNG", new File(urls+contractId+"_"+i+".png"));
tempImage.flush();
image.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
document.dispose();
if(null!=pdfReader){
pdfReader.close();
}
}
}