office转pdf实现office在线预览(aspose)

1 篇文章 0 订阅
1 篇文章 0 订阅

1. jar包地址

链接: 百度云下载链接
提取码: 630z

2. 将jar包安装到本地

maven安装本地依赖教程

3. pom文件添加依赖

注:此处aspose版本为上一部安装到本地仓库的版本

            <dependency>
                <groupId>com.aspose</groupId>
                <artifactId>aspose-cells</artifactId>
                <version>${aspose-cells.version}</version>
            </dependency>

            <dependency>
                <groupId>com.aspose</groupId>
                <artifactId>aspose-words</artifactId>
                <version>${aspose-words.version}</version>
            </dependency>

            <dependency>
                <groupId>com.aspose</groupId>
                <artifactId>aspose-slides</artifactId>
                <version>${aspose-slides.version}</version>
            </dependency>

4. license.xml

① license.xml存放路径

license.xml为水印破解,放在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>

5. 工具类 AsposeUtil

package com.package.name;


import com.aspose.cells.PaperSizeType;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.*;

public class AsposeUtil {

    public static void main(String[] args) throws Exception {
        AsposeUtil bean = new AsposeUtil();
		bean.word2Pdf2("D:\\pdf\\12.docx","D:\\pdf\\12.pdf");;
    }
    
    /**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    public  boolean getLicense() {
        boolean result = false;
        try {
            //引入license.xml文件,去除水印
            InputStream is =this.getClass().getClassLoader().getResourceAsStream("static/license.xml");
            //注意此处为apose-slides的jar包
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    public  boolean getLicenseExcel() {
        boolean result = false;
        try {

            InputStream is =this.getClass().getClassLoader().getResourceAsStream("static/license.xml");
            //注意此处为对应aspose-cells的jar包
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();

            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    public boolean getLicensePpt(){
        boolean result = false;
        try {

            InputStream is =this.getClass().getClassLoader().getResourceAsStream("static/license.xml");
            //注意此处为对应aspose-slides的jar包
            com.aspose.slides.License aposeLic = new com.aspose.slides.License();

            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * word转pdf
     * inpath: 输入word的路径
     * outpath: 输出pdf的路径
     */
    public  void word2Pdf2(String inpath,String outpath) throws Exception {
        if (!getLicense()) {
            System.out.println("非法------------");
            return;
        }

        long old = System.currentTimeMillis();
        File file = new File(outpath);

        FileOutputStream os = new FileOutputStream(file);

        //解决乱码
        //如果是windows执行,不需要加这个
        //TODO 如果是linux执行,需要添加这个*****
        //FontSettings.setFontsFolder("/usr/share/fonts",true);

        Document doc = new Document(inpath);

        //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        doc.save(os, SaveFormat.PDF);

        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
    }


    /**
     * word转pdf
     * @param path      pdf输出路径
     * @param wordInput word输入流
     * @param wordName  word文档的名称
     */
    public  void word2pdf(String path, InputStream wordInput, String wordName) throws FileNotFoundException {
        if (!getLicense()) {
            System.out.println("非法");
            return;
        }

        //新建一个空白pdf文档
        long old = System.currentTimeMillis();
        File file = new File(path + wordName + ".pdf");
        FileOutputStream os = new FileOutputStream(file);

        //Address是将要被转化的word文档
        Document doc = null;
        try {
            doc = new Document(wordInput);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long now = System.currentTimeMillis();
        //转化用时
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
    }


    public void excel2Pdf(String path,String outpath) throws FileNotFoundException {
        FileOutputStream fileOutputStream = null;
        if (!getLicenseExcel()) {
            System.out.println("非法------------");
            return;
        }

        File file = new File(outpath);
        try {
            Workbook wb = new Workbook(path);
            fileOutputStream= new FileOutputStream(file);
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
            autoDraw(wb);

            wb.save(fileOutputStream, com.aspose.cells.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置打印的sheet 自动拉伸比例
     * @param ts
     */
    public void autoDraw(Workbook wb){
        if(wb.getWorksheets().getCount() > 0){
            for (int i = 0; i < wb.getWorksheets().getCount(); i++) {
                wb.getWorksheets().get(i).getPageSetup().setZoom(20);
                wb.getWorksheets().get(i).getPageSetup().setOrientation(PaperSizeType.PAPER_A_4);
            }
        }
    }

    public void ppt2Pdf(String path,String outpath)throws FileNotFoundException{
        if (!getLicensePpt()) {
            System.out.println("非法------------");
            return;
        }
        File file = new File(outpath);

        try {
            Presentation pres = new Presentation(path);//输入pdf路径
            FileOutputStream fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
            fileOS.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值