convert word to 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 {
	
    /**
     * Convert Office documents to PDF. OpenOffice, OpenOffice is required to run this function
     *  
     * @param sourceFile
     * Source file, absolute path. It can be documents in all formats of Office2003-2007, not tested for Office2010. Including .doc, .docx, .xls, .xlsx, .ppt, .pptx, etc.
     *  
     * @param destFile
     * Object file. Absolute path.
     */  
    public static void word2pdf(String inputFilePath) {  
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();  
        String officeHome = getOfficeHome();  
        System.out.println("Get the home directory of openOffice installation"+officeHome);
        config.setOfficeHome(officeHome);  
      
        OfficeManager officeManager = config.buildOfficeManager();  
        officeManager.start();  
        System.out.println("Open the openOffice service");
      
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
        String outputFilePath = getOutputFilePath(inputFilePath);  
        File inputFile = new File(inputFilePath);  
        if (inputFile.exists()) {// The source file cannot be found, then return  
            File outputFile = new File(outputFilePath);  
            if (!outputFile.getParentFile().exists()) { // If the target path does not exist, create a new path  
                outputFile.getParentFile().mkdirs();  
            }  
            converter.convert(inputFile, outputFile);  
        }  
        officeManager.stop();  
        System.out.println("Close the openOffice service");
        System.out.println("WORD successfully converted to 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 System Requirements Analysis Document.doc");
	}
}

 

Guess you like

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