aspose实现office文件转换pdf文件

aspose实现office文件转换pdf文件

jar:

  • aspose.slides-19.3.jar
  • aspose-cells-8.5.2.jar
  • aspose-words-16.8.0.jar
  • license.xml

链接:https://pan.baidu.com/s/11rBQxPYTn9x9WOc1OBZjUQ
提取码:x2b4
复制这段内容后打开百度网盘手机App,操作更方便哦

license.xml放入resources目录下
验证license.xml

	private static InputStream license;

    //校验license
    public boolean judgeLicense() {
        boolean result = false;
        try {
            license = AsposeUtilImpl.class.getClassLoader().getResourceAsStream("license.xml"); //文件路径
            if (license != null) {
                License aposeLic = new License();
                aposeLic.setLicense(license);
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

外部类调用方法

 /**
     *
     * @param filePath  office路径
     * @param pdfPath   pdf输出路径
     * @param type	office文件类型
     * @return
     * @throws OutOfMemoryError
     */
    public boolean trans(String filePath, String pdfPath, String type) throws OutOfMemoryError{
        try {
            if (!judgeLicense()) {
                logger.error("license错误");
            }
            File file = new File(pdfPath);
            return  toPdf(file, filePath, type);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

判断文件类型

    private boolean toPdf(File file, String filePath, String type) throws OutOfMemoryError{
        boolean result = false;
        switch (type) {
            case "doc":
            case "docx":
                result = docToPdf(file, filePath);
                break;
            case "xls":
            case "xlsx":
                result = xlsToPdf(file, filePath);
                break;
            case "ppt":
            case "pptx":
                result = pptToPdf(file, filePath);
                break;
            default:
                System.out.println("暂不支持该类型:" + type);
        }
        return result;
    }

doc转pdf

private boolean docToPdf(File file, String filePath) throws OutOfMemoryError{
        FileOutputStream os = null;
        Document doc;
        boolean result = false;
        try {
        	//linux下转换乱码,安装windows字符集("cd /usr/share/fonts" 下安装)
            if(System.getProperty("os.name").toLowerCase().contains("linux")){
                FontSettings.getDefaultInstance().setFontsFolder(File.separator + "usr"
                        + File.separator + "share" + File.separator + "fonts" + File.separator, true);
            }
            os = new FileOutputStream(file);
            doc = new Document(filePath);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            
            result = true;
        } catch (Exception e) {
            logger.error("doc格式转换pdf格式出错:" + e.getStackTrace());
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
            }
        }
        return result;
    }

xsl转换pdf

private boolean xlsToPdf(File file, String filePath) throws OutOfMemoryError{
        FileOutputStream os = null;
        boolean result = false;
        try {
            os = new FileOutputStream(file);
            Workbook wb = new Workbook(filePath);
            wb.save(os, com.aspose.cells.SaveFormat.PDF);
            result = true;
        } catch (Exception e) {
            logger.error("xls格式转换pdf格式出错:" + e.getStackTrace());
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
                logger.error("xls格式StringBuffer关闭错误:" + e.getStackTrace());
            }
        }
        return result;
    }

ppt转换pdf

    private boolean pptToPdf(File file, String filePath) throws OutOfMemoryError{
        BufferedOutputStream out = null;
        boolean result = false;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            Presentation pres = new Presentation(filePath);// 输入pdf路径
            pres.save(out, com.aspose.words.SaveFormat.PDF);
            result = true;
        } catch (Exception e) {
            logger.error("ppt格式转换pdf格式出错:" + e.getStackTrace());
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
                logger.error("ppt格式StringBuffer关闭错误:" + e.getStackTrace());
            }
        }
        return result;
    }

重点!!!

  • doc.save(os, com.aspose.words.SaveFormat.PDF);
  • wb.save(os, com.aspose.cells.SaveFormat.PDF);
  • pres.save(os,com.aspose.slides.SaveFormat.Pdf);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值