http://xuexin0714.javaeye.com/blog/458026
http://topic.csdn.net/u/20080407/16/15E7B2B1-FD3F-41A6-9007-BF515EC41517.html
http://blog.csdn.net/lyzhang87/archive/2010/06/10/5662217.aspx
http://blog.csdn.net/wudi626/archive/2008/04/28/2337857.aspx
http://www.open-open.com/34.htm
http://www.oschina.net/p/podofo
http://podofo.sourceforge.net/
http://hi.baidu.com/jiang365/blog/item/a972fc95892c5444d1135ea4.html
http://adf.ly/91637/banner/http://sourceforge.net/projects/pdfcreator/
http://wiki.services.openoffice.org/wiki/API/Samples/Java/Office/DocumentHandling
http://api.openoffice.org/docs/DevelopersGuide/ProfUNO/ProfUNO.xhtml#1_4_2_C_2B_2B_Language_Binding
http://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export#How_to_use_it_from_C.2B.2B
http://www.javaeye.com/topic/352103
package com.rizon.word2pdf;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/** * 把 word 文档转化为 pdf 文档
*
* @author SUJL
*
*/
public class D2P {
private ActiveXComponent wordCom= null ;
private Object wordDoc= null ;
private final Variant False= new Variant( false );
private final Variant True= new Variant( true );
/**
* 打开 word 文档
*
* @param filePath word文档
* @return 返回word 文档对象
*/
public boolean openWord(String filePath) {
//建立 ActiveX 部件 wordCom= new ActiveXComponent("Word.Application");
try {
//返回 wrdCom.Documents 的 Dispatch Dispatch wrdDocs= wordCom.getProperty("Documents").toDispatch();
//调用 wrdCom.Documents.Open 方法打开指定的 word 文档,返回 wordDoc wordDoc= Dispatch.invoke(wrdDocs,"Open", Dispatch.Method,
new Object[] { filePath }, new int [1]).toDispatch();
return true ;
} catch (Exception ex) {
ex.printStackTrace();
}
return false ;
}
/**
* 关闭 word 文档
*/
public void closeWord( boolean saveOnExit) {
if (wordCom!= null ) {
//关闭 word 文件
//Dispatch.call(wordDoc, "Close", new Variant(saveOnExit)); wordCom.invoke("Quit", new Variant (0) );
//wordCom.invoke("Quit",new Variant[0]); wordCom.release(); //add 2008-07-07 wordCom= null ;
//释放在程序线程中引用的其它 com ,比如 Adobe PDFDistiller ComThread.Release();
}
}
/**
* 将 word 文档打印为 PS 文件后,使用 Distiller 将 PS 文件转换为 PDF 文件
*
* @param sourceFilePath
* 源文件路径
* @param destinPSFilePath
* 首先生成的 PS 文件路径
* @param destinPDFFilePath
* 生成 PDF 文件路径
*/
public void docToPDF(String sourceFilePath, String destinPSFilePath,
String destinPDFFilePath) {
if (!openWord(sourceFilePath)) {
closeWord( true );
return ;
}
//建立 Adobe Distiller 的 com 对象 ActiveXComponent distiller= new ActiveXComponent("PDFDistiller.PDFDistiller.1");
try {
//设置当前使用的打印机,我的 Adobe Distiller 打印机名字为 "Adobe PDF" wordCom.setProperty("ActivePrinter", new Variant("Adobe PDF"));
//设置 printout 的参数,将 word 文档打印为 postscript 文档。目前只使用了前 5 个参数,如果要使用更多的话可以参考 MSDN 的 office 开发相关 api
//是否在后台运行 Variant Background= True;
//是否追加打印 Variant Append= False;
//打印所有文档 int wdPrintAllDocument= 0;
Variant Range= new Variant(wdPrintAllDocument);
//输出的 postscript 文件的路径 Variant OutputFileName= new Variant(destinPSFilePath);
Dispatch.callN((Dispatch) wordDoc,"PrintOut", new Variant[] {
Background, Append, Range, OutputFileName });
System.out.println("由 word 文档打印为 ps 文档成功! ");
//调用 Distiller 对象的 FileToPDF 方法所用的参数,详细内容参考 Distiller Api 手册
//作为输入的 ps 文档路径 Variant inputPostScriptFilePath= new Variant(destinPSFilePath);
//作为输出的 pdf 文档的路径 Variant outputPDFFilePath= new Variant(destinPDFFilePath);
//定义 FileToPDF 方法要使用 adobe pdf 设置文件的路径,在这里没有赋值表示并不使用 pdf 配置文件 Variant PDFOption= new Variant("");
//调用 FileToPDF 方法将 ps 文档转换为 pdf 文档 Dispatch.callN(distiller,"FileToPDF", new Variant[] {
inputPostScriptFilePath, outputPDFFilePath, PDFOption });
//add 2008-07-07 Dispatch.call(wordDoc,"Close", new Variant( false ));
System.out.println("由 ps 文档转换为 pdf 文档成功! ");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeWord( true );
}
}
public static void main(String[] argv) {
D2P d2p= new D2P();
//d2p.openWord("D:/D2P/doc/doc.doc");
//d2p.callWordMacro( "c:/12.docc ", "MyWordMacro ",
// new String[] { "这是调用 word 宏的测试程序 " } ); d2p.docToPDF("D:/D2P/doc/doc.doc","D:/D2P/ps/ps.ps","D:/D2P/pdf/pdf.pdf"); //只转一个没问题
//d2p.docToPDF("D:/D2P/doc/部署说明 .doc", "D:/D2P/ps/ 部署说明 .ps", "D:/D2P/pdf/ 部署说明 .pdf");// 把注释去掉,再转一个就抱错了。为什么呢?一些资源没释放??? }
}