word转pdf的java实现(documents4j)

一、多余的话

java实现word转pdf可用的jar包不多,很多都是收费的。最近发现com.documents4j挺好用的,它支持在本机转换,也支持远程服务转换。但它依赖于微软的office。电脑需要安装office才能转换。鉴于没在linux中使用office,本文转换在windows中进行。

用途:主要是对word文件转换成pdf后,提供在线预览服务。也可以用于合同生成等。

支持文件格式:docx,doc,txt

二、前提条件

windows服务器或电脑需安装office软件。

三、代码实现

添加依赖:

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.1.6</version>
        </dependency>

转换代码类:WordToPdfUtil.java

package com.lan.fts.util;

import com.documents4j.api.*;
import com.documents4j.job.LocalConverter;

import java.io.*;
import java.util.concurrent.Future;

public class WordToPdfUtil {
	
	private IConverter getConverter(){
		return LocalConverter.builder().build();
	}
	private void releaseConverter(IConverter converter){
		converter.shutDown();
	}
	
    public boolean wordToPdf(String fromFilePath, String pdfFilePath){
        boolean result = false;
 
        File inputFile = new File(fromFilePath);
        File outputFile = new File(pdfFilePath);
        InputStream inputStream=null;
        OutputStream outputStream = null;
        IConverter converter = getConverter();
        try {
            inputStream = new FileInputStream(inputFile);
            outputStream = new FileOutputStream(outputFile);
            String wordFilePath_low=fromFilePath.toLowerCase();

            if (wordFilePath_low.endsWith(".docx")) {
				Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.DOCX).to(outputStream, true).as(DocumentType.PDF).schedule();
				result = waitsShedule(schedule, 180000);
			}else if(wordFilePath_low.endsWith(".doc")){
				Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.DOC).to(outputStream, true).as(DocumentType.PDF).schedule();
				result = waitsShedule(schedule, 180000);
			}else if(wordFilePath_low.endsWith(".txt")){
				Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.TEXT).to(outputStream, true).as(DocumentType.PDF).schedule();
				result = waitsShedule(schedule, 180000);
            }
        } catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if(outputStream!=null)outputStream.close();
			} catch (IOException e) {};
			try {
				if(inputStream!=null)inputStream.close();
			} catch (IOException e) {};
			releaseConverter(converter);
		}
        return result;
    }

    private boolean waitsShedule(Future<Boolean> schedule, int timeout){
		int time=0;
		while (!schedule.isDone()){
			MyThread.sleep(500);
			time+=500;
			if(time>timeout){
				schedule.cancel(true);
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
	//	new WordToPdfUtil().wordToPdf("D:\\data\\out\\ffec88b6ee26397bf99834acb059f7b0.docx", "D:\\data\\out\\ffec88b6ee26397bf99834acb059f7b0.docx.pdf");
	}


}

说明:waitsShedule,是等待转换完成。如果超时,将取消转换任务

四、运行验证

	public static void main(String[] args) {
		new WordToPdfUtil().wordToPdf("D:\\data\\out\\lanhezhong文件转换.docx", "D:\\data\\out\\lanhezhong文件转换.docx.pdf");
	}

运行结果:

***********************************************************************************************
author:蓝何忠
email:lanhezhong@163.com
***********************************************************************************************

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值