private static final int wdformatpdf = 17; private static final int xltypepdf = 0; private static final int ppsaveaspdf = 32; private static final int msoTrue =-1; private static final int msofalse = 0;
The use of Jacob http://www.oschina.net/code/snippet_128886_43591 is also
Call this method directly to public boolean convert2pdf (String inputfile, String pdffile) {string suffix = getfilesufix (inputfile); File File = new file (inputfile), if (!file.exists ()) {System.out.println ("files do not exist!) "); 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")) {return word2pdf (inputfile,pdffile);} else if (suffix.equals ("ppt") | | Suffix.equals ("pptx")) {return ppt2pdf (inputfile,pdffile);} else if (suffix.equals ("xls") | | Suffix.equals ("xlsx")) {return excel2pdf (inputfile,pdffile);} ELSE{SYSTEM.OUT.PRINTLN ("file format does not support conversion!"); return false;}} public static string Getfilesufix (string fileName) {int splitindex = Filename.lastindexof ("."); Return filename.substring (Splitindex + 1);} public boolean word2pdf (String inputfile,string pdffile) {try{//Opens the Word application activexcomponent app = new Activexcomponent (" Word.Application ");//Set Word not visible app.setproperty (" visible ", false);//Get all open documents in Word, return to documents object Dispatch doCS = App.getproperty ("Documents"). Todispatch ();//Call the Documents object in the Open method, and return the open Document object Documentdispatch doc = Dispatch.call (Docs, "Open", Inputfile,false,true). Todispatch ();//Call the SaveAs method of the Document object to save the documents in PDF format/* Dispatch.call (Doc, "SaveAs", Pdffile,wdformatpdf//word saved as PDF macro, value is); */dispatch.call (Doc, "ExportAsFixedFormat", Pdffile,wdformatpdf//word Save as PDF macro with a value of 17);//Close document Dispatch.call (DOC, "Close", false);//close Word application App.invoke ("Quit", 0); return true;} catch (Exception e) {return false;}} public boolean excel2pdf (String inputfile,string pdffile) {try{activexcomponent app = new Activexcomponent (" Excel.Application "), App.setproperty (" Visible ", false);D Ispatch excels = App.getproperty (" Workbooks "). Todispatch () ;D ispatch Excel = Dispatch.call (excels, "Open", Inputfile,false,true). Todispatch ();D Ispatch.call (Excel, " ExportAsFixedFormat ", Xltypepdf,pdffile);D Ispatch.call (Excel," Close ", false); App.invoke (" Quit "); return true;} catch (Exception e) {return false;}} public boolean ppt2pdf (String inputfile,string pdffile) {try{activexcomponent app = new Activexcomponent ("PowerPoint.Application");//app.setproperty ("Visible", msofalse);D Ispatch ppts = App.getproperty ("Presentations"). Todispatch ();D ispatch ppt = Dispatch.call (ppts, "Open", inputfile,true A//readonlytrue,//untitled that specifies whether the file has a caption false//withwindow the specified file is visible). Todispatch ();D ispatch.call (ppt, "SaveAs", Pdffile, ppsaveaspdf);D ispatch.call (ppt, "Close"); App.invoke ("Quit"); return true;} catch (Exception e) {return false;}}
JAVA Jacob Office convert PDF code