Java uses openoffice to convert doc docx to pdf instance code
- 2021-01-11 02:00:56
- OfStack
This paper mainly studies Java programming using openoffice to doc, docx into pdf implementation code, as follows.
1. The software you need
OpenOffice , JodConverter
2. Start OpenOffice services
soffice ="socket,host=127.0.0.1,port=8100 "soffice-headless-accept ="socket,host=127.0.0.1,port=8100" soffice-headless-accept ="socket,host=127.0.0.1,port=8100" urp;" .
In fact, for my project, transcoding is done only occasionally, but when the OpenOffice transcoding service is started, the process (process name is soffice.exe) will always exist and occupy about 100M memory, which feels very wasteful. So I figured out a way to call the command to execute the service directly in the Java code, and then kill the process when the transcoding is completed. This will be explained in the JAVA code below.
So, actually, step 2 can be skipped
3. Add jar packages related to JodConverter to the project
After unzipping JodConverter, add all the jar packages below lib to the project
Note: Install openoffice
4. Here are the key points. See Java code analysis for details
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 into pdf
* pdf into swf file
* @author Administrator
*
*/
public class Converter {
private static String openOfficePath = "E:\\ Install the software \\openoffice\\date";
//openoffice The installation path of the software
/**
* will Office Document conversion to PDF. You need to run this function OpenOffice and jodconverter-2.2.2
* <pre>
* Methods the sample :
* String sourcePath = "F:\\office\\source.doc";
* String destFile = "F:\\pdf\\dest.pdf";
* Converter.office2PDF(sourcePath, destFile);
* </pre>
*
* @param sourceFile
* The source file , An absolute path . Can be Office2003-2007 Documents in all formats , Office2010 Don't test . including .doc,
* .docx, .xls, .xlsx, .ppt, .pptx Etc. . The sample : F:\\office\\source.doc
* @param destFile
* The target file . An absolute path . The sample : F:\\pdf\\dest.pdf
* @return A message indicating whether the operation was successful or not . If the return -1, The source file cannot be found , or url.properties Configuration error ; If the return 0,
* The operation was successful ; return 1, This indicates that the transformation failed
*/
public static int office2PDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;
// The source file could not be found , It returns -1
}
// If the destination path doesn't exist , Create the path
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
String OpenOffice_HOME = openOfficePath;
// Here is the OpenOffice The installation directory of
// If you read from a file URL Address of the last 1 A character is not '\' , then add '\'
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
OpenOffice_HOME += "\\";
}
// Start the OpenOffice The service of
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();
// Shut down OpenOffice Process of the service
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 of 1 See table .xls";
String destFile = "C:\\Users\\Administrator\\Desktop\\1\\dest.pdf";
int flag = Converter.office2PDF(sourcePath, destFile);
if (flag == 1) {
System.out.println(" Convert defeat ");
} else if(flag == 0){
System.out.println(" Conversion of success ");
} else {
System.out.println(" The source file could not be found , or url.properties Configuration error ");
}
}
}
conclusion
The above is this article about Java using openoffice to doc, docx to pdf instance code of all the content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!