Java实现PDF转HTML/Word/Excel/PPT/PNG的示例代码

发布时间:2022-6-01 10:04

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的示例代码的详细内容。

GoLang与Java各自生成grpc代码流程介绍 生活杂谈

GoLang与Java各自生成grpc代码流程介绍

1.背景: 由于公司的日志系统使用的是plumelog,最近生产环境老是报 jedis连接池不够,导致丢失日志,而且服务老是重启,怀疑跟日志系统有关,于是自己改造plumelog,使用go grpc...
Java Process中waitFor()的问题详解 生活杂谈

Java Process中waitFor()的问题详解

在编写Java程序时,有时候我们需要调用其他的诸如exe,shell这样的程序或脚本。在Java中提供了两种方法来启动其他程序: (1) 使用Runtime的exec()方法 (2) 使用Proces...
Java数据结构之有向图的拓扑排序详解 生活杂谈

Java数据结构之有向图的拓扑排序详解

在现实生活中,我们经常会同一时间接到很多任务去完成,但是这些任务的完成是有先后次序的。以我们学习java 学科为例,我们需要学习很多知识,但是这些知识在学习的过程中是需要按照先后次序来完成的。从...