欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2010 word 转换pdf jacob

程序员文章站 2024-01-30 09:14:46
...
package as.pdf;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordAsPDF {
public static void wordToPDF(String docfile, String toFile,int type) { 
        ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word 
        //ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel 
       //ActiveXComponent app = new ActiveXComponent("PowerPoint.Application"); // 启动PowerPoint 
        try { 
            app.setProperty("Visible", new Variant(false));  //转ppt的时候把false改为了true
            Dispatch docs = app.getProperty("Documents").toDispatch();  //word
            //Dispatch docs = app.getProperty("Workbooks").toDispatch();  //excel
            //Dispatch docs = app.getProperty("Presentations").toDispatch();  //ppt
            Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
            docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); 
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { 
                    toFile, new Variant(type) }, new int[1]); 
            Variant f = new Variant(false); 
            Dispatch.call(doc, "Close", f);  //转ppt的时候把这两行代码去掉
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            app.invoke("Quit", new Variant[] {}); 
        } 
    } 

public static void main(String[] args) {
//源文件全路径
String docfile ="D:\\Downloads\\b.docx";
//String docfile ="D:\\Downloads\\a.xls";
//String docfile ="D:\\Downloads\\a.ppt";
//些路径test为实际存在的目录,s后面为要另存为的文件名
String toFile="C:\\Documents and Settings\\Administrator\\桌面\\end\\"+17;
//wordToPDF(docfile, toFile,32);//ppt
wordToPDF(docfile, toFile,17);//word
//wordToPDF(docfile, toFile,57);//excel
}
}
需要 jacob.jar, jacob.bll放到jdk下
相关标签: pptx docx jacob