1 ImportJava.io.File;2 3 Importcom.jacob.activeX.ActiveXComponent;4 ImportCom.jacob.com.Dispatch;5 6 Public classOfficetopdftools {7 8 Private Static Final intWdformatpdf = 17;9 Private Static Final intXlsformatpdf = 0;Ten Private Static Final intPptformatpdf = 32; One Private Static Final intMsoTrue =-1; A Private Static Final intmsofalse = 0; - - the Public Static Booleanconvert2pdf (String inputfile, String pdffile) { -String suffix =Getfilesufix (inputfile); -File File =NewFile (inputfile); - if(!file.exists ()) { +SYSTEM.OUT.PRINTLN ("file does not exist!) "); - return false; + } A if(Suffix.equals ("PDF")) { atSystem.out.println ("PDF not need to convert!"); - return false; - } - if(Suffix.equals ("Doc") | | | suffix.equals ("docx")) { - returnword2pdf (Inputfile, pdffile); -}Else if(Suffix.equals ("ppt") | | suffix.equals ("PPTX"))) { in returnppt2pdf (Inputfile, pdffile); -}Else if(Suffix.equals ("xls") | | suffix.equals ("xlsx")) { to returnexcel2pdf (Inputfile, pdffile); +}Else { -SYSTEM.OUT.PRINTLN ("file format does not support conversion!")); the return false; * } $ }Panax Notoginseng - Private Staticstring Getfilesufix (String fileName) { the intSplitindex = Filename.lastindexof (".")); + returnFilename.substring (Splitindex + 1); A } the + Private Static Booleanword2pdf (String inputfile, String pdffile) { - Try { $Activexcomponent app =NewActivexcomponent ("Word.Application"); $App.setproperty ("Visible",false); -Dispatch docs = app.getproperty ("Documents")). Todispatch (); -Dispatch doc = Dispatch.call (docs, "Open", Inputfile,false,true) the . Todispatch (); -File ToFile =NewFile (pdffile);Wuyi if(Tofile.exists ()) { the Tofile.delete (); - } WuDispatch.call (Doc, "ExportAsFixedFormat"), Pdffile, Wdformatpdf - ); AboutDispatch.call (Doc, "Close",false); $App.invoke ("Quit", 0); - return true; -}Catch(Exception e) { - return false; A } + } the - Private Static Booleanexcel2pdf (String inputfile, String pdffile) { $ Try { theActivexcomponent app =NewActivexcomponent ("Excel.Application"); theApp.setproperty ("Visible",false); theDispatch excels = App.getproperty ("Workbooks"). Todispatch (); theDispatch Excel = Dispatch.call (excels, "Open", Inputfile,false, - true). Todispatch (); inFile ToFile =NewFile (pdffile); the if(Tofile.exists ()) { the Tofile.delete (); About } theDispatch.call (Excel, "ExportAsFixedFormat", Xlsformatpdf, pdffile); theDispatch.call (Excel, "Close",false); theApp.invoke ("Quit"); + return true; -}Catch(Exception e) { the return false;Bayi } the the } - - Private Static Booleanppt2pdf (String inputfile, String pdffile) { the Try { theActivexcomponent app =NewActivexcomponent ( the"PowerPoint.Application"); theDispatch ppts = App.getproperty ("Presentations"). Todispatch (); -Dispatch ppt = Dispatch.call (ppts, "Open", Inputfile,true,//ReadOnly the true,//untitled Specifies whether the file has a title the false//WithWindow Specifies whether the file is visible the ). Todispatch ();94File ToFile =NewFile (pdffile); the if(Tofile.exists ()) { the Tofile.delete (); the }98Dispatch.call (ppt, "SaveAs", Pdffile, pptformatpdf); AboutDispatch.call (ppt, "Close"); -App.invoke ("Quit");101 return true;102}Catch(Exception e) {103 return false;104 } the }106 107 108 Public Static voidMain (string[] args) {109 //officetopdftools.convert2pdf ("C:\\ppt.pptx", "c:\\ppt.pdf"); theOfficetopdftools.convert2pdf ("C:\\excel.xls", "C:\\excel.pdf");111 //officetopdftools.convert2pdf ("C:\\word.docx", "c:\\word.pdf"); the 113 } the
Description: Download http://www.oschina.net/p/jacob/download Jacob pack
Java uses Jacob to turn office to PDF