OpenOffice实现word转pdf

OpenOffice实现word转pdf

1.pom.xml

<!-- openoffice word转pdf -->
        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.1</version>
        </dependency>

2.下载jodconverter2.2.2.jar
3.jodconverter2.2.2推送至maven仓库

mvn install:install-file -Dfile=本地jar包路径 -DgroupId=com.artofsolving -DartifactId=jodconverter -Dversion=2.2.2 -Dpackaging=jar

4.OpenOffice安装
5.OpenOffice服务启动cmd命令

cd C:\Program Files (x86)\OpenOffice 4\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

6.文件转换工具类

package com.zimax.cqyf.admin.tests;

import java.io.File;
import java.io.IOException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class DOC2PDFUtil {



	//使用openoffice 将word格式的文件转换为pdf格式
	public static void word2Pdf(String srcPath, String desPath) throws IOException {
		// 源文件目录
		File inputFile = new File(srcPath);
		if (!inputFile.exists()) {
			System.out.println("源文件不存在!");
			return;
		}
		// 输出文件目录
		File outputFile = new File(desPath);
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().exists();
		}
		// 调用openoffice服务线程
//		String command = "C:\\Program Files (x86)\\OpenOffice 4\\program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
//		Process p = Runtime.getRuntime().exec(command);

		// 连接openoffice服务
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(
				"127.0.0.1", 8100);
		connection.connect();

		// 转换word到pdf
		DocumentConverter converter = new OpenOfficeDocumentConverter(
				connection);
		converter.convert(inputFile, outputFile);

		// 关闭连接
		connection.disconnect();

		// 关闭进程
		//p.destroy();
		System.out.println("转换完成!");
	}

	/**
	 * 测试main方法
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		word2Pdf("D:\\123.doc","D:\\123.pdf");
	}
}

7.转换结果

13:06:13.283 [main] DEBUG com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection - connecting
13:06:13.417 [main] INFO com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection - connected
13:06:26.158 [main] DEBUG com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection - disconnecting
13:06:26.160 [main] INFO com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection - disconnected
转换完成!

OpenOffice服务开机自启https://blog.csdn.net/sll714827/article/details/121679137
资源下载:https://download.csdn.net/download/sll714827/34874030

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值