生活随笔
收集整理的這篇文章主要介紹了
使用pdfbox实现PDF转JPG
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
引入依賴
<dependency
><groupId
>org.apache.pdfbox
</groupId
><artifactId
>pdfbox
</artifactId
><version
>1.7.
1</version
>
</dependency
>
<dependency
><groupId
>org.apache.pdfbox
</groupId
><artifactId
>fontbox
</artifactId
><version
>1.7.
1</version
>
</dependency
>
實(shí)現(xiàn)簡單的PDF轉(zhuǎn)JPG
public static void main
(String
[] args
) {String pdfFilePath
= "D:/aaa.pdf";//pdf文件路徑String destFormat
= "jpg";//最終希望PDF文件轉(zhuǎn)成的文件格式int dpi
= 144;//pdi越大,圖片越清晰,生成的圖片也越大File
file = new File
(pdfFilePath
);String destDirFile
= "D:/";//最終jpg圖片生成保存的目錄PDDocument doc
= null
;int num
= 1;try
{doc
= PDDocument.load
(file
);List
<PDPage
> pages
= doc.getDocumentCatalog
().getAllPages
();//獲取到當(dāng)前PDF內(nèi)的總頁數(shù)
for (int i
= 0; i
< pages.size
(); i++
) {PDPage page
= pages.get
(i
);//生成彩色的圖,想生成黑白的圖可以改變“TYPE_3BYTE_BGR”處的值BufferedImage image
= page.convertToImage
(BufferedImage.TYPE_3BYTE_BGR, dpi
);//獲取PDF名-
>aaa.pdfString pdfName
= pdfFilePath.substring
(pdfFilePath.lastIndexOf
("/") +
1);//拼接生成的圖片路徑-
>D:/destDir/aaa-1.jpgString destPath
= destDirFile + pdfName.substring
(0, pdfName.lastIndexOf
(".")) +
"-" + num++ +
"." + destFormat
;File destDirFiles
= new File
(destPath
);if (!destDirFiles.exists
()) {destDirFiles.mkdirs
();}File jpgFile
= new File
(destPath
);ImageIO.write
(image, destFormat, jpgFile
);//生成圖片
}} finally
{if (doc
!= null
) {doc.close
();}}}
總結(jié)
以上是生活随笔為你收集整理的使用pdfbox实现PDF转JPG的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。