[求助]java实现word批量转pdf 各位大大,快帮帮忙吧,老师给的题目,实在不知道怎么做了急求 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 估计你要分析doc和pdf的文件格式,然后在网上找找存取doc、pdf格式的API。我知道pdf是有Java API的,Office中的Excel也有,Word应该也有,但我没用过,你找找看。 这有一个,可以转一个啊,但是转两个就会报错,我也不知道为什么。package com.rizon.d2p;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[]{}); //wordCom.invoke("Quit",new Variant[0]); 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 = False; //是否追加打印 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 }); 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:/test/doc/doc.doc", "D:/test/ps/ps.ps", "D:/test/pdf/pdf.pdf"); //d2p.docToPDF("D:/test/doc/部署说明.doc", "D:/test/ps/部署说明.ps", "D:/test/pdf/部署说明.pdf");//把这句也加上就报错了,不能一次性导多个啊,不知道是那个东西没关闭。 } } 运行该程序还得安装不少东西呢,请看:http://blog.csdn.net/java_user/archive/2008/02/14/2095882.aspx 看看http://www.blogjava.net/lusm/archive/2007/03/27/106737.aspx 给解释一下这个正则表达式。 java中关于内部类的一些问题,请高手帮忙解释下! 字符串求值问题 Socket建立连接后~ 如何给URL类型的变量赋值??????? console如何清屏??? Java与xml 希望各位都能进看看 (收集JAVA异常类) 包的概念是什么?引用整个包a和import a.*有什么不同? 超级新手: 在98或winme下设置jdk时怎么设置系统环境变量PATH?? JNI调用VC创建无模式对话框的DLL问题 java中怎么将域名解析为ip
我知道pdf是有Java API的,Office中的Excel也有,Word应该也有,但我没用过,你找找看。
package com.rizon.d2p;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[]{});
//wordCom.invoke("Quit",new Variant[0]);
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 = False;
//是否追加打印
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 });
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:/test/doc/doc.doc", "D:/test/ps/ps.ps", "D:/test/pdf/pdf.pdf");
//d2p.docToPDF("D:/test/doc/部署说明.doc", "D:/test/ps/部署说明.ps", "D:/test/pdf/部署说明.pdf");//把这句也加上就报错了,不能一次性导多个啊,不知道是那个东西没关闭。
}
} 运行该程序还得安装不少东西呢,请看:http://blog.csdn.net/java_user/archive/2008/02/14/2095882.aspx