java jacob pdf_java使用jacob将office文档转换为PDF格式

这段代码展示了如何使用Java的Jacob库将不同的Office文档(如Word、Excel和PowerPoint)转换为PDF格式。通过ActiveXComponent与Word、Excel和PowerPoint的应用程序接口交互,实现文件的读取、转换并保存为PDF。
摘要由CSDN通过智能技术生成

packagecom.ypr.modules.op.utils;importjava.io.File;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.ComFailException;importcom.jacob.com.ComThread;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;public classToPDF {private static final int wdFormatPDF = 17; //PDF 格式

private static final int xlTypePDF = 0; // xls格式public booleantoPDF(String sfileName, String toFileName) {

System.out.println("------开始转换------");

String suffix=getFileSufix(sfileName);

File file= newFile(sfileName);if (!file.exists()) {

System.out.println("文件不存在!");return false;

}if (suffix.equals("pdf")) {

System.out.println("PDF not need to convert!");return false;

}if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {returnword2PDF(sfileName, toFileName);

}else if (suffix.equals("ppt") || suffix.equals("pptx")) {returnppt2PDF(sfileName, toFileName);

}else if (suffix.equals("xls") || suffix.equals("xlsx")) {returnexcel2PDF(sfileName, toFileName);

}else{

System.out.println("文件格式不支持转换!");return false;

}

}

//截取文件后缀方法public staticString getFileSufix(String fileName) {int splitIndex = fileName.lastIndexOf(".");return fileName.substring(splitIndex + 1);

}

//转换word文档public booleanword2PDF(String sfileName, String toFileName) {long start =System.currentTimeMillis();

ActiveXComponent app= null;

Dispatch doc= null;boolean result = true;try{

app= new ActiveXComponent("Word.Application");

app.setProperty("Visible", new Variant(false));

Dispatch docs= app.getProperty("Documents").toDispatch();

doc= Dispatch.call(docs, "Open", sfileName).toDispatch();

System.out.println("打开文档..." +sfileName);

System.out.println("转换文档到 PDF..." +toFileName);

File tofile= newFile(toFileName);if(tofile.exists()) {

tofile.delete();

}

Dispatch.call(doc,"SaveAs", toFileName, wdFormatPDF);long end =System.currentTimeMillis();

System.out.println("转换完成..用时:" + (end - start) + "ms.");

result= true;

}catch(Exception e) {//TODO: handle exception

System.out.println("========Error:文档转换失败:" +e.getMessage());

result= false;

}finally{

Dispatch.call(doc,"Close", false);

System.out.println("关闭文档");if (app != null) {

app.invoke("Quit", newVariant[] {});

}

}

ComThread.Release();returnresult;

}

//转换excel文档public booleanexcel2PDF(String inputFile, String pdfFile) {

ActiveXComponent app= null;

Dispatch excel= null;boolean result = true;try{

app= new ActiveXComponent("Excel.Application");

app.setProperty("Visible", false);

Dispatch excels= app.getProperty("Workbooks").toDispatch();

excel= Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();

Dispatch.call(excel,"ExportAsFixedFormat", xlTypePDF, pdfFile);

System.out.println("打开文档..." +inputFile);

System.out.println("转换文档到 PDF..." +pdfFile);

result= true;

}catch(Exception e) {

result= false;

}finally{if (excel != null) {

Dispatch.call(excel,"Close");

}if (app != null) {

app.invoke("Quit");

}

}returnresult;

}

//转换ppt文档public booleanppt2PDF(String srcFilePath, String pdfFilePath) {

ActiveXComponent app= null;

Dispatch ppt= null;boolean result = true;try{

ComThread.InitSTA();

app= new ActiveXComponent("PowerPoint.Application");

Dispatch ppts= app.getProperty("Presentations").toDispatch();//因POWER.EXE的发布规则为同步,所以设置为同步发布

ppt = Dispatch.call(ppts, "Open", srcFilePath, true, //ReadOnly

true, //Untitled指定文件是否有标题

false//WithWindow指定文件是否可见

).toDispatch();

Dispatch.call(ppt,"SaveAs", pdfFilePath, 32); //ppSaveAsPDF为特定值32

System.out.println("转换文档到 PDF..." +pdfFilePath);

result= true; //set flag true;

} catch(ComFailException e) {

result= false;

}catch(Exception e) {

result= false;

}finally{if (ppt != null) {

Dispatch.call(ppt,"Close");

}if (app != null) {

app.invoke("Quit");

}

ComThread.Release();

}returnresult;

}public static voidmain(String[] args) {

ToPDF d= newToPDF();//d.wordToPDF("E:\\poi-test\\hadoop集群搭建.docx", "E:\\poi-test\\hadoop集群搭建.pdf");

d.toPDF("E:\\poi-test\\私有云清单.xlsx", "E:\\poi-test\\私有云清单.pdf");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值