1. 程式人生 > >將PDF轉換為圖片

將PDF轉換為圖片

在做將列印word的操作時,我個人認為將word轉換為PDF格式(在博主另一篇部落格中有說道),將PDF轉換為圖片,將圖片傳給前臺去做列印,這樣很方便,前臺不用去管後臺的資料,只需要找到圖片,做簡單的列印操作。
本篇部落格是博主在網上收集的前輩寫的方法,很好用,特分享出來,如有侵權請聯絡博主刪除,謝謝。

依賴jar包

/**
* 將PDF轉化為圖片,可調節清晰度
* @param PDFPath
* @param id
* @param imgSavePath
*/
public static List changePdfToImg(String PDFPath,Long id,String saveUrl) {
List list = new ArrayList();
try {
File file = new File(PDFPath);
RandomAccessFile raf = new RandomAccessFile(file, “r”);
FileChannel channel = raf.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
for (int i = 1; i <= pdffile.getNumPages(); i++) {
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox().getWidth()), ((int) page.getBBox().getHeight()));
int n = 2;/*圖片清晰度(n>0且n<7)【pdf放大引數】

/
Image img = page.getImage(rect.width*n, rect.height*n, rect, /*放大pdf到n倍,建立圖片。/
null, /*null for the ImageObserver /
true, /* fill background with white /
true /* block until drawing is done /
);
BufferedImage tag = new BufferedImage(rect.width*n, rect.height*n, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width*n, rect.height*n, null);
/** File imgfile = new File(“D:\work\mybook\FilesNew\img\” + i + “.jpg”);
if(imgfile.exists()){
if(imgfile.createNewFile())
{ System.out.println(“建立圖片:”+”D:\work\mybook\FilesNew\img\” + i + “.jpg”);
} else { System.out.println(“建立圖片失敗!”);
} } */

              int j = 1;
              String imgPath = saveUrl+"使用者id=" + id + "的第"+i+"張申請表.jpg";
              list.add(imgPath);

// FileOutputStream out = new FileOutputStream(imgPath); /* 輸出到檔案流/
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
// param2.setQuality(1f, true);/*1f~0.01f是提高生成的圖片質量

/
// encoder.setJPEGEncodeParam(param2);
// encoder.encode(tag);/* JPEG編碼 /

             // BufferedImage image = ImageIO.read(new File(imgPath));



              String formatName = imgPath.substring(imgPath.lastIndexOf(".") + 1);

// FileOutputStream out = new FileOutputStream(dstName);
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// encoder.encode(dstImage);
ImageIO.write(tag, /“GIF”/ formatName /* format desired / , new File(imgPath) / target */ );

                 //out.close();          
              }          
              channel.close();          
              raf.close();          
              unmap(buf);/**如果要在轉圖片之後刪除pdf,就必須要這個關閉流和清空緩衝的方法*/          
              } catch (FileNotFoundException e) {
                  e.printStackTrace();          
              }catch (IOException e) {
                  e.printStackTrace();          
              }      
              return list;

              }     
              @SuppressWarnings("unchecked")   
              private static void unmap(final Object buffer) {         
              AccessController.doPrivileged(new PrivilegedAction() {          
              public Object run() {          
              try {          
              Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);          
              getCleanerMethod.setAccessible(true);         
              sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, new Object[0]);          
              cleaner.clean();          
              } catch (Exception e) {
              e.printStackTrace();          }         
              return null;          }          
              });     
              } 

“`