您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java如何實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java如何實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG文章都會有所收獲,下面我們一起來看看吧。
從 Maven 下載 Aspose.PDF
通過將以下配置添加到 pom.xml, 您可以直接從基于Maven的項目 輕松地使用Aspose.PDF for Java 。
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>22.4</version> </dependency>
核心代碼實現(xiàn)(單類)
import com.aspose.pdf.Document; import com.aspose.pdf.SaveFormat; import com.aspose.pdf.devices.PngDevice; import com.aspose.pdf.devices.Resolution; import java.io.*; public class PDFHelper3 { public static void main(String[] args) throws IOException { pdf2image("C:\\Users\\liuya\\Desktop\\pdf\\示例文件.pdf"); } //轉(zhuǎn)word public static void pdf2word(String pdfPath) { long old = System.currentTimeMillis(); try { String wordPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".docx"; FileOutputStream os = new FileOutputStream(wordPath); Document doc = new Document(pdfPath); doc.save(os, SaveFormat.DocX); os.close(); long now = System.currentTimeMillis(); System.out.println("Pdf 轉(zhuǎn) Word 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Pdf 轉(zhuǎn) Word 失敗..."); e.printStackTrace(); } } //轉(zhuǎn)ppt public static void pdf2ppt(String pdfPath) { long old = System.currentTimeMillis(); try { String wordPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".ppt"; FileOutputStream os = new FileOutputStream(wordPath); Document doc = new Document(pdfPath); doc.save(os, SaveFormat.Pptx); os.close(); long now = System.currentTimeMillis(); System.out.println("Pdf 轉(zhuǎn) PPT 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Pdf 轉(zhuǎn) PPT 失敗..."); e.printStackTrace(); } } //轉(zhuǎn)excel public static void pdf2excel(String pdfPath) { long old = System.currentTimeMillis(); try { String wordPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".xlsx"; FileOutputStream os = new FileOutputStream(wordPath); Document doc = new Document(pdfPath); doc.save(os, SaveFormat.Excel); os.close(); long now = System.currentTimeMillis(); System.out.println("Pdf 轉(zhuǎn) EXCEL 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Pdf 轉(zhuǎn) EXCEL 失敗..."); e.printStackTrace(); } } //轉(zhuǎn)html public static void pdf2Html(String pdfPath) { long old = System.currentTimeMillis(); try { String htmlPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".html"; Document doc = new Document(pdfPath); doc.save(htmlPath,SaveFormat.Html); long now = System.currentTimeMillis(); System.out.println("Pdf 轉(zhuǎn) HTML 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Pdf 轉(zhuǎn) HTML 失敗..."); e.printStackTrace(); } } //轉(zhuǎn)圖片 public static void pdf2image(String pdfPath) { long old = System.currentTimeMillis(); try { Resolution resolution = new Resolution(300); String dataDir=pdfPath.substring(0,pdfPath.lastIndexOf(".")); File imageDir = new File(dataDir+"_images"); imageDir.mkdirs(); Document doc = new Document(pdfPath); PngDevice pngDevice = new PngDevice(resolution); for (int pageCount = 1; pageCount <= doc.getPages().size(); pageCount++) { OutputStream imageStream = new FileOutputStream(imageDir+"/"+pageCount+".png"); pngDevice.process(doc.getPages().get_Item(pageCount), imageStream); imageStream.close(); } long now = System.currentTimeMillis(); System.out.println("Pdf 轉(zhuǎn) PNG 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Pdf 轉(zhuǎn) PNG 失敗..."); e.printStackTrace(); } } }
運行方法,idea里右鍵運行,如果要做成web系統(tǒng)可以將代碼封裝程web服務(wù),調(diào)用方法就行。
轉(zhuǎn)換文件結(jié)果
以一個十四的pdf文件轉(zhuǎn)化為例,大部分轉(zhuǎn)換時間在10-12s,只有轉(zhuǎn)ppt花費的時間久一點需要20s.可能pdf里面不是表格類的內(nèi)容,所以轉(zhuǎn)換excel文件后,樣式差別會有點大,其他文件轉(zhuǎn)換后樣式和之前是保持一樣的。
關(guān)于“Java如何實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Java如何實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。