word转pdf工具类

关键代码片段

public static void main(String[] args) {
        String docPath = "D:\\测试文件.docx";
        String pdfPath = "D:\\测试文件.pdf";
        docToPdf(docPath, pdfPath);

//        String excelPath = "D:\\测试文件.xls";
//        String pdfPath = "D:\\测试文件.pdf";
//        excelToPdf(excelPath, pdfPath);
    }

/**
     * word转pdf
     * @param wordPath word文件路径
     * @param pdfPath  pdf文件路径
     */
    public synchronized static void docToPdf(String wordPath, String pdfPath) {
        if(!getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(pdfPath);
            String os = System.getProperty("os.name");
            System.out.println(os);
            if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
                String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
                FontSettings.setFontsFolder(path, true);
            }
            FileOutputStream stream = new FileOutputStream(file);
            Document document = new Document(wordPath);
            // 全面支持DOC、DOCX、
            document.save(stream, SaveFormat.PDF);
            stream.close();
            long now = System.currentTimeMillis();
            System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

完整代码如下:

package com.xxx.common.utils;import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;/*** Pdf工具类*/
public class PDFUtil {/*** 获取验证授权*/private static boolean getLicense() {boolean result = false;try {InputStream stream = PDFUtil.class.getClassLoader().getResourceAsStream("license.xml");License license = new License();license.setLicense(stream);result = true;}catch (Exception e) {e.printStackTrace();}return result;}/*** word转pdf* @param wordPath word文件路径* @param pdfPath  pdf文件路径*/public synchronized static void docToPdf(String wordPath, String pdfPath) {if(!getLicense()) {return;}try {long old = System.currentTimeMillis();File file = new File(pdfPath);String os = System.getProperty("os.name");System.out.println(os);if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";FontSettings.setFontsFolder(path, true);}FileOutputStream stream = new FileOutputStream(file);Document document = new Document(wordPath);// 全面支持DOC、DOCX、document.save(stream, SaveFormat.PDF);stream.close();long now = System.currentTimeMillis();System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");}catch (Exception e) {e.printStackTrace();}}/*** xceld转pdf* @param excelPath excel文件路径* @param pdfPath  pdf文件路径*/public synchronized static void excelToPdf(String excelPath, String pdfPath) {if(!getLicense()) {return;}try {long old = System.currentTimeMillis();Workbook workbook = new Workbook(excelPath);String os = System.getProperty("os.name");System.out.println(os);if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";FontSettings.setFontsFolder(path, true);}FileOutputStream stream = new FileOutputStream(pdfPath);workbook.save(stream, com.aspose.cells.SaveFormat.PDF);stream.close();long now = System.currentTimeMillis();System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");}catch (Exception e) {e.printStackTrace();}}/*** xceld转pdf* @param excelPath excel文件路径* @param pdfPath  pdf文件路径*/public synchronized static void excelToPdfTwo(String excelPath, String pdfPath) {if(!getLicense()) {return;}try {long old = System.currentTimeMillis();Workbook workbook = new Workbook(excelPath);String os = System.getProperty("os.name");System.out.println(os);if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";FontSettings.setFontsFolder(path, true);}FileOutputStream stream = new FileOutputStream(pdfPath);PdfSaveOptions pdfSaveOptions=new PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(true);//当excel对应的页宽度太大时,在pdf中会拆断并分页,此处等比缩放int[] autoDrawSheets={3};autoDraw(workbook,autoDrawSheets);workbook.save(stream, pdfSaveOptions);stream.close();long now = System.currentTimeMillis();System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");}catch (Exception e) {e.printStackTrace();}}public static  void autoDraw(Workbook wb,int[] page){if(null!=page && page.length>0){for (int i=0;i<page.length;i++){wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();wb.getWorksheets().get(i).getVerticalPageBreaks().clear();}}}public static void main(String[] args) {String docPath = "D:\\测试文件.docx";String pdfPath = "D:\\测试文件.pdf";docToPdf(docPath, pdfPath);//        String excelPath = "D:\\测试文件.xls";
//        String pdfPath = "D:\\测试文件.pdf";
//        excelToPdf(excelPath, pdfPath);}}

JAVA--word等文件转PDF工具类相关推荐

