java实现word转pdf

gavin_sw 2007-04-09 12:14:28
谁有正常运行过的程序或配置,能否共享一下。

网上看了好多资料,最终没能正常运行,谢拉。
...全文
4174 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
架构师虎哥 2009-09-05
  • 打赏
  • 举报
回复
标记
cb1156 2008-04-12
  • 打赏
  • 举报
回复
GOOGLE 搜索 "JAVA操作OPENOFFICE"
第一个

还有
这是相当有难度的, 10分显然少了
zhangshudo 2008-04-12
  • 打赏
  • 举报
回复
这个可能是jacob版本的问题。

你换一个jacob可能会好得~
niuniuhuang 2007-04-25
  • 打赏
  • 举报
回复
mark
javaoaout 2007-04-16
  • 打赏
  • 举报
回复
mark
gavin_sw 2007-04-10
  • 打赏
  • 举报
回复
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
* @author XuMing Li
*
* @version 1.00, 2007-4-9
*
*/
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() {
//关闭word文件
wordCom.invoke("Quit", new Variant[] {});
}

/**
* * 将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();
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();
}
}

public static void main(String[] argv) {
D2P d2p = new D2P();
// d2p.openWord("c:/12.doc");
// d2p.callWordMacro("c:/12.docc", "MyWordMacro",
// new String[] { "这是调用word宏的测试程序" });
d2p.docToPDF("d:/12.doc", "c:/1p.ps", "c:/1p.pdf");
}
}

=================================================
When you create a PostScript file you have to send the host fonts.
Please go to the printer properties, "Adobe PDF Settings" page
and turn OFF the option "Do not send fonts to Distiller"
=================================================


com.jacob.com.ComFailException: Invoke of: PrintOut
Source: Microsoft Word
Description: SSORS=1

at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:804)
at com.jacob.com.Dispatch.callN(Dispatch.java:440)
at com.lixuming.util.D2P.docToPDF(D2P.java:87)
at com.lixuming.util.D2P.main(D2P.java:113)
gavin_sw 2007-04-10
  • 打赏
  • 举报
回复
用jacob
pengpeng2395 2007-04-10
  • 打赏
  • 举报
回复
是用jCom?
gavin_sw 2007-04-10
  • 打赏
  • 举报
回复
help
gavin_sw 2007-04-09
  • 打赏
  • 举报
回复
打开word文档就出错,你呢?
dada_911911 2007-04-09
  • 打赏
  • 举报
回复
请问你能实现PDF的显示吗?
ghb_1212 2007-04-09
  • 打赏
  • 举报
回复
www.itspw.com
gavin_sw 2007-04-09
  • 打赏
  • 举报
回复
望大家不吝赐教,谢谢。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