[Java] office文档转换为pdf文档 →→→→→进入此内容的聊天室

来自 , 2020-01-23, 写在 Java, 查看 114 次.
URL http://www.code666.cn/view/5ec91aac
  1. import java.io.File;
  2.  
  3. import org.artofsolving.jodconverter.OfficeDocumentConverter;
  4. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
  5. import org.artofsolving.jodconverter.office.OfficeManager;
  6.  
  7. /**
  8.  * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档
  9.  * (.doc|.docx|.xls|.xlsx|.ppt|.pptx)转化为pdf文件
  10.  */
  11. public class OfficeToPDF {
  12.  
  13.     public static final String OFFICE_DOC = "doc";
  14.     public static final String OFFICE_DOCX = "docx";
  15.     public static final String OFFICE_XLS = "xls";
  16.     public static final String OFFICE_XLSX = "xlsx";
  17.     public static final String OFFICE_PPT = "ppt";
  18.     public static final String OFFICE_PPTX = "pptx";
  19.     public static final String OFFICE_TO_PDF = "pdf";
  20.  
  21.     /**
  22.      * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
  23.      *
  24.      * @param inputFilePath
  25.      *            源文件路径,如:"e:/test.docx"
  26.      * @param outputFilePath
  27.      *            目标文件路径,如:"e:/test_docx.pdf"
  28.      * @return
  29.      */
  30.     public static boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
  31.         return office2pdf(inputFilePath, outputFilePath);
  32.     }
  33.  
  34.     /**
  35.      * 获取OpenOffice.org 3的安装目录
  36.      *
  37.      * @return OpenOffice.org 3的安装目录
  38.      */
  39.     public static String getOfficeHome() {
  40.         String path = PropertiesUtil.getValueByPropertyName(
  41.                 ClassLoader.getSystemResourceAsStream("toPdf.properties"),
  42.                 "OPENOFFICE_PATH");
  43.         return path;
  44.     }
  45.  
  46.     /**
  47.      * 连接OpenOffice.org 并且启动OpenOffice.org
  48.      *
  49.      * @return
  50.      */
  51.     public static OfficeManager getOfficeManager() {
  52.         DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
  53.         // 获取OpenOffice.org 3的安装目录
  54.         String officeHome = getOfficeHome();
  55.         config.setOfficeHome(officeHome);
  56.         // 启动OpenOffice的服务
  57.         OfficeManager officeManager = config.buildOfficeManager();
  58.         officeManager.start();
  59.         return officeManager;
  60.     }
  61.  
  62.     /**
  63.      * 转换文件
  64.      *
  65.      * @param inputFile
  66.      * @param outputFilePath_end
  67.      * @param inputFilePath
  68.      * @param outputFilePath
  69.      * @param converter
  70.      */
  71.     public static void converterFile(File inputFile, String outputFilePath_end,
  72.             String inputFilePath, String outputFilePath,
  73.             OfficeDocumentConverter converter) {
  74.         File outputFile = new File(outputFilePath_end);
  75.         // 假如目标路径不存在,则新建该路径
  76.         if (!outputFile.getParentFile().exists()) {
  77.             outputFile.getParentFile().mkdirs();
  78.         }
  79.         converter.convert(inputFile, outputFile);
  80.     }
  81.  
  82.     /**
  83.      * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
  84.      *
  85.      * @param inputFilePath
  86.      *            源文件路径,如:"e:/test.docx"
  87.      * @param outputFilePath
  88.      *            目标文件路径,如:"e:/test_docx.pdf"
  89.      * @return
  90.      */
  91.     public static boolean office2pdf(String inputFilePath, String outputFilePath) {
  92.         boolean flag = false;
  93.         OfficeManager officeManager = getOfficeManager();
  94.         // 连接OpenOffice
  95.         OfficeDocumentConverter converter = new OfficeDocumentConverter(
  96.                 officeManager);
  97.         if (null != inputFilePath) {
  98.             File inputFile = new File(inputFilePath);
  99.             // 判断目标文件路径是否为空
  100.             if (null == outputFilePath) {
  101.                 // 转换后的文件路径
  102.                 String outputFilePath_end = getOutputFilePath(inputFilePath);
  103.                 if (inputFile.exists()) {// 找不到源文件, 则返回
  104.                     converterFile(inputFile, outputFilePath_end, inputFilePath,
  105.                             outputFilePath, converter);
  106.                     flag = true;
  107.                 }
  108.             } else {
  109.                 if (inputFile.exists()) {// 找不到源文件, 则返回
  110.                     converterFile(inputFile, outputFilePath, inputFilePath,
  111.                             outputFilePath, converter);
  112.                     flag = true;
  113.                 }
  114.             }
  115.             officeManager.stop();
  116.         } else {
  117.             flag = false;
  118.         }
  119.         return flag;
  120.     }
  121.  
  122.     /**
  123.      * 获取输出文件
  124.      *
  125.      * @param inputFilePath
  126.      * @return
  127.      */
  128.     public static String getOutputFilePath(String inputFilePath) {
  129.         String outputFilePath = inputFilePath.replaceAll("."
  130.                 + getPostfix(inputFilePath), ".pdf");
  131.         return outputFilePath;
  132.     }
  133.  
  134.     /**
  135.      * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"
  136.      *
  137.      * @param inputFilePath
  138.      * @return
  139.      */
  140.     public static String getPostfix(String inputFilePath) {
  141.         return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
  142.     }
  143.  

回复 "office文档转换为pdf文档"

这儿你可以回复上面这条便签

captcha