  1. Java中导出pdf文件,pdf工具类demo

    最近在做导出pdf文件的功能,参考了很多资料和demo,完成了转出pdf的功能,并适合项目的pdf工具类,现贴出具体的工具类和demo,如有不对的地方欢迎指正 1.pdf工具类PdfUtil.java ...

  2. Word或Excel 转Pdf 工具类(License 去水印)

    Word或Excel 转Pdf 帮助类 需要引入jar包: aspose-words-15.8.0-jdk16.jar aspose-cells-8.5.2.jar 工具类 import com.as ...

  3. Java使用ftl模板文件生成Word,以及Word转换图片或Pdf工具类

    Java使用ftl模板文件生成Word 一.写在前面 最近在项目中使用打印功能,发现这个功能我已经写过多次了,下面这个文章的发步日期在2020年,不得不感慨时间之快啊. https://blog.cs ...

  4. java pdf工具类_Java PDF工具类(一)| 使用 itextpdf 根据PDF模板生成PDF(文字和图片)...

    Java PDF工具类(一)| 使用 itextpdf 根据设置好的PDF模板填充PDF(文字和图片) 相关文章: Java PDF工具类(二)| 使用 wkhtmltox 实现 HTML转PDF(文 ...

  5. java中文件操作的工具类

    代码: package com.lky.pojo;import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...

  6. Java学习日记8:文件上传工具类的实现和服务器给客户端发送图片

    文件上传的工具类: 每次文件上传要写很多东西,把这些封装成一个工具类,以后用起来就会方便很多. 先创建一个类存储上传文件的信息: package com.upload; /*** 每上传成功一个文件的 ...

  7. 分享一个文件上传工具类

    文件上传状态枚举类: View Code 1 package com.hoo.enums; 2 3 4 5 /** 6 7 * <b>function:</b> 文件上传状态 ...

  8. 利用若依@Excel注解导出PDF工具类

    利用若依@Excel注解导出PDF工具类 1.pom依赖 <!--PDF导出--><dependency><groupId>com.itextpdf</gro ...

  9. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

最新文章

  1. ASP.Net中利用CSS实现多界面两法
  2. 【面试题】如何设计一个高并发的系统?
  3. 递归式求时间复杂度的代入法与迭代法的举例讲解
  4. rstp 小米网络摄像头_国家部门调查联邦美国快递,联通VoLTE试商用开启,iOS蜂窝网络下载上限提高,小米申请屏下摄像头专利,这就是今天的其他大新闻!...
  5. 1 张图,拆解阿里 P8高级架构师必会技术栈!
  6. POJ - 3070 Fibonacci
  7. Python学习之函数及流程控制
  8. jQuery对象插件封装步骤
  9. 计算机组成名词解释移码,自考“计算机组成原理”串讲资料
  10. 杭电2031进制转换
  11. Linux系统开机启动流程介绍
  12. IM、RTC技术两生花,看融云如何打造“IM+RTC+Push”一站式通信云服务
  13. 关于MyBatis的缓存
  14. 重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor
  15. 互联网人在硅谷:听 Google 资深产品经理 bigjoe 聊聊职业与热爱
  16. 提升机类设备电动机保护器的保护范围及优化
  17. 如何应用quartz定时任务?
  18. 2019山东省赛总结
  19. 计算机专业,25岁成985高校博导,13篇顶会!入职半年发ICML,网友:万点暴击...
  20. 《在远方》首播获佳绩 刘烨改行送快递马伊琍长发少女感MAX

热门文章

  1. 二合一平板意外下滑,苹果和微软都拯救不了平板了?
  2. 张益唐对于素数间隔有限研究概述(转)
  3. 【每周一算】一张大饼切n刀,最多可以切多少块?
  4. setAttribute总结
  5. 古诗文,想要背得正确写得漂亮学得明白?买它!
  6. 简单的 PHP 抽奖程序
  7. Weekly Roundup: 补课QAQ
  8. Floyd 算法介绍
  9. Java Web程序设计基础二(服务器交互篇——九大内置对象)
  10. 桌面计算机硬盘打不开,电脑硬盘打不开怎么解决