栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java编程工具之 word转pdf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java编程工具之 word转pdf

1、依赖,或者直接下载aspose-words-15.8.0-jdk16-Java文档类资源-CSDN下载亲测可用更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/weixin_44364444/85500059,通过(project Structrue——Modeules——右边的Dependecies)把包引进来。通过这种方式引入aspose-words,pom中就无需引入



    4.0.0

    org.example
    wordToPdf
    1.0-SNAPSHOT
    
        
            org.springframework
            spring-context
            3.0.7.RELEASE
        
    

2、证书 resource根目录下 license.xml


    
        
            Aspose.Total for Java
            Aspose.Words for Java
        
        Enterprise
        20991231
        20991231
        8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
    
    
        sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
    

3、工具类

package com.xxh;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

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


public class Word2PdfAsposeUtil {

    
    public static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            Resource resource = new ClassPathResource("license.xml");
            is = resource.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    
    public static boolean doc2pdf(String inPath, String outPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            File file = new File(outPath);
            os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(inPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            // 转化用时
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
}

4.测试类

import com.xxh.Word2PdfAsposeUtil;

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


public class Test {
    public static void main(String[] args) {
        String sourcePath="D:\code\wordToPdf\src\main\resources\Java知识点复习.docx";
        String distPath="D:\code\wordToPdf\src\main\resources\Java知识点复习temp.pdf";
        try {
            File file = new File(distPath);
            if(!file.exists()){
                file.createNewFile();
            }
            Word2PdfAsposeUtil.doc2pdf(sourcePath,distPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

效果如图:

后缀名加上.pdf就可以了。控制台红色部分出现的原因:由于aspose比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms1024m -Xmx1024m(参考值)

  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939209.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号