Java中几种office文档转pdf的方式

最近公司要做office的文档,搜集了几种office文档转pdf的方式,简单的做下总结

我主要尝试了三种方式:openoffice,aspose,jacob

对他们进行了大文件,小文件,在linux,在windows,转换txt,excel,word,ppt的测试。

一、aspose:这种方式在目前来看应该是最好的,无论是转换的速度还是成功的概率,还支持的文件类型。

(1)使用:

这种方式使用很简单,引入jar包就可以直接使用

代码:

源码,jar包在最后提供

package aspose;

import java.io.*;

import javax.servlet.http.HttpServletRequest;

import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.words.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class AsposeUtil {

    //校验license
    private static boolean judgeLicense() {
        boolean result = false;
        try {
            InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    // 转换
    public static void trans(String filePath, String pdfPath, String type) {
        if (!judgeLicense()) {
            System.out.println("license错误");
        }
        try {
            System.out.println("as开始:" + filePath);
            long old = System.currentTimeMillis();
            File file = new File(pdfPath);
            toPdf(file, filePath, type);
            long now = System.currentTimeMillis();
            System.out.println("完成:" + pdfPath);
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void toPdf(File file, String filePath, String type) {
        if ("word".equals(type) || "txt".equals(type)) {
            wordofpdf(file, filePath);
        } else if ("excel".equals(type)) {
            exceOfPdf(file, filePath);
        } else if ("ppt".equals(type)) {
            pptofpdf(file, filePath);
        }else{
            System.out.println("暂不支持该类型:"+type);
        }
    }

    private static void wordofpdf(File file, String filePath) {
        FileOutputStream os = null;
        Document doc;
        try {
            os = new FileOutputStream(file);
            doc = new Document(filePath);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void exceOfPdf(File file, String filePath) {
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            Workbook wb = new Workbook(filePath);
            wb.save(os, com.aspose.cells.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void pptofpdf(File file, String filePath) {
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            Presentation pres = new Presentation(filePath);// 输入pdf路径
            pres.save(os, SaveFormat.Pdf);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

测试代码:

这里面每一种类型的文档都至少取了三个,主要是为了测试他们对大文件,小文件的支持,从而能在不同的场景更好的选择使用的方式,在本篇文章的最后会将测试的结果贴上

package openoffice;

import aspose.AsposeUtil;

/**
 * @author sonyan
 * @version 2019年9月25日 下午1:12:21
 * @desc
 */
public class Test {

    private static void testWord(String path_word, String pafpath) throws Exception {
        String word1 = path_word + "01正方数字.docx";
        String word2 = path_word + "02正方数字.docx";
        String word3 = path_word + "03正方数字.doc";
        String word4 = path_word + "04正方数字.doc";
        String word5 = path_word + "05正方数字.docx";
        String word6 = path_word + "06正方数字.doc";

        OpenOfficeUtils.toPdf(word1, pafpath + "Open-word-01测试.pdf");
        OpenOfficeUtils.toPdf(word2, pafpath + "Open-word-02测试.pdf");
        OpenOfficeUtils.toPdf(word3, pafpath + "Open-word-03测试.pdf");
        OpenOfficeUtils.toPdf(word4, pafpath + "Open-word-04测试.pdf");
        OpenOfficeUtils.toPdf(word5, pafpath + "Open-word-05测试.pdf");
        OpenOfficeUtils.toPdf(word6, pafpath + "Open-word-06测试.pdf");

    }

    private static void testWord2(String path_word, String pafpath) throws Exception {
        String word1 = path_word + "01.docx";
        String word2 = path_word + "02.docx";
        String word3 = path_word + "03.doc";
        String word4 = path_word + "04.doc";
        String word5 = path_word + "05.docx";
        String word6 = path_word + "06.doc";

        OpenOfficeUtils.toPdf(word1, pafpath + "Open-word-01.pdf");
        OpenOfficeUtils.toPdf(word2, pafpath + "Open-word-02.pdf");
        OpenOfficeUtils.toPdf(word3, pafpath + "Open-word-03.pdf");
        OpenOfficeUtils.toPdf(word4, pafpath + "Open-word-04.pdf");
        OpenOfficeUtils.toPdf(word5, pafpath + "Open-word-05.pdf");
        OpenOfficeUtils.toPdf(word6, pafpath + "Open-word-06.pdf");

    }

    private static void testTxt(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01jvm.txt";
        String txt2 = path_word + "02jvm.txt";
        String txt3 = path_word + "03jvm.txt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-txt-01测试.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-txt-02测试.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-txt-03测试.pdf");
    }

    private static void testTxt2(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01jvm.txt";
        String txt2 = path_word + "02jvm.txt";
        String txt3 = path_word + "03jvm.txt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-txt-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-txt-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-txt-03.pdf");
    }

    private static void testExcel(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01部门开发任务管理.xlsx";
        String txt2 = path_word + "02部门开发任务管理.xlsx";
        String txt3 = path_word + "03部门开发任务管理.xlsx";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-excel-01测试.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-excel-02测试.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-excel-03测试.pdf");
    }

    private static void testExcel2(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01.xlsx";
        String txt2 = path_word + "02.xlsx";
        String txt3 = path_word + "03.xlsx";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-excel-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-excel-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-excel-03.pdf");
    }

    private static void testPPt(String path_ppt, String pafpath) throws Exception {
        String txt1 = path_ppt + "01jquery培训.pptx";
        String txt2 = path_ppt + "02jquery培训.pptx";
        String txt3 = path_ppt + "03jquery培训.ppt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-ppt-01测试.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-ppt-02测试.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-ppt-03测试.pdf");
    }

    private static void testPPt2(String path_ppt, String pafpath) throws Exception {
        String txt1 = path_ppt + "01jquery.pptx";
        String txt2 = path_ppt + "02jquery.pptx";
        String txt3 = path_ppt + "03jquery培训.ppt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-ppt-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-ppt-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-ppt-03.pdf");
    }

    public static void LinuxTest() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pafpath = "/software/songyan/hah/pdf/";

        System.out.println("************************");
        testTxt(path_txt, pafpath);
        System.out.println("************************");
        testExcel(path_excel, pafpath);
        System.out.println("************************");
        testPPt(path_ppt, pafpath);
        System.out.println("************************");
        testWord(path_word, pafpath);
    }

    public static void LinuxTest2() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pafpath = "/software/songyan/hah/pdf/";

        System.out.println("************************");
        testTxt2(path_txt, pafpath);
        System.out.println("************************");
        testExcel2(path_excel, pafpath);
        System.out.println("************************");
        testPPt2(path_ppt, pafpath);
        System.out.println("************************");
        testWord2(path_word, pafpath);
    }

    public static void winTest() throws Exception {
        String path_word = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/测试文档/转换前文档/01word/";
        String path_txt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/测试文档/转换前文档/02txt/";
        String path_excel = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/测试文档/转换前文档/03excel/";
        String path_ppt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/测试文档/转换前文档/04ppt/";
        String pafpath = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/测试文档/pdf/";

        System.out.println("************************");
        testWord(path_word, pafpath);
        System.out.println("************************");
        testTxt(path_txt, pafpath);
        System.out.println("************************");
        testExcel(path_excel, pafpath);
        System.out.println("************************");
        testPPt(path_ppt, pafpath);
    }

    public static void main(String[] args) throws Exception {
        winTest();
    }
}

(2)初次使用遇到的问题:

  1)我在这种方式的初次尝试中走的最大的一个坑就是:jar包的引入

  

   

  

上面这三个分别是转换word/txt,excel,ppt的关键代码,在网上找的很多代码可能都是对一种方式的转换,所以SaveFormat也就不会加上包名,所以我在开始就以为是同一个类,怎么调试都不对,最后也是不记得看了哪个博友的博客才注意到这,一定要加上包名,这三个不同包下的类来自三个不同的jar包

2)还有一个很大的坑就是在linux系统上部署测试的时候报了下面的错

这个问题就查到了一个结果,官方给出的解释是:

也就是缺少jar包,可是我检查后发现jar包引入的也没有错误,在第二天找同事帮忙的时候发现,同样的war包使用他的工具可以正常执行转换

很不可思议,我们是在同一台服务器上上传的相同的war包,不一样的就是使用的工具不一样,我用的xshell,他用的sshClient。

我有又测试了一下,第一次访问的时候都会有这个提示

要求安装xmanager,然后就试了试,安装之后果然就可以了,原因现在也没搞明白,,知道的留言吧(我们经理说可能是依赖xmanager里面的包,只是一种猜测)

二,openoffice方式:这种方式在转小文件的时候还是很好用的,在调研的过程中也发现不少公司在实际开发中就是使用的这种方式,我们公司在之前也是用的这种,但是在文件太大的时候转换的时间就会特别的慢,几十分钟的那种。而且这种方式需要依赖openoffice

(1)代码

注:这种方式

原文地址:https://www.cnblogs.com/excellencesy/p/11603892.html

时间: 2024-08-30 06:18:00

Java中几种office文档转pdf的方式的相关文章

Java实现web在线预览office文档与pdf文档实例

https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档与pdf文档实例, 1.首先我们需要找到可以把office转换成pdf的方法,查找资料发现有openoffice这一软件可以把office转换成pdf,这一软件先下载下来,然后记住自己安装的在那个位置.然后在cmd环境下进入安装目录的program目 云计算 云服务器ECS 大数据 建站 备案 文档

Java实现office文档与pdf文档的在线预览功能

最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完.查找资料发现我们要实现的过程就是把office转换成pdf,当然pdf就不用转换了.然后在pdf转换为swf文件,在浏览器实现预览swf文件.整个过程就是这样,看起来很简单,实际操作起来会出现各种问题.下面我就把自己写的这一小功能记录下来. 1.首先我们需要找到可以把office转换成pdf的方法

office文档与pdf文件如何转为jpg图片

有些文档需要把内容输出成图片文件进行发送或是查看,也就是把像office文档以及pdf这类的文档转换成jpg图片的格式,我们都知道这些文档是不能直接保存输出成图片文件的,那要怎样操作才可以将这些文档快速的转为图片文件呢? 通过pdf转换成jpg软件可以将文档的内容输出成为图片的形式.打开转换软件后在左侧的第一个转换类别中选择“文件转图片”.一般转换的图片都是jpg格式. 添加文件既可以添加pdf格式的文件,也能添加office文档进行转换,并且对转换的文件没有要求和限制,文档可以是加密的或者扫描

office 文档转pdf

本地先安装 金山wps,并确保可用 工程目录 1.使用前,先执行install.bat 安装jacob 到maven本地仓库 2.复制 jacob-1.18-M2-x64.dlljacob-1.18-M2-x86.dll 到jdk的bin目录 maven的pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.

linux下使用libreoffice将Office文档转PDF

Linux下可以通过libreoffice将常见的office文档转换成pdf文件,具体步骤如下: 1.安装libreoffice wget http://free.nchc.org.tw/tdf/libreoffice/stable/5.4.6/rpm/x86/LibreOffice_5.4.6_Linux_x86_rpm.tar.gz tar zxvf LibreOffice_5.4.6_Linux_x86_rpm.tar.gz sudo yum install LibreOffice_5.

Java中按行写文档的方法

原文引用https://www.dazhuanlan.com/2019/08/25/5d622ab9a21fa/ 这篇文章总结了使用相关类写文档的操作 1. FileOutputStream 12345678910 public static void writeFile1() throws IOException { File fout = new File("out.txt"); FileOutputStream fos = new FileOutputStream(fout);

Java中Dom解析xml文档

xml文档 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id="1"> <name>你好</name> <author>李四</author> <price>80</price> </book> <book id="2"> &l

各种PDF转换问题(二).将OFFICE文档转为PDF文档

原则上说,PDF一旦创建,其本身是不可以修改的(某些操作,如批注等,则不属于此范围),又因为PDF有轻量,格式通用,允许包含的内容丰富等诸多优点,故很多企事业单位都喜欢将PDF作为最后的文档发放格式.OFFICE转PDF,则成了一个很常用的操作. OFFICE转PDF的手段很多,但最主流的,可能有这么三种: 1.使用微软的免费OFFICE插件SaveAsPDFandXPS.这个插件可以直接到微软官网下载,特点是免费,转换风险低.插件安装好之后,会在OFFICE开始菜单中,"另存为"子项

Java 使用 jacob 将 word 文档转换为 pdf 文件

网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全,要么可是可能有问题,使用这个 jacob 的方法我最开始是最不想用的,因为它要导入 dll 文件,但最后我还是选择了使用该方法,原因是感觉转换后的 pdf 文件简直就是完美. jacob 缺点:需要 window 环境,而且速度是最慢的需要安装 msofficeWord 以及 SaveAsPDFan