We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words Problem processing Docx/Docm

Hello everyone,

We use Aspose.Words for Java to fill um some form fields in our documents.

We are successfully using it since around 2007, never upgrading Aspose versions.

The need for the upgrade has arrived, with the need to process all 2007+ Office versions (docx and docm).

We are using Aspose inside our IBM FileNet Workplace (ECM) customized application.

At the time the users submit a new document, if the document fulfills some requirements, it is processed and some form fields are filled in.

For some reason I can’t understand, when the user downloads the new document, MS Word says it is corrupted. It asks if we want to try to recover the document, and it succeeds recovering the document.

Just for testing, I,ve renamend the problematic docx to .zip and tried to decompress it.
7Zip gives errors decompressing for some files, like “invalid compression method for file word\styles.xml”.

I’ve attached the original and the processed documents, so that you can help me finding out what’s wrong with the final document.

Thanks in Advance.

Filipe Sousa

Hi Filipe,

Thanks for your inquiry. Please create a standalone/runnable simple Java application that demonstrates the code you used to generate your output document. I will investigate the issue on my side and provide you more information.

Hi Tahir,

First of all, I’m sorry for the so late reply.

This is not a runnable example, but is the code that I am running to change my documents.

public static void processDoc(String srcFile, String dstFile) throws Exception {
    String mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    int number = 123123;
    String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    String docNumber = year + "/" + String.valueOf(number);

    try {
        com.aspose.words.Document document = new com.aspose.words.Document(srcFile);
        document.getRange().getFormFields().get("DocNumber").setResult(docNumber);

        int saveFormat;
        if ("application/msword".equals(mimeType)) {
            saveFormat = SaveFormat.DOC;
        } else if ("application/vnd.openxmlformats-officedocument.wordprocessingml.document".equals(mimeType)) {
            saveFormat = SaveFormat.DOCX;
        } else if ("application/vnd.ms-word.document.macroEnabled.12".equals(mimeType)) {
            saveFormat = SaveFormat.DOCM;
        } else {
            saveFormat = SaveFormat.DOC;
        }
        document.save(dstFile, saveFormat);
    } catch (UnsupportedFileFormatException e) {
        System.out.println(e.getLocalizedMessage());
        System.out.println("Error processing document with Aspose. " + mimeType);
    } catch (FileCorruptedException e) {
        System.out.println(e.getLocalizedMessage());
        System.out.println("Error processing document with Aspose, corrupted file");
    }
}

What I’ve found out today is, if I don’t give any SaveFormat to the document.save method, the file is saved without any problem.

As my source files are temp files, the extension only is not correct, it is .tmp.

As workaround I am saving the file to another temporary location, with the added extension and then, after saving, I am renaming it to the previous name.

As a test, I’ve tryied to rename the file and set the saveFormat. As expected, everything worked well.

Maybe in some place the filename is being taken into account instead of the given SaveFormat?

Hello again,

Other thing I’ve found out is if I use only Streams, everything works fine too.

For reading the source file, I’m creating a FileInputStream and for writing I am creating an OutputFileStream and the process runs smoothly.

Thanks for your support.

Hi Filipe,

Thanks for sharing the detail.

In your code, you are doing the followings

  1. Loading the document
  2. Set field value
  3. Save the document

There is no need to rename the file which has extension .tmp. Aspose.Words detects the file format. You may use Document Constructor (String, LoadOptions) method. Please use the LoadOptions.LoadFormat property to specify the format of the document to be loaded. Please use the latest version version of Aspose.Words for Java 14.4.1. Hope this helps you.

Moreover, FileFormatUtil class provides utility methods for working with file formats, such as detecting file format or converting file extensions to/from file format enums. please read the members of FileFormatUtil class from here:
https://reference.aspose.com/words/java/com.aspose.words/FileFormatUtil

If you want to load and save the document into stream, please read following documentation links.
https://docs.aspose.com/words/java/create-or-load-a-document/
https://docs.aspose.com/words/java/save-a-document/

If you still face problem, please share your document which contain the fields and have extension .tmp. I will investigate the issue on my side and provide you more information.