The main research in this paper is Java programming using openoffice to convert doc and docx into pdf implementation code, as follows.
OpenOffice, JodConverter
When I went online to check how to use OpenOffice for transcoding, I always needed to start a software service with cmd first. The command to start is: software -headless -accept="socket,host=127.0.0.1,port=8100;urp;".
But in fact, for my project, transcoding is only occasionally done. However, after the OpenOffice transcoding service is started, the process (the process name is software.exe) will always exist and occupy about 100M of memory, which feels very wasteful. So I thought of a way to call the commands that execute the service directly in Java code, and then when the transcoding is completed, I will kill the process directly. There will be an explanation in the JAVA code below.
So, in fact, this step 2 can be skipped directly
After decompressing JodConverter, add all the jar packages below lib to the project
Note: Install openoffice
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 converted to pdf * pdf converted 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. It can be a document in all formats of Office2003-2007, and it has not been tested for Office2010. Including .doc, * .docx, .xls, .xlsx, .ppt, .pptx, etc. Example: F://office//source.doc * @param destFile * Target file. Absolute path. Example: F://pdf//dest.pdf * @return A prompt message for success or not. If -1 is returned, it means that the source file cannot be found, or the url.properties configuration is incorrect; if 0 is returned, * means that the operation is successful; if 1 is returned, it means that the conversion has failed*/public static int office2PDF(String sourceFile, String destFile) {try {File inputFile = new File(sourceFile);if (!inputFile.exists()) {return -1;// If the source file cannot be found, return -1}// If the target path does not exist, create the new path File outputFile = new File(destFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}String OpenOffice_HOME = openOfficePath;//This 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 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);connect.connect();// convert DocumentConverter converter = new OpenOfficeDocumentConverter( connection);converter.convert(inputFile, outputFile);// close the connection connection.disconnect();// Close the process of OpenOffice 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//Group status list.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("Conversion successful");} else {System.out.println("The source file cannot be found, or url.properties configuration error");}}}The above is the entire content of this article about Java using openoffice to convert doc and docx into pdf instance code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!