Convert DOC file to DOCX with Java

前端 未结 7 1997
天涯浪人
天涯浪人 2021-02-20 12:43

I need to use DOCX files (actually the XML contained in them) in a Java software I\'m currently developing, but some people in my company still use the DOC format.

Do yo

7条回答
  •  悲哀的现实
    2021-02-20 13:30

    JODConvertor calls OpenOffice/LibreOffice via a network protocol. It can therefore 'do anything you can do in OpenOffice'. This includes converting formats. But it only does as good a job as whatever version of OpenOffice you are running. I have some art in one of my docs, and it doesn't convert them as I hoped.

    JODConvertor is no longer supported, according to the google code web site for v3.

    To get JOD to do the job you need to do something like

    private static void transformBinaryWordDocToDocX(File in, File out)
    {
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx");
        docx.setStoreProperties(DocumentFamily.TEXT,
        Collections.singletonMap("FilterName", "MS Word 2007 XML"));
    
        converter.convert(in, out, docx);
    }
    
    
    private static void transformBinaryWordDocToW2003Xml(File in, File out)
    {
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);;
        DocumentFormat w2003xml = new DocumentFormat("Microsoft Word 2003 XML", "xml", "text/xml");
        w2003xml.setInputFamily(DocumentFamily.TEXT);
        w2003xml.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2003 XML"));
        converter.convert(in, out, w2003xml);
    }
    
    
    
    private static OfficeManager officeManager;
    
    @BeforeClass
    public static void setupStatic() throws IOException {
    
              /*officeManager = new DefaultOfficeManagerConfiguration()
          .setOfficeHome("C:/Program Files/LibreOffice 3.6")
          .buildOfficeManager();
          */
    
        officeManager = new ExternalOfficeManagerConfiguration().setConnectOnStart(true).setPortNumber(8100).buildOfficeManager();
    
    
        officeManager.start();
    }
    
    @AfterClass
    public static void shutdownStatic() throws IOException {
    
        officeManager.stop();
    }
    

    For this to work you need to be running LibreOffice as a networked server ( I could not get the 'run on demand' part of JODConvertor to work under windows with LO 3.6 very well )

提交回复
热议问题