用java写的jodconverter借用openoffice来转换office成pdf的示例代码

下载安装java sdk7安装完成即可.不用设置环境

安装openoffice到d:/program files/open....目录中

创建目录

g:/sys/qidizi/desktop/java/test

下面的相对路径都是针对test目录来说的;

在./src/com/qidizi/office2pdf/office2pdf.java中输入以下代码
package com.qidizi.office2pdf;


import java.io.File;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;


public class office2pdf {
    
    private static  OfficeManager officeManager;
    private static String OPEN_OFFICE_HOME = "D:/Program Files/OpenOffice.org 3/";
    private static int OPEN_OFFICE_PORT[] = {8100};
    
    public static void main(String[] args) throws java.io.IOException {
        System.out.println("\n\n\n");
        
        if (args.length < 2) {
            System.out.println("输入参数错误,必须指定至少2个参数.\n  office2pdf \"office路径\" \"pdf保存路径\" -env; \n -env 显示当前java运行环境;\n 目前你输入的参数个数是" + args.length);
            return;
        }
        
        if ( (args.length > 2) && args[2].equalsIgnoreCase("-env")) {//查看java运行环境参数
            System.getProperties().storeToXML(System.out, "java 运行环境", "UTF-8");//输出内容是utf8,所以,本java源代码也必须是utf8来保存
            System.out.println("\n\n\n");
        }
        
        String inputFile = args[0];
        String pdfFile = args[1];
        
        if (inputFile.isEmpty()) {
            System.out.println("待转换office路径不允许留空");
            return;
        }
        
        if (!pdfFile.toLowerCase().endsWith(".pdf")) {
            System.out.println("输出pdf路径必须以.pdf结尾");
            return;
        }
        
        System.out.println("进行文档转换转换:" + inputFile + " --> " + pdfFile);
        long startTime = System.currentTimeMillis();
        startService();        
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        converter.convert(new File(inputFile),new File(pdfFile));
        System.out.println("转换完成.耗时" +( (System.currentTimeMillis() - startTime) / 60.0)+ "秒");
        stopService();
        System.out.println("运行结束");        
    }
    
    public static void stopService(){
        System.out.println("关闭office转换服务....");
        
        if (officeManager != null) {
            officeManager.stop();
        }
        
        System.out.println("关闭office转换成功!");
    }
    
    public static void startService(){
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        
        try {
            System.out.println("准备启动安装在" + OPEN_OFFICE_HOME + "目录下的openoffice服务....");
            configuration.setOfficeHome(OPEN_OFFICE_HOME);//设置OpenOffice.org安装目录
            configuration.setPortNumbers(OPEN_OFFICE_PORT); //设置转换端口,默认为8100
            configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时
         
            officeManager = configuration.buildOfficeManager();
            officeManager.start();    //启动服务
            System.out.println("office转换服务启动成功!");
        } catch (Exception ce) {
            System.out.println("office转换服务启动失败!详细信息:" + ce);
        }
    }
}


下载jodconverter包解压类包jar到./lib中,共有8个jar

建立class目录./bin

打开cmd.exe运行下面命令

G:\sys\qidizi\desktop\java\test>cd "G:\sys\qidizi\desktop\java\test\src\com\qidizi\office2pdf"

G:\sys\qidizi\desktop\java\test\src\com\qidizi\office2pdf>D:\Progra~1\Java\jdk1.7.0_09\bin\javac.exe  -encoding UTF-8 -classpath "G:\sys\qidizi\desktop\java\test\bin;G:\sys\qidizi\desktop\java\test\lib\commons-io-1.4.jar;G:\sys\qidizi\desktop\java\test\lib\juh-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\jurt-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\ridl-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\unoil-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\commons-cli-1.1.jar;G:\sys\qidizi\desktop\java\test\lib\json-20090211.jar;G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar" -d "G:/sys/qidizi/desktop/java/test/bin/" -Werror -deprecation -verbose  office2pdf.java
[语法分析开始时间 RegularFileObject[office2pdf.java]]
[语法分析已完成, 用时 20 毫秒]
[源文件的搜索路径: G:\sys\qidizi\desktop\java\test\bin,G:\sys\qidizi\desktop\java\test\lib\commons-io-1.4.jar,G:\sys\qidizi\desktop\java\test\lib\juh-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\ridl.jar,G:\sys\qidizi\desktop\java\test\lib\jurt.jar,G:\sys\qidizi\desktop\java\test\lib\..\..\lib,G:\sys\qidizi\desktop\java\test\lib\jurt-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\unoloader.jar,G:\sys\qidizi\desktop\java\test\lib\ridl-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\unoil-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\commons-cli-1.1.jar,G:\sys\qidizi\desktop\java\test\lib\json-20090211.jar,G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar,G:\sys\qidizi\desktop\java\test\lib\sigar-1.6.5.132.jar]
[类文件的搜索路径: D:\Progra~1\Java\jdk1.7.0_09\jre\lib\resources.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\rt.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\sunrsasign.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\jsse.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\jce.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\charsets.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\jfr.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\classes,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\access-bridge.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\dnsns.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\jaccess.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\localedata.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\sunec.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\sunjce_provider.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\sunmscapi.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\sunpkcs11.jar,D:\Progra~1\Java\jdk1.7.0_09\jre\lib\ext\zipfs.jar,G:\sys\qidizi\desktop\java\test\bin,G:\sys\qidizi\desktop\java\test\lib\commons-io-1.4.jar,G:\sys\qidizi\desktop\java\test\lib\juh-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\ridl.jar,G:\sys\qidizi\desktop\java\test\lib\jurt.jar,G:\sys\qidizi\desktop\java\test\lib\..\..\lib,G:\sys\qidizi\desktop\java\test\lib\jurt-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\unoloader.jar,G:\sys\qidizi\desktop\java\test\lib\ridl-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\unoil-3.2.1.jar,G:\sys\qidizi\desktop\java\test\lib\commons-cli-1.1.jar,G:\sys\qidizi\desktop\java\test\lib\json-20090211.jar,G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar,G:\sys\qidizi\desktop\java\test\lib\sigar-1.6.5.132.jar]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/io/File.class)]]
[正在加载ZipFileIndexFileObject[G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar(org/artofsolving/jodconverter/OfficeDocumentConverter.class)]]
[正在加载ZipFileIndexFileObject[G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar(org/artofsolving/jodconverter/office/DefaultOfficeManagerConfiguration.class)]]
[正在加载ZipFileIndexFileObject[G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar(org/artofsolving/jodconverter/office/OfficeManager.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/io/IOException.class)]]
[正在检查com.qidizi.office2pdf.office2pdf]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Exception.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Throwable.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/System.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/io/PrintStream.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/io/FilterOutputStream.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/io/OutputStream.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/util/Properties.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/util/Hashtable.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/util/Dictionary.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/net/URI.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Error.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/RuntimeException.class)]]
[正在加载ZipFileIndexFileObject[G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar(org/artofsolving/jodconverter/office/OfficeException.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/NullPointerException.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/IllegalArgumentException.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/IllegalStateException.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/StringBuilder.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/CharSequence.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Comparable.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AbstractStringBuilder.class)]]
[正在加载ZipFileIndexFileObject[D:\Progra~1\Java\jdk1.7.0_09\lib\ct.sym(META-INF/sym/rt.jar/java/lang/StringBuffer.class)]]
[已写入RegularFileObject[G:\sys\qidizi\desktop\java\test\bin\com\qidizi\office2pdf\office2pdf.class]]
[共 320 毫秒]

