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");
}
}