aspose-words转PDF

1.引入pom.xml(可能需要下载引入jar)
在这里插入图片描述

    <dependency>
            <groupId>aspose-words</groupId>
            <artifactId>aspose-words</artifactId>
            <scope>system</scope>
            <version>19.5</version>
            <systemPath>${project.basedir}/lib/aspose-words-19.5.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>aspose-cells</groupId>
            <artifactId>aspose-cells</artifactId>
            <scope>system</scope>
            <version>21.1</version>
            <systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
        </dependency>

2.添加授权文件,在resources下新建文件license.xml
在这里插入图片描述

<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>

4.代码工具类

package com.xmcbs.portalserver.utils;



import com.aspose.cells.*;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
 * @author :wangjp
 * @date :2022/6/6 17:56
 */

public class ExcelToPdf {
    /**
     * 获取license 去除水印
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is =   Thread.currentThread().getContextClassLoader().getResourceAsStream("excel-license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * excel 转为pdf 输出。
     *
     * @param sourceFilePath  excel文件
     * @param desFilePathd  pad 输出文件目录
     */
    public static void excel2pdf(String sourceFilePath, String desFilePathd){
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            Workbook wb = new Workbook(sourceFilePath);// 原始excel路径

            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            int[] autoDrawSheets={3};
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
//            autoDraw(wb,autoDrawSheets);
            int[] showSheets={0};
            //隐藏workbook中不需要的sheet页。
//            printSheetPage(wb,showSheets);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();
            //System.out.println("转换PDF完毕!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ByteArrayOutputStream excel2pdf(InputStream pdfPath){
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Workbook wb = new Workbook(pdfPath); //原始excel路径
//            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            int[] autoDrawSheets={3};
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
//            autoDraw(wb,autoDrawSheets);
            int[] showSheets={0};
            //隐藏workbook中不需要的sheet页。
//            printSheetPage(wb,showSheets);
            wb.save(os, pdfSaveOptions);
//            fileOS.flush();
//            fileOS.close();
            //System.out.println("转换PDF完毕!");
            return os;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Aspose.Words是一款强大的文档处理工具,可以帮助开发人员对Word文档进行读取、编辑、换等操作,支持多种格式的文档,包括.doc、.docx、.rtf等等。其中,将Word文档换为PDF格式是常见的需求。 使用Aspose.Words将Word文档换为PDF格式是非常简单的。首先,需要使用Aspose.Words读取Word文档,可以使用Document类进行操作。然后,使用PdfSaveOptions将Word文档保存为PDF格式。PdfSaveOptions类提供了多种选项,例如设置PDF导出的质量、设置页面大小、设置文档保护等等。 Aspose.Words还提供了其他相关的功能,例如将PDF文档换为Word文档、将文档换为HTML格式等等。同时,Aspose.Words还支持对文档中的内容进行操作,例如插入内容、替换内容、合并文档等等,可以大大提高开发人员的效率。 总之,Aspose.Words是一款强大的文档处理工具,可以帮助开发人员轻松实现Word文档和PDF文档之间的换,同时还提供了其他相关的功能,非常实用。 ### 回答2: Aspose.Words是一款功能强大的文档处理软件,可以处理Word文档并将其换为PDF格式。通过使用Aspose.Words,用户可以轻松地将多种文件格式换为PDF,例如Word、HTML、RTF、XML等。Aspose.Words不仅可以换文件格式,还可以执行许多其他操作,例如插入文本、设置格式、处理图像等等。Aspose.Words的优点是可以轻松地处理复杂的文档格式,包括表格、图像、公式和文本框。此外,Aspose.Words还提供了一个友好的用户界面,使得用户可以轻松地管理和操作各种文档。对于需要频繁处理文档的企业和个人用户,Aspose.Words是一个非常有价值的工具。总之,Aspose.Words是一款可靠且强大的文档处理软件,它可以帮助用户将多种文件格式换为PDF,并提供各种其他功能以便更好地处理和管理文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值