pdf与图片互相转换代码

pdf与图片互相转换代码

ID:8976757

大小:16.96 KB

页数:6页

时间:2018-04-13

pdf与图片互相转换代码_第1页
pdf与图片互相转换代码_第2页
pdf与图片互相转换代码_第3页
pdf与图片互相转换代码_第4页
pdf与图片互相转换代码_第5页
资源描述:

《pdf与图片互相转换代码》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库

1、所需jar包importjava.awt.Rectangle;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.lang.reflect.Method;importjava.nio.MappedByteBuffer;importjava.nio.channels.Fi

2、leChannel;importjava.security.AccessController;importjava.security.PrivilegedAction;importcom.lowagie.text.Document;importcom.lowagie.text.DocumentException;importcom.lowagie.text.pdf.PdfWriter;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGEncodeParam;importcom.sun.im

3、age.codec.jpeg.JPEGImageEncoder;importcom.sun.pdfview.PDFFile;importcom.sun.pdfview.PDFPage;publicclassTestPdf{publicstaticvoidmain(String[]args)throwsException{PdfToImg();ImgToPdf();}/***将pdf转成img*/publicstaticvoidPdfToImg(){try{Filefile=newFile("c://PDFbox使用教程.pdf");RandomAccessFileraf=newRandom

4、AccessFile(file,"r");FileChannelchannel=raf.getChannel();MappedByteBufferbuf=channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());PDFFilepdffile=newPDFFile(buf);for(inti=1;i<=pdffile.getNumPages();i++){PDFPagepage=pdffile.getPage(i);Rectanglerect=newRectangle(0,0,((int)page.getBBox().getWid

5、th()),((int)page.getBBox().getHeight()));java.awt.Imageimg=page.getImage(rect.width,rect.height,rect,null,//nullfortheImageObservertrue,//fillbackgroundwithwhitetrue//blockuntildrawingisdone);BufferedImagetag=newBufferedImage(rect.width,rect.height,BufferedImage.TYPE_INT_RGB);tag.getGraphics().dra

6、wImage(img,0,0,rect.width,rect.height,null);FileOutputStreamout=newFileOutputStream("c:\"+i+".jpg");//输出到文件流JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);JPEGEncodeParamparam2=encoder.getDefaultJPEGEncodeParam(tag);param2.setQuality(1f,false);//1f是提高生成的图片质量encoder.setJPEGEncodeParam(pa

7、ram2);encoder.encode(tag);//JPEG编码out.close();}channel.close();raf.close();unmap(buf);//如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。