在linux环境下,java怎么实现从word格式转换为pdf格式
答案:2 悬赏:60 手机版
解决时间 2021-02-01 11:57
- 提问者网友:骨子里的高雅
- 2021-01-31 20:33
在linux环境下,java怎么实现从word格式转换为pdf格式
最佳答案
- 五星知识达人网友:执傲
- 2021-01-31 22:08
跟windows下一样,java是虚拟机,跟windows平台和linux平台无关,根源你得清楚。
全部回答
- 1楼网友:傲气稳了全场
- 2021-01-31 22:15
import com.jacob.activex.activexcomponent;
import com.jacob.com.dispatch;
import com.jacob.com.variant;
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);
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;
}
public void closeword() {
//关闭word文件
wordcom.invoke( "quit ", new variant[] {});
}
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 ");
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