溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用java怎么將pdf按頁轉換為圖片

發布時間:2021-02-22 15:57:27 來源:億速云 閱讀:267 作者:Leah 欄目:編程語言

本篇文章為大家展示了使用java怎么將pdf按頁轉換為圖片,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

Java可以用來干什么

Java主要應用于:1. web開發;2. Android開發;3. 客戶端開發;4. 網頁開發;5. 企業級應用開發;6. Java大數據開發;7.游戲開發等。

package core.util;
 
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
 
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
 
public class PDFchangToImage {
 public static int changePdfToImg(String instructiopath,String picturepath) {
 int countpage =0;
 try {
 //instructiopath ="D:/instructio/2015-05-16/Android 4編程入門經典.pdf"
 //picturepath = "D:/instructio/picture/2015-05-16/";
 
 File file = new File(instructiopath);
 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);
 //創建圖片文件夾
 File dirfile = new File(picturepath);
  if(!dirfile.exists()){
  dirfile.mkdirs();
 }
 //獲得圖片頁數
 countpage = pdffile.getNumPages();
 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("創建圖片失??!"); } }
  */
 FileOutputStream out = new FileOutputStream(picturepath+"/" + i
  + ".png");
 /** 輸出到文件流 */
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
 param2.setQuality(1f, true);
 /** 1f~0.01f是提高生成的圖片質量 */
 encoder.setJPEGEncodeParam(param2);
 encoder.encode(tag);
 /** JPEG編碼 */
 out.close();
 }
 channel.close();
 raf.close();
 unmap(buf);
 /** 如果要在轉圖片之后刪除pdf,就必須要這個關閉流和清空緩沖的方法 */
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return countpage;
 
 }
 
 @SuppressWarnings("unchecked")
 public 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;
 }
 });
 }
}

上述內容就是使用java怎么將pdf按頁轉換為圖片,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女