1. 程式人生 > >基於openoffice的 office文件轉化為

基於openoffice的 office文件轉化為

依賴jar
在這裡插入圖片描述

原始碼

/**
 * 
 */
package com.b510.office2pdf;

import java.io.File;
import java.util.Date;
import java.util.regex.Pattern;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

/**
 * 這是一個工具類,主要是為了使Office2003-2007全部格式的文件(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
 * 轉化為pdf檔案<br>
 * Office2010的沒測試<br>
 * 
 * @date 2012-11-5
 * @author xhw
 * 
 */
public class Office2PDF {

	/**
	 * office中.doc格式
	 */
	public static final String OFFICE_DOC = "doc";
	/**
	 * office中.docx格式
	 */
	public static final String OFFICE_DOCX = "docx";
	/**
	 * office中.xls格式
	 */
	public static final String OFFICE_XLS = "xls";
	/**
	 * office中.xlsx格式
	 */
	public static final String OFFICE_XLSX = "xlsx";
	/**
	 * office中.ppt格式
	 */
	public static final String OFFICE_PPT = "ppt";
	/**
	 * office中.pptx格式
	 */
	public static final String OFFICE_PPTX = "pptx";
	/**
	 * pdf格式
	 */
	public static final String OFFICE_TO_PDF = "pdf";

	public static void main(String[] args) {
		Office2PDF office2pdf = new Office2PDF();
		office2pdf.openOfficeToPDF("e:/test." + OFFICE_DOCX, "e:/test_" + new Date().getTime() + "." + OFFICE_TO_PDF);
		office2pdf.openOfficeToPDF("e:/test." + OFFICE_PPTX, null); 
	}

	/**
	 * 使Office2003-2007全部格式的文件(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化為pdf檔案<br>
	 * 
	 * @param inputFilePath
	 *            原始檔路徑,如:"e:/test.docx"
	 * @param outputFilePath
	 *            目標檔案路徑,如:"e:/test_docx.pdf"
	 * @return
	 */
	public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
		return office2pdf(inputFilePath, outputFilePath);
	}

	/**
	 * 根據作業系統的名稱,獲取OpenOffice.org 3的安裝目錄<br>
	 * 如我的OpenOffice.org 3安裝在:C:/Program Files (x86)/OpenOffice.org 3<br>
	 * 
	 * @return OpenOffice.org 3的安裝目錄
	 */
	public String getOfficeHome() {
		String osName = System.getProperty("os.name");
		if (Pattern.matches("Linux.*", osName)) {
			return "/opt/openoffice.org3";
		} else if (Pattern.matches("Windows.*", osName)) {
			return "C:/Program Files (x86)/OpenOffice.org 3";
		} else if (Pattern.matches("Mac.*", osName)) {
			return "/Application/OpenOffice.org.app/Contents";
		}
		return null;
	}

	/**
	 * 連線OpenOffice.org 並且啟動OpenOffice.org
	 * 
	 * @return
	 */
	public OfficeManager getOfficeManager() {
		DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
		// 獲取OpenOffice.org 3的安裝目錄
		String officeHome = getOfficeHome();
		config.setOfficeHome(officeHome);
		// 啟動OpenOffice的服務
		OfficeManager officeManager = config.buildOfficeManager();
		officeManager.start();
		return officeManager;
	}

	/**
	 * 轉換檔案
	 * 
	 * @param inputFile
	 * @param outputFilePath_end
	 * @param inputFilePath
	 * @param outputFilePath
	 * @param converter
	 */
	public void converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {
		File outputFile = new File(outputFilePath_end);
		// 假如目標路徑不存在,則新建該路徑
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().mkdirs();
		}
		converter.convert(inputFile, outputFile);
		System.out.println("檔案:" + inputFilePath + "\n轉換為\n目標檔案:" + outputFile + "\n成功!");
	}

	/**
	 * 使Office2003-2007全部格式的文件(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化為pdf檔案<br>
	 * 
	 * @param inputFilePath
	 *            原始檔路徑,如:"e:/test.docx"
	 * @param outputFilePath
	 *            目標檔案路徑,如:"e:/test_docx.pdf"
	 * @return
	 */
	public boolean office2pdf(String inputFilePath, String outputFilePath) {
		boolean flag = false;
		OfficeManager officeManager = getOfficeManager();
		// 連線OpenOffice
		OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
		long begin_time = new Date().getTime();
		if (null != inputFilePath) {
			File inputFile = new File(inputFilePath);
			// 判斷目標檔案路徑是否為空
			if (null == outputFilePath) {
				// 轉換後的檔案路徑
				String outputFilePath_end = getOutputFilePath(inputFilePath);
				if (inputFile.exists()) {// 找不到原始檔, 則返回
					converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);
					flag = true;
				}
			} else {
				if (inputFile.exists()) {// 找不到原始檔, 則返回
					converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter);
					flag = true;
				}
			}
			officeManager.stop();
		} else {
			System.out.println("con't find the resource");
		}
		long end_time = new Date().getTime();
		System.out.println("檔案轉換耗時:[" + (end_time - begin_time) + "]ms");
		return flag;
	}

	/**
	 * 獲取輸出檔案
	 * 
	 * @param inputFilePath
	 * @return
	 */
	public String getOutputFilePath(String inputFilePath) {
		String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
		return outputFilePath;
	}

	/**
	 * 獲取inputFilePath的字尾名,如:"e:/test.pptx"的字尾名為:"pptx"<br>
	 * 
	 * @param inputFilePath
	 * @return
	 */
	public String getPostfix(String inputFilePath) {
		return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
	}

}

參照地址