java使用renderer將pdf按頁(yè)轉(zhuǎn)換為圖片
項(xiàng)目中遇到了需要把用戶上傳的word,execl,ppt每頁(yè)截圖保存。需要先用到j(luò)acob把資源轉(zhuǎn)換為pdf,在通過(guò)pdf-renderer把每頁(yè)截圖下來(lái)。
首先下載相關(guān)jar包:下載地址
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;
//如果com.sun.image找不到,就是Eclipse默認(rèn)把這些受訪問(wèn)限制的API設(shè)成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)選為Warning就可以編譯通過(guò)
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 pdfToImage {
public static void main(String[] args) {
String instructiopath="E:/臨時(shí)文件1.pdf";
String picturepath = "E:/臨時(shí)文件1/";
changePdfToImg(instructiopath,picturepath);
}
public static int changePdfToImg(String instructiopath,String picturepath) {
int countpage =0;
try {
//instructiopath ="D:/instructio/2015-05-16/Android 4編程入門經(jīng)典.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);
//創(chuàng)建圖片文件夾
File dirfile = new File(picturepath);
if(!dirfile.exists()){
dirfile.mkdirs();
}
//獲得圖片頁(yè)數(shù)
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放大參數(shù)】 */
Image img = page.getImage(rect.width * n, rect.height * n,
rect, /** 放大pdf到n倍,創(chuàng)建圖片。 */
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("創(chuàng)建圖片:"+
* "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else {
* System.out.println("創(chuàng)建圖片失敗!"); } }
*/
FileOutputStream out = new FileOutputStream(picturepath+"/" + i
+ ".png");
/** 輸出到文件流 */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
param2.setQuality(1f, true);
/** 1f~0.01f是提高生成的圖片質(zhì)量 */
encoder.setJPEGEncodeParam(param2);
encoder.encode(tag);
/** JPEG編碼 */
out.close();
}
channel.close();
raf.close();
/*unmap(buf);*/ //pdf轉(zhuǎn)化成圖片后,釋放MappedByteBuffer資源。調(diào)用unmap(buf);無(wú)效。
/** 如果要在轉(zhuǎn)圖片之后刪除pdf,就必須要這個(gè)關(guān)閉流和清空緩沖的方法 */
} 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;
}
});
}
}
成功釋放MappedByteBuffer資源
Method m = FileChannelImpl.class.getDeclaredMethod("unmap",
MappedByteBuffer.class);
m.setAccessible(true);
m.invoke(FileChannelImpl.class, buf);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud之Feign代理,聲明式服務(wù)調(diào)用方式
這篇文章主要介紹了SpringCloud之Feign代理,聲明式服務(wù)調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot自定義MessageConvert詳細(xì)講解
正在學(xué)習(xí)SpringBoot,在自定義MessageConverter時(shí)發(fā)現(xiàn):為同一個(gè)返回值類型配置多個(gè)MessageConverter時(shí),可能會(huì)發(fā)生響應(yīng)數(shù)據(jù)格式錯(cuò)誤,或406異常(客戶端無(wú)法接收相應(yīng)數(shù)據(jù))。在此記錄一下解決問(wèn)題以及追蹤源碼的過(guò)程2023-01-01
如何使用@ConditionalOnExpression決定是否生效注釋
這篇文章主要介紹了如何使用@ConditionalOnExpression決定是否生效注釋的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java多線程并發(fā)之線程池任務(wù)請(qǐng)求攔截測(cè)試實(shí)例
這篇文章主要介紹了Java多線程并發(fā)之線程池任務(wù)請(qǐng)求攔截測(cè)試實(shí)例,隊(duì)列中永遠(yuǎn)沒(méi)有線程被加入,即使線程池已滿,也不會(huì)導(dǎo)致被加入排隊(duì)隊(duì)列,實(shí)現(xiàn)了只有線程池存在空閑線程的時(shí)候才會(huì)接受新任務(wù)的需求,需要的朋友可以參考下2023-12-12
ssm框架Springmvc文件上傳實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了ssm框架Springmvc文件上傳實(shí)現(xiàn)代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Idea進(jìn)行pull的時(shí)候Your local changes would be
這篇文章主要介紹了Idea進(jìn)行pull的時(shí)候Your local changes would be overwritten by merge.具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot熱部署啟動(dòng)關(guān)閉流程詳解
Spring?Boot啟動(dòng)熱部署是一種技術(shù),它能讓開發(fā)者在不重啟應(yīng)用程序的情況下實(shí)時(shí)更新代碼。這樣可以提高開發(fā)效率,避免頻繁重啟應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-04-04
SpringBoot中操作Bean的生命周期的方法總結(jié)
在SpringBoot應(yīng)用中,管理和操作Bean的生命周期是一項(xiàng)關(guān)鍵的任務(wù),這不僅涉及到如何創(chuàng)建和銷毀Bean,還包括如何在應(yīng)用的生命周期中對(duì)Bean進(jìn)行精細(xì)控制,本文給大家總結(jié)了SpringBoot中操作Bean的生命周期的方法,需要的朋友可以參考下2023-12-12

