LibreOffice實現對office文件的轉換
阿新 • • 發佈:2018-11-11
package com.jalor.LibreOffice; import java.io.File; import java.util.Date; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; public class LibreOfficeAndJodconverter { public static String doDocToFdpLibre() { // File inputFile = new File("d:/1.txt"); // File inputFile = new File("d:/ppt.ppt"); // File inputFile = new File("d:/pptx.pptx"); // File inputFile = new File("d:/doc.doc"); File inputFile = new File("D:/招聘模組進度表2018.09.07.docx"); // File inputFile = new File("d:/xls.xls"); // File inputFile = new File("d:/jpg.jpg"); // File inputFile = new File("d:/gif.gif"); // txt:使用libreOffice來轉換pdf,轉換成功,但是中文有亂碼!!!!! // doc:這是office中的doc文件,可以轉換成功,並且中文沒有亂碼 // docx:這是office中的docx文件,可以轉換成功,並且中文沒有亂碼 // ppt和pptx:轉換成功。 // xls:轉換成功,沒有中文亂碼 // xlsx:轉換成功 // jpg和png:成功 System.out.println("libreOffice開始轉換.............................."); Long startTime = new Date().getTime(); OfficeManager officeManager = null; try { System.out.println("嘗試連線已啟動的服務..."); ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration(); externalProcessOfficeManager.setConnectOnStart(true); externalProcessOfficeManager.setPortNumber(8100); officeManager = externalProcessOfficeManager.buildOfficeManager(); officeManager.start(); System.out.println("轉換服務啟動成功!"); } catch (Exception e) { // 命令方式:soffice -headless // -accept="socket,host=127.0.0.1,port=8100;urp;" // -nofirststartwizard System.out.println("啟動新服務!"); String libreOfficePath = "E:/Program Files/LibreOffice"; // 此類在jodconverter-core中3版本中存在,在2.2.2版本中不存在 DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); // libreOffice的安裝目錄 configuration.setOfficeHome(new File(libreOfficePath)); // 設定埠號 configuration.setPortNumber(8100); // 設定任務執行超時為5分鐘 configuration.setTaskExecutionTimeout(1000 * 60 * 5L); // 設定任務佇列超時為24小時 configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L); // 開啟轉換服務 officeManager = configuration.buildOfficeManager(); officeManager.start(); System.out.println("服務啟動成功!"); } OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); System.out.println("===============對檔名稱進行處理中================="); String inputFileName = String.valueOf(inputFile); HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); // char[] input = inputFileName.trim().toCharArray(); // 獲取檔名稱 char[] input = inputFile.getName().trim().toCharArray(); // 獲取檔案所在的路徑名稱 String pathName = inputFileName.substring(0, inputFileName.lastIndexOf("\\") + 1); String outputFileName = ""; try { for (int i = 0; i < input.length; i++) { if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format); outputFileName += temp[0]; } else { outputFileName += Character.toString(input[i]); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); // Log.v(TAG, "BadHanyuPinyinOutputFormatCombination"); } String fileName = outputFileName.substring(0, outputFileName.lastIndexOf(".")); System.out.println("=====處理後的檔名稱======" + fileName); File outputFile = new File(pathName + fileName + ".pdf"); System.out.println("轉換後文件所在路徑:" + String.valueOf(outputFile)); // File outputFile = new File("d:/LibrePDF.pdf"); converter.convert(inputFile, outputFile); // converter.convert(inputFile, outputFile, outputFormat) // 轉換結束 officeManager.stop(); System.out.println("轉換結束。。。。。"); // 轉換時間 long endTime = new Date().getTime(); long time = endTime - startTime; System.out.println("libreOffice轉換所用時間為:" + time); return outputFile.getPath(); } public static void main(String[] args) { doDocToFdpLibre(); } }
所需JAR包:
commons-cli-1.2.jar
commons-io-1.4.jar
commons-logging-1.1.1.jar
itext-2.1.5.jar
jodconverter-2.2.2.jar
jodconverter-cli-2.2.2.jar
jodconverter-core-3.0-beta-4.jar
juh-3.0.1.jar
jurt-3.0.1.jar
pinyin4j-2.5.0.jar
ridl-3.0.1.jar
slf4j-api-1.5.6.jar
slf4j-jdk14-1.5.6.jar
unoil-3.0.1.jar
xstream-1.3.1.jar