栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现office转pdf文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现office转pdf文件

java实现office文件转pdf文件

一、jar包下载和依赖引入
依赖jar百度云地址:https://pan.baidu.com/s/1_7Q5FLhdY7VYy89QTsgIQQ
提取码:bckw

		
            com.aspose
            ToPDF
            system
            ${project.basedir}/src/main/resources/jar/aspose-cells-20.7.jar
            1.0
        
        
            com.aspose
            worldToPDF
            system
            ${project.basedir}/src/main/resources/jar/aspose-words-18.5.0718-jdk16.jar
            1.0
        
        
            com.aspose
            ppt2Pdf
            system
            ${project.basedir}/src/main/resources/jar/aspose-slides-19.6.jar
            1.0
        

二、OfficePdfUtil 工具类

public class OfficePdfUtil {
    public static boolean getLicense(){
        boolean result = false;
        try {

            String license =
                    "n" +
                            "  n" +
                            "    n" +
                            "      Aspose.Total for Javan" +
                            "      Aspose.Words for Javan" +
                            "    n" +
                            "    Enterprisen" +
                            "    20991231n" +
                            "    20991231n" +
                            "    8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7n" +
                            "  n" +
                            "  sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=n" +
                            "";
            InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
            if(is!=null){
                License aposeLic = new License();
                aposeLic.setLicense(is);
                result = true;
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
	
    public static boolean docTopdf(String inPath, String outPath)  {
        boolean ret=false;
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
//            throw new Exception("com.aspose.words lic ERROR!");
            return ret;
        }
        System.out.println(inPath + " -> " + outPath);
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath);
            FileOutputStream os = new FileOutputStream(file);
            document doc = new document(inPath); // word文档
            // 支持RTF HTML,Opendocument, PDF,EPUB, XPS转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return ret;
        }
    }
    
    public static Boolean excelToPdf(String excelFilePath, String pdfFilePath){
        //验证License
        System.out.println(excelFilePath + " -> " + pdfFilePath);
        if (!getLicense()){
            return false;
        }
        try {
            Workbook wb = new Workbook(excelFilePath);
            File pdfFile = new File(pdfFilePath);
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
//            wb.save(fileOS, SaveFormat.PDF);
//            pdfSaveOptions.setonePagePerSheet(true);
            wb.save(fileOS, pdfSaveOptions);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static boolean pptxTopdf(String sourcePath, String savePath){
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
//            throw new Exception("com.aspose.words lic ERROR!");
            return false;
        }
        try {
            //读取ppt文件
            FileInputStream fileInput = new FileInputStream(sourcePath);
            Presentation pres = new Presentation(fileInput);
            //指定输出路径
            FileOutputStream outputStream = new FileOutputStream(new File(savePath));
            //输出
            pres.save(outputStream, SaveFormat.Pdf);
            outputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785343.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号