欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼

 更新時(shí)間:2022年05月31日 15:10:38   作者:洛陽泰山  
這篇文章主要為大家介紹了如何利用Java語言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

從 Maven 下載 Aspose.PDF

通過將以下配置添加到 pom.xml, 您可以直接從基于Maven的項(xiàng)目 輕松地使用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>

核心代碼實(shí)現(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 共耗時(shí):" + ((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 共耗時(shí):" + ((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 共耗時(shí):" + ((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 共耗時(shí):" + ((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 共耗時(shí):" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 轉(zhuǎn) PNG 失敗...");
            e.printStackTrace();
        }
    }
 
 
}

運(yùn)行方法,idea里右鍵運(yùn)行,如果要做成web系統(tǒng)可以將代碼封裝程web服務(wù),調(diào)用方法就行。

轉(zhuǎn)換文件結(jié)果

以一個(gè)十四的pdf文件轉(zhuǎn)化為例,大部分轉(zhuǎn)換時(shí)間在10-12s,只有轉(zhuǎn)ppt花費(fèi)的時(shí)間久一點(diǎn)需要20s.可能pdf里面不是表格類的內(nèi)容,所以轉(zhuǎn)換excel文件后,樣式差別會(huì)有點(diǎn)大,其他文件轉(zhuǎn)換后樣式和之前是保持一樣的。

以上就是Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java PDF轉(zhuǎn)HTML Word Excel PPT PNG的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java為什么需要虛擬機(jī)jvm原理詳解

    java為什么需要虛擬機(jī)jvm原理詳解

    這篇文章主要為大家介紹了java為什么需要虛擬機(jī)jvm的原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-11-11
  • eclipse下搭建hibernate5.0環(huán)境的步驟(圖文)

    eclipse下搭建hibernate5.0環(huán)境的步驟(圖文)

    這篇文章主要介紹了eclipse下搭建hibernate5.0環(huán)境的步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • Java.SE數(shù)組的一些常見練習(xí)題

    Java.SE數(shù)組的一些常見練習(xí)題

    數(shù)組可以看成是相同類型元素的一個(gè)集合,在內(nèi)存中是一段連續(xù)的空間,這篇文章主要給大家介紹了關(guān)于Java.SE數(shù)組的一些常見練習(xí)題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • Java設(shè)計(jì)模式之單例模式示例詳解

    Java設(shè)計(jì)模式之單例模式示例詳解

    單例模式是最簡單也是最基礎(chǔ)的設(shè)計(jì)模式之一,單例模式確保某個(gè)類只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例。本文將通過一些示例代碼為大家詳細(xì)介紹一下單例模式,感興趣的可以了解一下
    2021-12-12
  • jar包和war包區(qū)別解析

    jar包和war包區(qū)別解析

    jar是java普通項(xiàng)目打包,通常是開發(fā)時(shí)要引用通用類,打成jar包便于存放管理,war是java web項(xiàng)目打包,web網(wǎng)站完成后,打成war包部署到服務(wù)器,目的是為了節(jié)省資源,提供效率,這篇文章主要介紹了jar包和war包區(qū)別及理解,需要的朋友可以參考下
    2023-07-07
  • maven 配置多個(gè)倉庫的方法

    maven 配置多個(gè)倉庫的方法

    這篇文章主要介紹了maven 配置多個(gè)倉庫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式

    Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式

    這篇文章主要介紹了Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式,JAVA反射機(jī)制是在運(yùn)行狀態(tài)中,對于任意一個(gè)實(shí)體類,都能夠知道這個(gè)類的所有屬性和方法,需要的朋友可以參考下
    2023-10-10
  • java long轉(zhuǎn)String +Codeforces110A案例

    java long轉(zhuǎn)String +Codeforces110A案例

    這篇文章主要介紹了java long轉(zhuǎn)String +Codeforces110A案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • mybatis-plus中wrapper的用法實(shí)例詳解

    mybatis-plus中wrapper的用法實(shí)例詳解

    本文給大家介紹了mybatis-plus中wrapper的用法,包括條件構(gòu)造器關(guān)系、項(xiàng)目實(shí)例及具體使用操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • java網(wǎng)上圖書商城(9)支付模塊

    java網(wǎng)上圖書商城(9)支付模塊

    這篇文章主要為大家詳細(xì)介紹了java網(wǎng)上圖書商城,支付模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評論