分享一个word转pdf的工具类Aspose[java]

news/2024/7/24 7:16:17 标签: aspose, java, office转pdf

项目中使用到office文件的格式转换pdf阅读,但是没有一款好用的转换工具。由于本人是mac系统,openoffice也没法使用,前期使用itext转换一直中文乱码,也没有解决这个问题,后来发现aspose,虽说是付费的,但是确实是好用,更重要的是中国程序员的无私奉献精神,下面就来展示一下怎么转换的吧,其实关键就是几行代码

代码中file即要转换的文件,path即转换的pdf输出路径

使用aspose首先的肯定是验证下面就是验证

    private static boolean getLicense() {
        boolean result = false;
        try {
            // 凭证
            String licenseStr = "此处放入凭证,可自行百度,aspose验证";
            InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
            License asposeLic = new License();
            asposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            log.error("error:", e);
        }
        return result;
    }

word转pdf

    public static void word2Pdf(MultipartFile multipartFile, String pdfFilePath) {
        FileOutputStream fileOS = null;
        // 验证License
        if (!getLicense()) {
            log.error("验证License失败!");
            return;
        }
        try {
            Document doc = new Document(multipartFile.getInputStream());
            fileOS = new FileOutputStream(new File(pdfFilePath));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("error:", e);
        } finally {
            try {
                if(fileOS != null){
                    fileOS.close();
                }
            } catch (IOException e) {
                log.error("error:", e);
            }
        }
    }

excel转pdf

    public static void excel2pdf(MultipartFile file, String path) {
        if (!getLicense()) {          
       // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            File pdfFile = new File(path)
            Workbook wb = new Workbook(file.getInputStream());
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            wb.save(fileOS, SaveFormat.PDF);
            fileOS.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

ppt转pdf

    public static void ppt2pdf(MultipartFile file, String path){
        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
             Presentation pres = new Presentation(file.getInputStream());
            FileOutputStream fileOS = new FileOutputStream(path);
             pres.save(fileOS, SaveFormat.PDF);
             fileOS.close();
             } catch (Exception e) {
                 e.printStackTrace();
             }
    }

下面是附上jar包的地址
链接:https://pan.baidu.com/s/11js-Vi_HqYwAlid14xDmZg 密码:wxg8

好了,今天的分享到此结束,有问题欢迎留言


http://www.niftyadmin.cn/n/1246497.html

相关文章

MySQL时间字段设置自动更新

数据库表字段gmt_create 和gmt_modified 类型都是datetime,然后想着设置修改日期每次都自动更新,然后下面就是关键代码了 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP只要在datetime字段设置默认值为上面这句代码就可以实现自动更新了

springboot整合后台框架(三)整合elasticsearch

最近在学习es,起码要先有个es环境吧,然后再是整合到代码中使用一下,毕竟只有实践才会有深刻的记忆,这就是所谓的经验啊,下面开始吧,本文分两部分,第一部分配置es环境,第二部分整合到…

docker安装kafka(wurstmeister)

本文记录一下docker中安装kafka集群的过程 命令不能使用报错,请检查空格,尤其第一个转行符号后面的空格 客户端连接9092 如果java(springboot)连接,需要配置advertised.port 使用docker-compose,因为本机…

docker 部署springboot项目,连接mysql容器

入手docker部署springboot项目&#xff0c;记录一下学习过程&#xff0c;过程很简单&#xff0c;主要是部署过程中的收获&#xff0c;下面跟我一起来吧 创建一个springboot项目&#xff0c;pom引入maven-docker-plugin依赖 <plugin><groupId>com.spotify</gro…

spring-boot-starter-data-elasticsearch es带x-pack后台配置

pom配置 <repositories><repository><id>elasticsearch-releases</id><url>https://artifacts.elastic.co/maven</url><releases><enabled>true</enabled></releases><snapshots><enabled>false<…

JsonUtil工具类

package com.tian.mvc01.util;import com.google.gson.*; import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List;/*** 描述:* json转换工具类** author cui* create 2018-11-08 09:48*/ public class …

雪花算法id生成util

package com.tian.mvc01.util;/*** author C-UI* Classname SnowFlakeUtil* Description 生成唯一id* date 2019/4/11 11:33*/ public class SnowFlakeUtil {/*** 起始的时间戳*/private final static long START_STMP 1530795377086L;/*** 每一部分占用的位数*//*** 序列号占…

打包 android apk签名

生成密钥文件 keytool -genkey -v -keystore XXX-release.keystore -alias YYY -keyalg RSA -validity 4000&#xff08;执行命令之后会提示你输入密码&#xff0c;设置一些公司名称之类的&#xff0c;密码要记住&#xff0c;其他随意&#xff09; XXX-release.keystore &…