aspose转换文件类型

aspose转换文件类型

1、导包

代码中需要导入的包只有:aspose-words即可

导包方法:

  • win+r打开控制台
  • 进入jar包所在的目录
  • 输入命令
mvn install:install-file -DgroupId=com.aspose -DartifactId=com.aspose.word -Dversion=0.0.1 -Dpackaging=jar -Dfile=aspose-words-15.8.0-jdk16.jar
  • pom导包
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>com.aspose.word</artifactId>
    <version>0.0.1</version>
</dependency>

2、word转pdf

直接上代码,亲测有效

public static void main(String[] args) throws Exception {

    String srcFile = "D:\\test.docx";
	// word转pdf
    Document pdf = word2pdf(new File(srcFile));
    // 下载文件
    saveFile(pdf);
}

/**
 * word转pdf
 */
public Document word2pdf(File source) {
    // 验证License
    if (!getLicense()) {
        throw new BusinessException("验证License失败,请稍后重试");
    }
    try{
        // Address是将要被转化的word文档
        Document doc = new Document(source.getPath());
        return doc;
    } catch (Exception e) {
        throw new BusinessException("文件转换失败,请稍后重试");
    }
}

/**
 * 获取证书
 * @return
 */
private static boolean getLicense() {
    boolean result = false;
    try {
        // 凭证
        String licenseStr =
            "<License>\n" +
            "  <Data>\n" +
            "    <Products>\n" +
            "      <Product>Aspose.Total for Java</Product>\n" +
            "      <Product>Aspose.Words for Java</Product>\n" +
            "    </Products>\n" +
            "    <EditionType>Enterprise</EditionType>\n" +
            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
            "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
            "  </Data>\n" +
            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
            "</License>";
        InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
        License asposeLic = new License();
        asposeLic.setLicense(license);
        result = true;
    } catch (Exception e) {
        throw new BusinessException("证书获取失败,请稍后重试");
    }
    return result;
}

/**
 * 保存文件,将文件流传给前端
 */
private void saveFile(Document doc){
    try(ServletOutputStream out = response.getOutputStream()) {
        response.reset();
        String fileName = URLEncoder.encode(GcmsDateUtil.currentTimeMillis()+".pdf", StandardCharsets.UTF_8.toString());
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
        //特殊处理,解决 Safari 浏览器下载文件名中文乱码问题。
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
        //解决前端跨域提示问题。
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        doc.save(out, SaveFormat.PDF); // 转换文件类型
    } catch (Exception e) {
        throw new BusinessException("下载失败,请稍后重试");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值