java使用Aspose.words实现word文档转pdf文件

引入原因:

感觉openOffice问题还是比较多的。

1. 比如经常出现8100端口问题,虽然kill了就行,但是还是多了一份操作。

2. 出现少数docx无法预览的问题。(已经修改过源码兼容docx,但是还有极少数docx无法转换为pdf)

最近正好碰到第二个问题了,之前使用的是:

<dependency>
    <groupId>com.github.livesense</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>1.0.5</version>
</dependency>

 后来尝试也不行,并且都修改过源码:

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.2</version>
</dependency>

所以引入了Aspose.words 版本是 19.5  据说低版本对字体样式等兼容不好,所以用的这个版本。

1. 下载jar

链接:https://pan.baidu.com/s/1ACFCkJiE6cqjrIhVSkK-6w 
提取码:9999

2. 安装到本地:执行mvn命令,要是报错mvn不是外部或内部命令那种,就找到本地maven文件夹

 命令行窗口输入命令:

mvn install:install-file -DgroupId=com.aspose.words -DartifactId=aspose-words -Dversion=19.5 -Dpackaging=jar -Dfile=写入文件所在地址

例如:

mvn install:install-file -DgroupId=com.aspose.words -DartifactId=aspose-words -Dversion=19.5 -Dpackaging=jar -Dfile=D:\software\apache-maven-3.8.6\bin\aspose-words-19.5.jar

 3. 引入jar包

<!-- 一些docx无法使用openOffice进行转换。转为使用Aspose.words -->
		<dependency>
			<groupId>com.aspose.words</groupId>
			<artifactId>aspose-words</artifactId>
			<version>19.5</version>
		</dependency>

4. 在resources目录新建license.xml。文件放在哪里都可以,后边代码有指定的引用路径(后边有getResourceAsStream("license.xml"); 代表的引用路径),所以放在哪里都可以

 license.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<!-- -->
<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

 5.创建一个工具类:这个我是参考直接拿过来用的:JAVA使用aspose实现word文档转pdf文件

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

public class AsposeUtil {

    /**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    public boolean getLicense() {
        boolean result = false;
        try {
            InputStream is =this.getClass().getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        AsposeUtil bean = new AsposeUtil();
        bean.word2Pdf2("D:\\进修审批表.docx","D:\\test.pdf");
    }

    /**
     * word转pdf
     * inpath: 输入word的路径
     * outpath: 输出pdf的路径
     */
    public void word2Pdf2(String inpath,String outpath) throws Exception {
        if (!getLicense()) {
            System.out.println("非法------------");
            return;
        }

        System.out.println("开始使用Aspose.words进行转换");

        long old = System.currentTimeMillis();
        File file = new File(outpath);

        FileOutputStream os = new FileOutputStream(file);

        //解决乱码
        //如果是windows执行,不需要加这个
        String osName = System.getProperty("os.name", "");
        if (osName.startsWith("Mac OS")) {
        } else if (osName.startsWith("Windows")) {
        } else { // assume Unix or Linux
            //如果是linux执行,需要添加这个 ,如果还有乱码,可以把/usr/share/fonts/chinese路径下的所有文件拷贝到有问题的环境。并且再执行:source /etc/profile
            new FontSettings().setFontsFolder("/usr/share/fonts/chinese",true);
        }

        Document doc = new Document(inpath);

        //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        doc.save(os, SaveFormat.PDF);
        long now = System.currentTimeMillis();
        System.out.println("Aspose.words转换结束,共耗时:" + ((now - old) / 1000.0) + "秒");
    }


    /**
     * word转pdf
     * @param path      pdf输出路径
     * @param wordInput word输入流
     * @param wordName  word文档的名称
     */
    public void word2pdf(String path, InputStream wordInput, String wordName) throws FileNotFoundException {
        if (!getLicense()) {
            System.out.println("非法");
            return;
        }

        //新建一个空白pdf文档
        long old = System.currentTimeMillis();
        File file = new File(path + wordName + ".pdf");
        FileOutputStream os = new FileOutputStream(file);

        //Address是将要被转化的word文档
        Document doc = null;
        try {
            doc = new Document(wordInput);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long now = System.currentTimeMillis();
        //转化用时
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
    }
}

6. 可以使用main方法进行测试了。

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值