Connecting, foldable
The previous article said: When the server is Windows Server, use Jacob to convert MSOffice (referred to as word,excel,ppt) to PDF.
If the server being deployed to a project is a Linux server such as CentOS, it is not possible to use the previously mentioned way.
In the previous article about OpenOffice will msoffice into a PDF, there will be typographical dislocation problem, or some content disappears, this is because msoffice in some of the unique format, OpenOffice does not recognize the cause of the resolution. Of course, most of the ordinary MSOffice documents convert to PDF, the effect is still satisfactory.
So this time, how to use OpenOffice to complete the conversion of MSOffice to PDF.
PS: This article is about how Java calls the OpenOffice service of the native (current server) for document conversion, and calls OpenOffice on other servers for document conversions the next time.
1. Pre-conditions
Native test, this machine installs one OpenOffice software can.
If the server of the project is deployed, you can install a OpenOffice software locally on the server, or you can invoke the OpenOffice service on other servers for document conversion.
OpenOffice: http://www.openoffice.org/
My native OpenOffice Software installation directory: D:\software\OpenOffice 4, as shown in:
2. Create a project, introduce a jar package
1) Maven project. My local pom.xml configuration is as follows:
<Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Indi.johnny</groupId> <Artifactid>Openoffice-convert</Artifactid> <version>0.0.1-snapshot</version> <Dependencies> <Dependency> <groupId>Com.google.guava</groupId> <Artifactid>Guava</Artifactid> <version>19.0</version> </Dependency> <Dependency> <groupId>Com.github.livesense</groupId> <Artifactid>Jodconverter-core</Artifactid> <version>1.0.5</version> </Dependency> </Dependencies></Project>
2) Normal Java project will download the jar package from this address to the project: http://www.mvnrepository.com/artifact/com.github.livesense/jodconverter-core/ 1.0.5, guava package can also be found in http://www.mvnrepository.com, download the introduction of the project.
The Approximate jar package:
3. On the Code
PackageIndi.johnny.convert;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportOrg.artofsolving.jodconverter.OfficeDocumentConverter;Importorg.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;ImportOrg.artofsolving.jodconverter.office.OfficeManager;//Convert document to PDF Public classOpenofficepdfconvert {/** * @paramargs*/ Private StaticOfficemanager Officemanager; Private StaticString office_home = "D:/software/openoffice 4/"; Private Static intPort[] = {8100 }; Public voidConvert2pdf (String inputfile, String outputFile)throwsFileNotFoundException {startservice (); System.out.println ("Convert Document Conversion:" + inputfile + "+" +outputFile); Officedocumentconverter Converter=NewOfficedocumentconverter (Officemanager); Converter.convert (NewFile (Inputfile),NewFile (outputFile)); StopService (); System.out.println (); } //Open Server Public Static voidStartService () {defaultofficemanagerconfiguration configuration=Newdefaultofficemanagerconfiguration (); Try{System.out.println ("Ready to start service ...."); Configuration.setofficehome (office_home);//setting up the OpenOffice.org installation directoryConfiguration.setportnumbers (port);//set the conversion port, default to 8100Configuration.settaskexecutiontimeout (* 5L);//set task execution timeout to 5 minutesConfiguration.settaskqueuetimeout (* * 24L);//Set Task Queue timeout to 24 hoursOfficemanager=Configuration.buildofficemanager (); Officemanager.start (); //Start the serviceSYSTEM.OUT.PRINTLN ("Office conversion service started successfully!")); } Catch(Exception CE) {System.out.println ("Office conversion service failed to start!" More information: "+CE); } } //shutting down the server Public Static voidStopService () {System.out.println ("Turn off Office Transformation Services ...."); if(Officemanager! =NULL) {officemanager.stop (); } System.out.println ("Turn off Office conversion success!"); } Public Static voidMain (string[] args)throwsException {String path= "c:/users/johnny/desktop/Document/20170420/TEST/001/"; Openofficepdfconvert OPC=NewOpenofficepdfconvert (); Opc.convert2pdf (Path+ "1.docx", path+ "1.pdf"); }}
Replace the office_home in your code with the installation path of your own OpenOffice, and Port 8100 will not move.
Find a MSOffice document test run, it should be possible.
Java if word,excel,ppt how to convert PDF--openoffice (1)