word和excel转pdf

excel有水印,欢迎大佬解决@!!!!
支持word和excel新版老板格式!!!1

需要的jar包:百度能找到,免费上传资源已存在传不了
在这里插入图片描述


import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.License;

import java.io.*;

/**
 * @description:
 * @author: Jhang
 * @time: 2020/12/7 16:32
 */

public class ExcelAndWorldToPdf {
    /**
     * 获取license 主要除去水印
     * @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) {
        }
        return result;
    }

    /**
     * excel转pdf 支持xls或者xlsx
     *
     * @param sourceExcel excel文件具体地址,N:/sourceExcel
     * @param targetPdf   转换后pdf具体地址,N:/target.pdf
     * @return
     * @auth xll
     */
    public static String excel2pdf(String sourceExcel, String targetPdf) throws IOException {
        if (!getLicense()) {
            //水印没有消除
            return "failed:can't find aspose license";
        }
        FileOutputStream os = null;
        try {
            long start = System.currentTimeMillis();
            //新建一个空白pdf文档
            File file = new File(targetPdf);
            os = new FileOutputStream(file);
            Workbook wb = new Workbook(sourceExcel);
            wb.save(os, SaveFormat.PDF);
            long end = System.currentTimeMillis();
            System.out.println("excel转pdf共耗时:" + ((end - start) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
        return "successed";
    }

    /**
     * word转pdf 支持doc或者docx
     *
     * @param sourceDoc word文件具体地址,N:/source.dox
     * @param targetPdf 转换后pdf具体地址,N:/target.pdf
     * @return
     * @auth xll
     */
    public static String doc2pdf(String sourceDoc, String targetPdf) throws IOException {
        if (!getLicense()) {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            return "failed:can't find aspose license";
        }
        FileOutputStream os = null;
        try {
            long start = System.currentTimeMillis();
            //新建一个空白pdf文档
            File file = new File(targetPdf);
            os = new FileOutputStream(file);
            Document doc = new Document(sourceDoc);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            doc.protect(1,"1111");
            long end = System.currentTimeMillis();
            System.out.println("word转pdf共耗时:" + ((end - start) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
        return "successed";
    }


    public static void main(String[] args) throws IOException {
        //excel2pdf("F:\\exce\\test\\pdf\\FM.xlsx", "F:\\exce\\test\\pdf\\ToPDF.pdf");
       // excel2pdf("F:\\exce\\test\\pdf\\测试.xls", "F:\\exce\\test\\pdf\\NOE1.pdf");

     //   doc2pdf("F:\\exce\\test\\pdf\\test01.docx", "F:\\exce\\test\\pdf\\wold01ToPDF.pdf");
      //  doc2pdf("F:\\exce\\test\\pdf\\test.doc", "F:\\exce\\test\\pdf\\wold02ToPDF.pdf");

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值