G:\sys\qidizi\desktop\java\test\src\com\qidizi\office2pdf>cd "G:\sys\qidizi\desktop\java\test"

G:\sys\qidizi\desktop\java\test>D:\Progra~1\Java\jre7\bin\java.exe  -Dfile.encoding=UTF-8 -classpath "G:\sys\qidizi\desktop\java\test\bin;G:\sys\qidizi\desktop\java\test\lib\commons-io-1.4.jar;G:\sys\qidizi\desktop\java\test\lib\juh-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\jurt-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\ridl-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\unoil-3.2.1.jar;G:\sys\qidizi\desktop\java\test\lib\commons-cli-1.1.jar;G:\sys\qidizi\desktop\java\test\lib\json-20090211.jar;G:\sys\qidizi\desktop\java\test\lib\jodconverter-core-3.0-beta-4.jar" com.qidizi.office2pdf.office2pdf "G:/sys/qidizi/desktop/1.doc" "G:/sys/qidizi/desktop/1.pdf"




杩涜鏂囨。杞崲杞崲:G:/sys/qidizi/desktop/1.doc --> G:/sys/qidizi/desktop/1.pdf
鍑嗗鍚姩瀹夎鍦―:/Program Files/OpenOffice.org 3/鐩綍涓嬬殑openoffice鏈嶅姟....
office杞崲鏈嶅姟鍚姩鎴愬姛!

鍏抽棴office杞崲鏈嶅姟....
鍏抽棴office杞崲鎴愬姛!
杩愯缁撴潫

因为中文输出的是utf-8.所以,会出现乱码.

运行后即可看到桌面有一个pdf文件

用java写的jodconverter借用openoffice来转换office成pdf的示例代码 - qidizi - qidizi 的博客


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,这是一个比较常见的需求,下面是一个使用Spring Boot和OpenOffice实现Word转PDF示例代码: 首先,需要在pom.xml中添加以下依赖: ```xml <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.4.0</version> </dependency> ``` 然后,在application.properties中配置OpenOffice相关属性: ```properties # OpenOffice配置 jodconverter.office-home=/usr/lib/libreoffice jodconverter.port-numbers=8100 ``` 接下来,编Word转PDF代码: ```java import org.jodconverter.DocumentConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.io.InputStream; import java.io.OutputStream; @Service public class WordToPdfService { @Autowired private DocumentConverter documentConverter; /** * 将Word文件转换PDF文件 * * @param wordFile Word文件 * @param pdfFile PDF文件 */ public void wordToPdf(File wordFile, File pdfFile) { documentConverter.convert(wordFile).to(pdfFile).execute(); } /** * 将Word文件流转换PDF文件流 * * @param wordInputStream Word文件流 * @param pdfOutputStream PDF文件流 */ public void wordToPdf(InputStream wordInputStream, OutputStream pdfOutputStream) { documentConverter.convert(wordInputStream).to(pdfOutputStream).execute(); } } ``` 以上代码使用了jodconverter,它是一个Java库,可以将Office文档转换为其他格式。在Spring Boot中,我们可以使用jodconverter-spring-boot-starter来简化配置。在WordToPdfService中,使用了自动注入的DocumentConverter,它是jodconverter的核心接口,用于执行文档转换操作。wordToPdf方法可以将Word文件转换PDF文件,wordToPdf方法可以将Word文件流转换PDF文件流。在使用这些方法之前,需要先配置好OpenOffice相关属性,确保OpenOffice已经正确安装并启动。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值