1

我正在使用以下代码使用 JOD 将 .doc 转换为 .pdf。

File inputFile = new File("document.doc");
File outputFile = new File("document.pdf");

// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();

// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);

// close the connection
connection.disconnect();

但我必须跑

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

分别以无头模式启动 LibreOffice。

有没有办法以编程方式启动 LibreOffice?或者,我们不能将 LibreOffice 文件夹的路径提供给 JOD 进行转换吗?

4

2 回答 2

1

您根本不需要 JOD 即可将 doc 文件转换为 PDF。这可以直接使用 LibreOffice 完成:

libreoffice --headless --convert-to pdf document.doc
于 2014-09-18T07:49:13.737 回答
0

一种方法是包装你的 cmd 指令

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

作为 java 进程,请参阅SO 上的这个问题。

解决方案可能是:

 File inputFile = new File("document.doc");
 File outputFile = new File("document.pdf");

 String[] args = {"cmd","/c","\\path to libreoffice executable","-headless",  "-accept='socket,host=127.0.0.1,port=8100;urp;'", "-nofirststartwizard"};

 try{
   Runtime rt = Runtime.getRuntime();
   ProcessBuilder pb = new ProcessBuilder(args);
   Process pr = pb.start();   

   // connect to an OpenOffice.org instance running on port 8100
   OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
   connection.connect();
 }catch{Exception e){
 }

 // convert
 DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
 converter.convert(inputFile, outputFile);

 // close the connection
 connection.disconnect();

它是临时的且未经测试的解决方案,但它可能会起作用。另一种选择是使用 cmd 命令在 windows 中创建批处理文件或在 linux 中创建 shell 脚本,并将其设置为在 windows 或 linux 登录时自动启动。之后按原样执行您的代码...

于 2013-08-22T13:18:20.007 回答