java 将office文件转换为pdf格式的文件


博客分类: java 将office文件转换为pdf格式的文件

Java.

     最近在做office文件转换为pdf的格式文件,工作终于告一段落,现在静下心来整理下代码。该类用到了一个日本开源的office文件转换pdf文件的jcom包(注意:需要加入jcom.dll文件,安装ms office和acrobat<不是reader>)。日本源码地址:http://sourceforge.net/projects/jcom代码如下:

package com.yyn.pdf;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;

/**
  * @author Andy
  * 2011-7-14
  * */
public class Office2Pdf {
/**
* @param officePath -office文件的原始路径
* @param pdfPath    -生成pdf的路径
* */
        public void createPDF(String officePath,String pdfPath) throws Exception {
                 ReleaseManager rm = null;
                IDispatch app = null;
                try {
                        rm=new ReleaseManager();
                        app = new IDispatch(rm, "PDFMakerAPI.PDFMakerApp");
                        app.method("CreatePDF",new Object[]{officePath,pdfPath});
                 } catch (Exception e) {
                        throw e;
                } finally {
                        try {
                                app=null;
                                rm.release();
                                rm = null;
                        } catch (Exception e) {
                                throw e;
                        }
                }
        }

        public static void main(String[] args) throws Exception {
                Office2Pdf one=new Office2Pdf();
                one.createPDF("D:\\Java\\workspace\\pdf\\test_files\\test.docx","D:\\Java\\workspace\\pdf\\test_files\\test.docx.pdf");
                 one.createPDF("D:\\Java\\workspace\\pdf\\test_files\\test.xls","D:\\Java\\workspace\\pdf\\test_files\\test.xls.pdf");
         }
}

你可能感兴趣的:(Office)