目前版本: JODConverter v2.2.1, OpenOffice v3.0.0
使用需求: JDK1.4以上, 安�bOpenOffice v2.0.3以上
基本�介:
JODConverter 主要的功能是用�碜龈鞣N�n案的�D�Q. 目前�y��^, Word,Excel,PowerPoint�DPDF都是�]���}的.
因��JODConverter 是透�^OpenOffice�碜鲛D�Q, 所以使用前需要先安�bOpenOffice, �K且��OpenOffice的Service���, 才可以使用.
使用教�W:
Step1: 安�bOpenOffice
Step2: ���OpenOffice Service
1 cd C:\Program Files\OpenOffice.org 3\program
2 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
Step3:��JODConverter 的Jar�n放�M�0钢械�Library, ��z查你的�0甘欠癜�含以下的Jar�n: jodconverter -2.2.1.jar
jurt-2.3.0.jar
xstream-1.2.2.jar
ridl-2.3.0.jar
commons-io-1.3.1.jar
juh-2.3.0.jar
slf4j-api-1.4.3.jar
unoil-2.3.0.jar
slf4j-jdk14-1.4.3.jar
Step4: ��湟��word�n放在c:/document.doc
Step5: �绦幸韵鲁淌�
- import java.io.File;
- 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;
- public class JodDemo {
- public static void main(String[] args) throws Exception{
- File inputFile = new File("c:/document.doc");
- File outputFile = new File("c:/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();
- }
- }
程式�f明:
程式的部份相�����, 特�e要注意的地方是第12行�B�的port必��c你���OpenOffice的Port相同,
另外JODConverter �A�O是用副�n名作文件�N�的判��, 所以副�n名必�要正�_才行.
如果副�n名比�^特�e的�, 就必�在convert()的�r候��制指定Document Type.
心得:
JODConverter 使用起�硐喈�方便, 官�W也提供War�n�JODConverter �成Web Service提供�o不同的�Z言�砗艚�.
特�e要注意的是, OpenOffice Service�K不是ThreadSafe的, 多��Web AP在使用的�r候必�要注意.
�⒖假Y料:
http://www.artofsolving.com/opensource/jodconverter
那我也�硌a充一些好了
之前也在��@���n案�D�Q的程式
程式最好加上 try-catch
因�橹�前�l�F有些�n案 format 不能�D,�l生 Exception 後,connection 不��自�忧�啵�程式�� hand 住
所以改成如下方式:
- public void convert(String input, String output){
- File inputFile = new File(input);
- File outputFile = new File(output);
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
- try {
- connection.connect();
- DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
- converter.convert(inputFile, outputFile);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
- }
- }
再�恚�明明就是 open office 的�n案,�s生不能�D�Q的���}。例如:*.STW, *.SXD, *.ODF 等,後�聿胖�道可以自行指定�碓�n和�出�n的 mime-type,程式如下:
- public void convertSTW(String input, String output){
- DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
- DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
- DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
- File inputFile = new File(input);
- File outputFile = new File(output);
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
- try {
- connection.connect();
- DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
- converter.convert(inputFile, stw, outputFile, pdf);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
- }
- }
上面的程式是�D�Q STW 到 PDF,如果是 SXD / ODF �t只需要�更 DocumentFormat 的�热菁纯伞�
- DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");
- DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula", "odf");
所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter .DefaultDocumentFormatRegistry �e,但�K非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定�x DocumentFormat。