Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
從 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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springcloud Bus消息總線原理是實(shí)現(xiàn)詳解
Spring Cloud Bus 使用輕量級(jí)的消息代理來連接微服務(wù)架構(gòu)中的各個(gè)服務(wù),可以將其用于廣播狀態(tài)更改(例如配置中心配置更改)或其他管理指令,本文將對(duì)其用法進(jìn)行詳細(xì)介紹2022-09-09
Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
這篇文章主要為大家介紹了如何利用Java語言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05
jvm內(nèi)存溢出解決方法(jvm內(nèi)存溢出怎么解決)
jvm內(nèi)存溢出解決方法,詳細(xì)內(nèi)容看下面解釋2013-12-12
數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的深入分析
本篇文章是對(duì)數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
解析Mybatis Porxy動(dòng)態(tài)代理和sql解析替換問題
這篇文章主要介紹了Mybatis Porxy動(dòng)態(tài)代理和sql解析替換,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
http調(diào)用controller方法時(shí)openfeign執(zhí)行流程
這篇文章主要為大家介紹了http調(diào)用controller方法時(shí)openfeign執(zhí)行流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

