word,excel转成pdf

须要用到OpenOffice ,所以须要先安装OpenOffice:

然后java代码:

package com.lynxiaojing.officereview;
import java.io.File;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
public class TTTTTT {
    public static void main(String[] args) {
                      
        // 1) Start LibreOffice in headless mode.
        OfficeManager officeManager = null;
        try {
            officeManager = new DefaultOfficeManagerConfiguration()
                    .setOfficeHome(new File("D:\\Program Files (x86)\\OpenOffice 4"))//openOffice安装位置
                    .buildOfficeManager();
            officeManager.start();
              
            // 2) Create JODConverter converter
            OfficeDocumentConverter converter = new OfficeDocumentConverter(
                    officeManager);
              
            // 3) Create PDF
//            createPDF(converter);
            createPDF(converter);
              
        } finally {
            // 4) Stop LibreOffice in headless mode.
            if (officeManager != null) {
                officeManager.stop();
            }
        }
    }
              
    private static void createPDF(OfficeDocumentConverter converter) {
        try {
            long start = System.currentTimeMillis();
            converter.convert(new File("d:/WP8学习之--NavigationService类.docx"), new File(
                    "d:/HelloWorld.pdf"));
            System.out.println("Generate pdf/HelloWorld.pdf with "
                    + (System.currentTimeMillis() - start) + "ms");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

jar包地址:http://download.csdn.net/detail/water_lang/6926897

你可能感兴趣的:(word,excel转成pdf)