The realization of Baidu Library--java use Openoffice,word to PDF

Source: Internet
Author: User

The main function of Baidu Library is to upload the Word document, transcoding into PDF format and then show it. There are four ways to do this:

method One: Use Apache Pio to read the doc file, and then convert the HTML file into a jsoup format HTML file, and finally use Itext to convert the HTML file into PDF. 

Method 2: Use Jdoctopdf to implement, this is a package good package, you can convert the doc to pdf,html,xml and other formats, call very convenient
you need to be aware of writing problems with Chinese fonts.

Method 3: Using Jodconverter to invoke OpenOffice's service to convert, OpenOffice has a version of each platform, so this approach is as cross-platform as method 1.
after installation to start OpenOffice Services, the specific starting method please Google

Method 4: One of the best methods, but requires a window environment, and the speed is the slowest need to install Msofficeword and SaveAsPDFandXPS.exe (a plugin for Word to convert Word to PDF)
The Office version is 2007 because Saveaspdfandxps is a plugin developed by Microsoft for office2007 and above

Here we adopt the third method, because of its cross-platform properties and simple and convenient operation.
 windows: 1 2 3. Download Jodconverter, and import all the jar packages under the Lib folder into Linux:  1. First install the OpenOffice for   Linux, can be downloaded to the official website can also use the command. Command download: wget http:  // sourceforge.net/ Projects/openofficeorg.mirror/files/4.1.1/binaries/zh-cn/apache_openoffice_4.1.1_linux_x86-64_install-rpm_ zh-cn.tar.gz   tar -xzvf apache_openoffice_4.1.1_linux_x86-64_      Install-rpm_zh-cn.tar.gz cd zh -CN CD RPMS RPM -ivh *.rpm  2. It is also possible to invoke command startup in Linux, but to tell you that it can be set to boot or boot with Tomcat, So that the transcoding program starts frequently. 

 Packagewordtopdf;ImportJava.io.File;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportCom.artofsolving.jodconverter.DocumentConverter;Importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;Importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;ImportCom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; Public classTopdftest { Public Static voidMain (string[] args) {office2pdf ("d:\\ resource management \ \ document. docx", "d:\\ resource management \ \ document. pdf"); }    /*** Convert Office documents to PDF. OpenOffice is required to run the function, OpenOffice is * http://www.openoffice.org/download/* * <pre> * Method Example: * String SourcePath = "F:\\office\\source.doc";      * String destfile = "F:\\pdf\\dest.pdf";      * Converter.office2pdf (SourcePath, destfile); * </pre> * *@paramsourcefile * source file, absolute path. Can be office2003-2007 in all formats, Office2010 not tested. Includes. doc, *. docx,. XLS,. xlsx,. ppt,. pptx, etc. Example: F:\\office\\source.doc *@paramdestfile * destination file. Absolute path. Example: F:\\pdf\\dest.pdf *@returnthe prompt information for the success or failure of the operation. Returns 1 indicating that the source file could not be found, or the Url.properties configuration error, if 0 is returned, * indicates a successful operation; returns 1, which indicates that the conversion failed*/       Public Static intoffice2pdf (String sourcefile, String destfile) {Try{File Inputfile=NewFile (sourcefile); if(!inputfile.exists ()) {                  return-1;//source file not found, return-1            }                //If the destination path does not exist, the path is createdFile OutputFile =NewFile (destfile); if(!Outputfile.getparentfile (). exists ())              {Outputfile.getparentfile (). Mkdirs (); } String openoffice_home= "C:\\Program Files (x86) \\OpenOffice 4";//here is the OpenOffice installation directory, in my project, in order to facilitate the expansion of the interface, not directly written like this, but this is absolutely no problem//if the last character of the URL address read from the file is not ' \ ', add ' \ '            if(Openoffice_home.charat (Openoffice_home.length ()-1)! = ' \ \ ') {Openoffice_home+= "\\"; }              //start the OpenOffice serviceString Command =Openoffice_home+ "program\\soffice.exe-headless-accept=\" socket,host=127.0.0.1,port=8100;urp;\ ""; Process Pro=runtime.getruntime (). exec (command); //Connect to a OpenOffice.org instance running on port 8100Openofficeconnection connection =NewSocketopenofficeconnection ("127.0.0.1", 8100);                Connection.connect (); //ConvertDocumentconverter converter =NewOpenofficedocumentconverter (connection);                Converter.convert (Inputfile, outputFile); //Close the connectionConnection.disconnect (); //shutting down the process of the OpenOffice servicePro.destroy (); return0; } Catch(FileNotFoundException e) {e.printstacktrace (); return-1; } Catch(IOException e) {e.printstacktrace (); }            return1; }  }

The realization of Baidu Library--java use Openoffice,word 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.