色欲av一区久久精品_久久综合色综合色88_无码在线观看不卡_色黄视频网站_亚洲国产精品久久久久秋霞66

Java庫將pdf文件轉(zhuǎn)換為高清圖像方法

時間:2023-05-17

近期需要將pdf文件轉(zhuǎn)成高清圖片,使用庫是pdfbox、fontbox??梢允褂胷enderImageWithDPI方法指定轉(zhuǎn)換的清晰度,當(dāng)然清晰度越高,轉(zhuǎn)換需要的時間越長,轉(zhuǎn)換出來的圖片越大,越清晰。
說明:由于adobo軟件越來越強大,支持的格式越來越多,這造成了java軟件有些不能轉(zhuǎn)換。所以對于新的格式可能會有轉(zhuǎn)換問題。Java
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>

文章標簽:

Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號

與項目經(jīng)理交流
掃描二維碼
與項目經(jīng)理交流
掃描二維碼
與項目經(jīng)理交流
ciya68