2013-08-16 38 views
0

我正在使用以下代碼將.doc轉換爲使用JOD的.pdf。JODConverter問題並在無頭模式下運行LibreOffice

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做轉換嗎?

+0

JODconverter 3.0似乎已經開始的LibreOffice如果需要的話,看到http://code.google.com/p/jodconverter/wiki/GettingStarted的一種方式。 –

回答

0

一種方法是來包裝你的CMD指令

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

爲java程序,請參見SO this問題。

的解決辦法是:

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命令在Linux中的Windows或shell腳本中創建批處理文件,並將其設置爲在Windows或Linux登錄時自動啓動。之後,執行你的代碼,因爲它是...

+0

爲什麼你在'cmd'中包裝libreoffice?這應該是多餘的。 –

0

你根本不需要JOD將doc文件轉換爲PDF。這可以用LibreOffice中直接完成:

libreoffice --headless --convert-to pdf document.doc 
相關問題