你可以把pdf转换成图片啊,用sun的PDFRenderer.jar public static void pdfToImage(File srcFile,String fileLocation,int no){ RandomAccessFile raf; try { logger.debug("srcFile:" + srcFile.getAbsolutePath() + ",no:" + no); raf = new RandomAccessFile(srcFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); if(no < 1){ no = 1; } if(no > pdffile.getNumPages()){ throw new RuntimeException("无数据,已到达末页!"); } // draw the first page to an image PDFPage page = pdffile.getPage(no);
//get the width and height for the doc at the default zoom int width = (int)page.getBBox().getWidth(); int height =(int)page.getBBox().getHeight();
Rectangle rect = new Rectangle(0,0,width,height); int rotation=page.getRotation(); Rectangle rect1=rect; if(rotation==90 || rotation==270) rect1=new Rectangle(0,0,rect.height,rect.width);
//generate the image BufferedImage img = (BufferedImage)page.getImage( rect.width, rect.height, //width & height rect1, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); ImageIO.write(img, "png", new File(fileLocation)); clean(buf); channel.close(); raf.close(); } catch (FileNotFoundException e1) { logger.error("file not found",e1); } catch (IOException e) { logger.error("io error",e); } }
public static void pdfToImage(File srcFile,String fileLocation,int no){
RandomAccessFile raf;
try {
logger.debug("srcFile:" + srcFile.getAbsolutePath() + ",no:" + no);
raf = new RandomAccessFile(srcFile, "r"); FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
if(no < 1){
no = 1;
}
if(no > pdffile.getNumPages()){
throw new RuntimeException("无数据,已到达末页!");
}
// draw the first page to an image
PDFPage page = pdffile.getPage(no);
//get the width and height for the doc at the default zoom
int width = (int)page.getBBox().getWidth();
int height =(int)page.getBBox().getHeight();
Rectangle rect = new Rectangle(0,0,width,height);
int rotation=page.getRotation();
Rectangle rect1=rect;
if(rotation==90 || rotation==270)
rect1=new Rectangle(0,0,rect.height,rect.width);
//generate the image
BufferedImage img = (BufferedImage)page.getImage(
rect.width, rect.height, //width & height
rect1, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
); ImageIO.write(img, "png", new File(fileLocation));
clean(buf);
channel.close();
raf.close();
}
catch (FileNotFoundException e1) {
logger.error("file not found",e1);
} catch (IOException e) {
logger.error("io error",e);
}
}