首頁 > 軟體

Java實現PDF轉HTML/Word/Excel/PPT/PNG的範例程式碼

2022-05-31 18:00:21

從 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>

核心程式碼實現(單類)

 
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");
    }
 
 
    //轉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 轉 Word 共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 轉 Word 失敗...");
            e.printStackTrace();
        }
    }
 
    //轉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 轉 PPT 共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 轉 PPT 失敗...");
            e.printStackTrace();
        }
    }
 
    //轉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 轉 EXCEL 共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 轉 EXCEL 失敗...");
            e.printStackTrace();
        }
    }
 
    //轉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 轉 HTML 共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 轉 HTML 失敗...");
            e.printStackTrace();
        }
    }
 
    //轉圖片
    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 轉 PNG 共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 轉 PNG 失敗...");
            e.printStackTrace();
        }
    }
 
 
}

執行方法,idea裡右鍵執行,如果要做成web系統可以將程式碼封裝程web服務,呼叫方法就行。

轉換檔案結果

以一個十四的pdf檔案轉化為例,大部分轉換時間在10-12s,只有轉ppt花費的時間久一點需要20s.可能pdf裡面不是表格類的內容,所以轉換excel檔案後,樣式差別會有點大,其他檔案轉換後樣式和之前是保持一樣的。

以上就是Java實現PDF轉HTML/Word/Excel/PPT/PNG的範例程式碼的詳細內容,更多關於Java PDF轉HTML Word Excel PPT PNG的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com