近期需要將pdf文件轉(zhuǎn)成高清圖片,使用庫是pdfbox、fontbox??梢允褂胷enderImageWithDPI方法指定轉(zhuǎn)換的清晰度,當(dāng)然清晰度越高,轉(zhuǎn)換需要的時間越長,轉(zhuǎn)換出來的圖片越大,越清晰。
說明:由于adobo軟件越來越強大,支持的格式越來越多,這造成了java軟件有些不能轉(zhuǎn)換。所以對于新的格式可能會有轉(zhuǎn)換問題。
1引入依賴
復(fù)制代碼
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.16</version>
</dependency>
<!–https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox–>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.16</version>
</dependency>
復(fù)制代碼
2代碼如下
復(fù)制代碼
publicstaticvoidconvertPdf2Image(StringpdfPath,StringimageDirPath){
log.info(“startconvertpdffile:[{}]toimagepath:[{}]”,pdfPath,imageDirPath);
if(!newFile(pdfPath).exists()){
log.info(“pdfFilename:[{}]notexist”,pdfPath);
return;
}
if(!newFile(imageDirPath).exists()){
log.info(“imageDir:[{}]notexist”,imageDirPath);
return;
}
byte[]pdfContent=FileUtil.getFileContentByte(pdfPath);
Stringfilename=FileUtil.getFilename(pdfPath);
floatdpi=200;
convertPdf2Image(pdfContent,filename,imageDirPath,dpi);
log.info(“convertpdffile:[{}]toimagesuccess”,filename);
}
privatestaticvoidconvertPdf2Image(byte[]pdfContent,StringpdfFilename,StringimageDirPath,floatdpi){
log.info(“convertpdfFilename:[{}]toimageDir:[{}]withdpi:[{}]”,pdfFilename,imageDirPath,dpi);
if(ArrayUtils.isEmpty(pdfContent)){
return;
}
//為了保證顯示清除,至少90
if(dpi<90){
dpi=90;
}
StringbaseSir=imageDirPath;
if(baseSir.endsWith(“/”)||baseSir.endsWith(“\\”)){
baseSir+=pdfFilename+”_”;
}else{
baseSir+=File.separator+pdfFilename+”_”;
}
PDDocumentdocument=null;
BufferedOutputStreamoutputStream=null;
try{
document=PDDocument.load(pdfContent);
intpageCount=document.getNumberOfPages();
PDFRendererpdfRenderer=newPDFRenderer(document);
StringimgPath;
for(inti=0;i<pageCount;i++){
imgPath=baseSir+i+”.png”;
outputStream=newBufferedOutputStream(newFileOutputStream(imgPath));
BufferedImageimage=pdfRenderer.renderImageWithDPI(i,dpi,ImageType.RGB);
ImageIO.write(image,”png”,outputStream);
outputStream.close();
log.info(“converttopng,total[{}],now[{}],ori:[{}],des[{}]”,pageCount,i+1,pdfFilename,imgPath);
}
}catch(IOExceptione){
log.error(“convertpdftoimageerror,pdfFilename:”+pdfFilename,e);
}finally{
IOUtil.closeSilently(outputStream);
IOUtil.closeSilently(document);
}
}
//IOUtil.closeSilently代碼
publicstaticvoidcloseSilently(Closeableio){
if(io!=null){
try{
io.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
復(fù)制代碼
在實際使用中遇到問題
1)ERRORo.a.p.contentstream.PDFStreamEngine911-CannotreadJBIG2image:jbig2-imageioisnotinstalled
2)CannotreadJPEG2000image:JavaAdvancedImaging(JAI)ImageI/OToolsarenotinstalled
以上兩個問題需要使用JAI插件和jbig2插件支持,通過引入jai-imageio-core、jai-imageio-jpeg2000、jbig2-imageio
復(fù)制代碼
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
<!–https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-jpeg2000–>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
<version>1.3.0</version>
</dependency>
<!–https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio–>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jbig2-imageio</artifactId>
<version>3.0.2</version>
</dependency>
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務(wù)熱線)
深圳市坂田十二橡樹莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務(wù)專線:400-001-5281
長沙市天心區(qū)芙蓉中路三段398號新時空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務(wù)專線/ 400-966-8830
旗下運營網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號