1

I need to convert doc to docx for which I am using JODConveter (OpenOffice), but unfortunately my code is breaking with error code 2074. Can anyone throw more insight into what this errorCode means and how i can fix it.

My code is shared below :

OfficeManager officeManager =
    new DefaultOfficeManagerConfiguration().setOfficeHome(
    new File("C:\\Program Files (x86)\\OpenOffice4")).buildOfficeManager();

officeManager.start(); 

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); 

DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx");
docx.setStoreProperties(DocumentFamily.TEXT,
                        Collections.singletonMap("FilterName",
                                                 "MS Word 2007 XML"));

converter.convert(new File("C:\\localFiles\\abc.doc"),
                  new File("C:\\localFiles\\abc_new.docx"));

officeManager.stop();

However if I change the extension of my expected file from docx to pdf the above code works perfectly fine.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
Aman Bharti
  • 17
  • 1
  • 7
  • @Brutal_JL can you provide some inputs? – Aman Bharti Oct 19 '16 at 15:28
  • I, too get the error 2074 by using open office to remotely open and save documents in java code. As far as I read, Error 2074 means "Memory Leak". But I cannot resolve that issue, too. – TvR Jun 22 '23 at 08:51

1 Answers1

2

As you apparently are on Windows, there is a more stable solution that will also give you conversion results with a much better fidelity.

You will have to install any version of Office (2007 or later) or download and install the Compatibility Pack from Microsoft (if not already done). Then you can convert from .doc to .docx easily using the following command:

"C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme <input file> <output file>

where <input file> and <output file> need to be fully qualified path names.

The command can be easily applied to multiple documents using for:

for %F in (*.doc) do "C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme "%F" "%Fx"

Or you can call the command from Java:

Process p = Runtime.getRuntime().exec(
    new String[] {
        "C:\Program Files\Microsoft Office\Office12\wordconv.exe",
        "-oice",
        "-nme",
        "C:\\localFiles\\abc.doc",
        "C:\\localFiles\\abc_new.docx"
    });
int exitVal = p.waitFor();
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • I need to convert doc to docx in my Java code as i need to inject custom properties later by converting the resulting docx to a XWPFDocument document. I'm not sure if these commands, the ones you have provided, can be executed from code. – Aman Bharti Oct 19 '16 at 15:45
  • PS : I have Office installed on my Windows machine. – Aman Bharti Oct 19 '16 at 15:47