1

I'm trying to process docx file with Apache POI. Just simply read and then write file (just for now). Here is my simple code:

FileInputStream fileInputStream = new FileInputStream(inputFile);   
XWPFDocument document = new XWPFDocument(OPCPackage.open(fileInputStream)); 
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
document.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();

Problem is that input file has small image in header. Because of that after processing input file with POI and opening output file in Microsoft Word I get corrupted file error :

Microsoft Office cannot open this file because some parts are missing or invalid.
Location: Part: /word/settings.xml, Line: 2, Column: 0

Everything works in OO Writer, but not in office.

The question is : what is wrong? Does apache POI not process files with image in header? Do you know any way to work around the problem?

I NEED to use Apache POI, I don't take into consideration other tools. Also I use POI 3.8

Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38
Maciek Murawski
  • 414
  • 4
  • 15
  • Firstly, I would want to validate that it is indeed the header that is causing the problem. Create a test document that only contains an imagine in the header and nothing else. Does that fail? If so, what are the differences between the XML before and after processing by POI? – ninesided Apr 08 '14 at 11:50
  • Indeed header was not causing the problem. Other file with image in header wes processed with no error. When I run diff I saw that `mc:Ignorable="w14"` parameter was removed from `w:settings` tag... – Maciek Murawski Apr 08 '14 at 13:33
  • What happens if you upgrade to the latest version of Apache POI? (Currently 3.10) – Gagravarr Apr 08 '14 at 20:42
  • Unfortunately, after processing with `POI 3.10-FINAL` Word still can't read file. Diff showed that `mc:Ignorable="w14"` wasn't removed, but was moved from the end of `w:settings` tag to it's beginning. – Maciek Murawski Apr 09 '14 at 07:05
  • I can't edit comment so... is there any way i can force `POI` not to edit `settings.xml` ? – Maciek Murawski Apr 09 '14 at 19:46

1 Answers1

0

The problem is not with the image header but with the Apache POI jar version. Use the latest jars.

poi-3.10-FINAL.jar
poi-ooxml-3.10-FINAL.jar
poi-ooxml-schemas-3.10-FINAL.jar
ooxml-schemas-1.1.jar

Having the above jars solved the issue for me.

kumareloaded
  • 3,882
  • 14
  • 41
  • 58