Java uses Jacob to turn office to PDF

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.