word转换为pdf

package test;

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

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


public class WordToPdf {
	
    /** 
     * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice 
     *  
     * @param sourceFile 
     *            源文件,绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等. 
     *  
     * @param destFile 
     *            目标文件.绝对路径. 
     */  
    public static void word2pdf(String inputFilePath) {  
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();  
        String officeHome = getOfficeHome();  
        System.out.println("获取openOffice安装主目录"+officeHome);
        config.setOfficeHome(officeHome);  
      
        OfficeManager officeManager = config.buildOfficeManager();  
        officeManager.start();  
        System.out.println("开启openOffice服务");
      
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
        String outputFilePath = getOutputFilePath(inputFilePath);  
        File inputFile = new File(inputFilePath);  
        if (inputFile.exists()) {// 找不到源文件, 则返回  
            File outputFile = new File(outputFilePath);  
            if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径  
                outputFile.getParentFile().mkdirs();  
            }  
            converter.convert(inputFile, outputFile);  
        }  
        officeManager.stop();  
        System.out.println("关闭openOffice服务");
        System.out.println("WORD成功转换为PDF");
    }  
      
    public static String getOutputFilePath(String inputFilePath) {  
        String outputFilePath = inputFilePath.replaceAll(".doc", ".pdf");  
        return outputFilePath;  
    }  
      
    public static 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/OpenOffice.org 3";  
        } else if (Pattern.matches("Mac.*", osName)) {  
            return "/Application/OpenOffice.org.app/Contents";  
        }  
        return null;
    }  
    
    public static void main(String[] args) {
    	word2pdf("D:/XX系统需求分析文档.doc");
	}
}

 

你可能感兴趣的:(word)