OpenOffice2PDF, using openoffice to convert word and excel to pdf

http://www.cnblogs.com/hold/archive/2013/03/10/2952855.html

copy code
  1 package com.xfzx.test.POI.main;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.Collections;
  6 import java.util.Date;
  7 import java.util.regex.Pattern;
  8 
  9 import org.artofsolving.jodconverter.OfficeDocumentConverter;
 10 import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
 11 import org.artofsolving.jodconverter.office.OfficeManager;
 12 
 13 public class OpenOffice2PDF {
 14 
 15     /**
 16      * office中各种格式
 17      */
 18     private static final String[] OFFICE_POSTFIXS = { "doc", "docx", "xls",
 19             "xlsx", "ppt", "pptx" };
 20     private ArrayList<String> Office_Formats = new ArrayList<String>();
 21 
 22     /**
 23      * pdf格式
 24      */
 25     private static final String PDF_POSTFIX= "pdf";
 26     
 27     /**
 28      * According to the name of the operating system, get the installation directory of OpenOffice.org 3. For example, my OpenOffice.org 3 is installed in: C:/Program
 29       * Files/OpenOffice.org 3
 30       */ 
31      
32      public String getOfficeHome() {
 33          String osName = System.getProperty("os.name" );
 34          if (Pattern.matches("Linux.*" , osName)) {
 35              return "/opt/openoffice.org3" ;
 36          } else  if (Pattern.matches( "Windows.*" , osName)) {
 37              return "D:/Program Files/OpenOffice.org 3" ;
 38          }
 39         return null;
 40     }
 41     /**
 42      * 转换文件
 43      * @param inputFilePath
 44      * @param outputFilePath
 45      * @param converter
 46      */
 47     public void converterFile(String inputFilePath, String outputFilePath,
 48             OfficeDocumentConverter converter) {
 49         File inputFile=new File(inputFilePath);
 50         File outputFile = new File(outputFilePath);
51          // If the target path does not exist, create a new path 
52          if (! outputFile.getParentFile().exists()) {
 53              outputFile.getParentFile().mkdirs();
 54          }
 55          converter.convert(inputFile, outputFile) ;
 56          System.out.println("File:" + inputFilePath + "\nConvert to\ntarget file:" + outputFile
 57                  + "\nSuccess!" );
 58      }
 59  
60      /** 
61       * make Office2003- 2007 documents in all formats (.doc|.docx|.xls|.xlsx|.ppt|.pptx) are converted into pdf files
 62       * 
 63       * @param inputFilePath
64       * Source file path, such as: "e:/test.docx"
 65       * @param outputFilePath
 66       * If specified, follow the specified method, if not specified (null), the target file path will be automatically generated according to the source file path, such as: " e:/test_docx.pdf"
 67       * @return 
68       */ 
69      public  boolean openOffice2Pdf(String inputFilePath, String outputFilePath) {
 70          boolean flag = false ;
 71          /* 
72           * Connect to OpenOffice.org and start OpenOffice.org
 73           */ 
74          DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
75          // Get the installation directory of OpenOffice.org 3 
76          String officeHome = getOfficeHome();
 77          config.setOfficeHome(officeHome);
 78          // Start the OpenOffice service 
79          OfficeManager officeManager = config.buildOfficeManager();
 80          officeManager.start() ;
 81          // Connect OpenOffice 
82          OfficeDocumentConverter converter = new OfficeDocumentConverter(
 83                  officeManager);
 84          long begin_time = new Date().getTime();
 85         File inputFile= new File(inputFilePath);
 86          Collections.addAll(Office_Formats, OFFICE_POSTFIXS);
 87          if (( null != inputFilePath) && (inputFile.exists())) {
 88              // Determine whether the target file path is empty 
89              if (Office_Formats.contains(getPostfix(inputFilePath))) {
 90                  if ( null == outputFilePath) {
 91                      // The converted file path 
92                      String outputFilePath_new = generateDefaultOutputFilePath(inputFilePath);
 93                     converterFile(inputFilePath, outputFilePath_new, converter);
 94                     flag = true;
 95 
 96                 } else {
 97                     converterFile(inputFilePath, outputFilePath, converter);
 98                     flag = true;
 99                 }
100             }
101 
102         } else {
103             System.out.println("con't find the resource");
104         }
105         long end_time = new Date().getTime();
 106          System.out.println("File conversion time: [" + (end_time - begin_time) + "]ms" );
 107          officeManager.stop();
 108          return flag;
 109      }
 110  
111      /** 
112       * If the output file path is not set, the output file address is generated according to the source file path and file name. For example, if the input is D:/fee.xlsx, the output is D:/fee_xlsx.pdf
 113       */ 
114      public String generateDefaultOutputFilePath(String inputFilePath) {
 115          String outputFilePath = inputFilePath.replaceAll("."
 116                  + getPostfix(inputFilePath), " _" + getPostfix(inputFilePath)
 117                 + ".pdf" );
 118          return outputFilePath;
 119      }
 120  
121      /** 
122       * Get the suffix name of inputFilePath, such as: "e:/test.pptx" suffix name: "pptx"
 123       */ 
124      public String getPostfix(String inputFilePath) {
 125          String[] p = inputFilePath.split("\\." );
 126          if (p.length > 0) { // Determine whether the file has an extension
 127              // Compare the file extension 
128              return p[p.length - 1 ];
 129          } else {
 130              return  null;
 131          }
 132      }
 133      
134      public  static  void main(String[] args) {
 135  
136          OpenOffice2PDF office2pdf = new OpenOffice2PDF();
 137          office2pdf.openOffice2Pdf("D:/The 2nd State Intellectual Property Office tug-of-war and field competition Notice.doc" ,
 138                  "D:/Notice of the 2nd State Intellectual Property Office Tug of War and Tian Jin Competition_" + new Date().getTime() + "."
 139                          + PDF_POSTFIX);
 140          office2pdf.openOffice2Pdf ("D:/Function custom call field_20130220_GC.xls", null );
 141      }
 142 
143 
144 }
copy code

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644942&siteId=291194637