Java uses openoffice to convert doc and docx into pdf instance code, openofficedocx

Source: Internet
Author: User

Java uses openoffice to convert doc and docx into pdf instance code, openofficedocx

This article focuses on Java programming using openoffice to convert doc and docx into pdf implementation code, as detailed below.

1. Required Software

OpenOffice, JodConverter

2. Start the OpenOffice Service

When I go online to check how to use OpenOffice for transcoding, I need to first use cmd to start a soffice service. The startup command is: soffice-headless-accept = "socket, host = 127.0.0.1, port = 8100; urp ;".

Memory) will always exist, and occupy about MB of memory, it feels very wasteful.So I thought of a way to directly call the commands that execute the service in Java code, and then directly kill the process when transcoding is complete. It will be explained in the following JAVA code.

Therefore, you can skip this step 2nd directly.

3. Add jar packages related to JodConverter to the project.

After extracting JodConverter, add all the jar packages under lib to the project.

Note: Install openoffice

4. The following is the focus. For details, see Java code parsing.
package cn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/ **
 * office to pdf
 * pdf to swf file
 * @author Administrator
 *
 * /
public class Converter {
private static String openOfficePath = "E: \\ install software \\ openoffice \\ date";
// Installation path of openoffice software
/ **
   * Convert Office documents to PDF. OpenOffice and jodconverter-2.2.2 are required to run this function
   * <pre>
   * Method example:
   * String sourcePath = "F: \\ office \\ source.doc";
   * String destFile = "F: \\ pdf \\ dest.pdf";
   * Converter.office2PDF (sourcePath, destFile);
   * </ pre>
   *
   * @param sourceFile
   * Source file, absolute path. Can be all documents in Office2003-2007 format, not tested in Office2010. Including .doc,
   * .docx, .xls, .xlsx, .ppt, .pptx, etc. Example: F: \\ office \\ source.doc
   * @param destFile
   * Object file. Absolute path. Example: F: \\ pdf \\ dest.pdf
   * @return message indicating the success or failure of the operation. If it returns -1, it means that the source file could not be found, or the url.properties configuration error; if it returns 0,
   * Means the operation was successful; return 1, it means the conversion failed
   * /
public static int office2PDF (String sourceFile, String destFile) {
try {
File inputFile = new File (sourceFile);
if (! inputFile.exists ()) {
return -1;
// If no source file is found, -1 is returned
}
// If the target path does not exist, create a new path
File outputFile = new File (destFile);
if (! outputFile.getParentFile (). exists ()) {
outputFile.getParentFile (). mkdirs ();
}
String OpenOffice_HOME = openOfficePath;
// Here is the installation directory of OpenOffice
// 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 service
String 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 an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection (
"127.0.0.1", 8100);
connection.connect ();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter (
connection);
converter.convert (inputFile, outputFile);
// close the connection
connection.disconnect ();
// Close the OpenOffice service process
pro.destroy ();
return 0;
}
catch (FileNotFoundException e) {
e.printStackTrace ();
return -1;
}
catch (IOException e) {
e.printStackTrace ();
}
return 1;
}
public static void main (String [] args) throws Exception {
String sourcePath = "C: \\ Users \\ Administrator \\ Desktop \\ 1 \\ Grouping table.xls";
String destFile = "C: \\ Users \\ Administrator \\ Desktop \\ 1 \\ dest.pdf";
int flag = Converter.office2PDF (sourcePath, destFile);
if (flag == 1) {
System.out.println ("Conversion failed");
} else if (flag == 0) {
System.out.println ("Successfully converted");
} else {
System.out.println ("Source file not found, or url.properties configuration error");
}
}
} 
Summary

The above is all about converting doc and docx into pdf instance code using openoffice in Java. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!


Related Article

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.