• JAVA--word等文件转PDF工具类


    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();
            }
        }

    完整代码如下:

    1. package com.xxx.common.utils;
    2. import com.aspose.cells.PdfSaveOptions;
    3. import com.aspose.cells.Workbook;
    4. import com.aspose.words.Document;
    5. import com.aspose.words.FontSettings;
    6. import com.aspose.words.License;
    7. import com.aspose.words.SaveFormat;
    8. import java.io.File;
    9. import java.io.FileOutputStream;
    10. import java.io.InputStream;
    11. /**
    12. * Pdf工具类
    13. */
    14. public class PDFUtil {
    15. /**
    16. * 获取验证授权
    17. */
    18. private static boolean getLicense() {
    19. boolean result = false;
    20. try {
    21. InputStream stream = PDFUtil.class.getClassLoader().getResourceAsStream("license.xml");
    22. License license = new License();
    23. license.setLicense(stream);
    24. result = true;
    25. }
    26. catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. return result;
    30. }
    31. /**
    32. * word转pdf
    33. * @param wordPath word文件路径
    34. * @param pdfPath pdf文件路径
    35. */
    36. public synchronized static void docToPdf(String wordPath, String pdfPath) {
    37. if(!getLicense()) {
    38. return;
    39. }
    40. try {
    41. long old = System.currentTimeMillis();
    42. File file = new File(pdfPath);
    43. String os = System.getProperty("os.name");
    44. System.out.println(os);
    45. if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
    46. String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
    47. FontSettings.setFontsFolder(path, true);
    48. }
    49. FileOutputStream stream = new FileOutputStream(file);
    50. Document document = new Document(wordPath);
    51. // 全面支持DOC、DOCX、
    52. document.save(stream, SaveFormat.PDF);
    53. stream.close();
    54. long now = System.currentTimeMillis();
    55. System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
    56. }
    57. catch (Exception e) {
    58. e.printStackTrace();
    59. }
    60. }
    61. /**
    62. * xceld转pdf
    63. * @param excelPath excel文件路径
    64. * @param pdfPath pdf文件路径
    65. */
    66. public synchronized static void excelToPdf(String excelPath, String pdfPath) {
    67. if(!getLicense()) {
    68. return;
    69. }
    70. try {
    71. long old = System.currentTimeMillis();
    72. Workbook workbook = new Workbook(excelPath);
    73. String os = System.getProperty("os.name");
    74. System.out.println(os);
    75. if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
    76. String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
    77. FontSettings.setFontsFolder(path, true);
    78. }
    79. FileOutputStream stream = new FileOutputStream(pdfPath);
    80. workbook.save(stream, com.aspose.cells.SaveFormat.PDF);
    81. stream.close();
    82. long now = System.currentTimeMillis();
    83. System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
    84. }
    85. catch (Exception e) {
    86. e.printStackTrace();
    87. }
    88. }
    89. /**
    90. * xceld转pdf
    91. * @param excelPath excel文件路径
    92. * @param pdfPath pdf文件路径
    93. */
    94. public synchronized static void excelToPdfTwo(String excelPath, String pdfPath) {
    95. if(!getLicense()) {
    96. return;
    97. }
    98. try {
    99. long old = System.currentTimeMillis();
    100. Workbook workbook = new Workbook(excelPath);
    101. String os = System.getProperty("os.name");
    102. System.out.println(os);
    103. if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
    104. String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
    105. FontSettings.setFontsFolder(path, true);
    106. }
    107. FileOutputStream stream = new FileOutputStream(pdfPath);
    108. PdfSaveOptions pdfSaveOptions=new PdfSaveOptions();
    109. pdfSaveOptions.setOnePagePerSheet(true);
    110. //当excel对应的页宽度太大时,在pdf中会拆断并分页,此处等比缩放
    111. int[] autoDrawSheets={3};
    112. autoDraw(workbook,autoDrawSheets);
    113. workbook.save(stream, pdfSaveOptions);
    114. stream.close();
    115. long now = System.currentTimeMillis();
    116. System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
    117. }
    118. catch (Exception e) {
    119. e.printStackTrace();
    120. }
    121. }
    122. public static void autoDraw(Workbook wb,int[] page){
    123. if(null!=page && page.length>0){
    124. for (int i=0;i
    125. wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
    126. wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
    127. }
    128. }
    129. }
    130. public static void main(String[] args) {
    131. String docPath = "D:\\测试文件.docx";
    132. String pdfPath = "D:\\测试文件.pdf";
    133. docToPdf(docPath, pdfPath);
    134. // String excelPath = "D:\\测试文件.xls";
    135. // String pdfPath = "D:\\测试文件.pdf";
    136. // excelToPdf(excelPath, pdfPath);
    137. }
    138. }

  • 相关阅读:
    Spring mvc源码分析系列--Servlet的前世今生
    【Leetcode】 96. 不同的二叉搜索树
    Java面试八股文之暑假合集
    基于ChatGPT用AI实现自然对话
    gslx680触摸屏驱动源码码分析(gslX680.c)
    Git版本控制工具使用
    Python 编程基础 | 第二章-基础语法 | 2.3、for 语句
    鸿翼归档:释放企业存储压力 提升数据利用效率
    设计提效-Figma技巧篇
    JavaScript学习笔记
  • 原文地址:https://blog.csdn.net/sinat_36279113/article/details/126281107