Page 1 of 1

Can't open generated docx

PostPosted: Wed Jun 28, 2017 8:21 pm
by Zanza
Hello !
I have spent a lot of time on Docx4j those last days, and 2 days ago my code was working perfectly.
But yersterday, it's start to not working at all, without a apparent change.

Trying to isolated the non working code, I finally came to this :
(the test1.docx file is a blank file, just created and saved)
Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File("D:\\test1.docx"));
org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();P p = wmlObjectFactory.createP();
R r = wmlObjectFactory.createR();
Text text = wmlObjectFactory.createText();
text.setValue("sdfgsdlffgsdl");
p.getContent().add(r);
r.getContent().add(text);
wordMLPackage.getMainDocumentPart().addObject(p);
wordMLPackage.save(new File("D:\\test2.docx"));


And it still produce corrupted docx : the file is created, but when I try to open it, Word announce the file is corrupted, and can't be open.
BUT, this problem only appear when the file "test1.docx" is made with Word 2016. The code is still perfectly working with W2013, and 2010.

Is there a incompatibility with the last update of W2016 (from the 06/14) ?

Re: Can't open generated docx

PostPosted: Wed Jun 28, 2017 10:34 pm
by jason

Re: Can't open generated docx

PostPosted: Thu Jun 29, 2017 12:02 am
by filan
Hello,

i have a similar problem. Please find my test docx file in the attachments.

I am just loading the file and using mainDocumentPart.getContents() (in the below mentioned example only printing it out) and then writing it back to OutputStream (in this case it's within a JSP file)

The file is corrupted after that. but only if i use mainDocumentPart.getContents()

Code: Select all
WordprocessingMLPackage pkg = WordprocessingMLPackage.load(myInputStream);

MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
System.out.println(mainDocumentPart.getContents()); // this is producing the error

VContentOutputStream cos = null;
   try
   {
      cos = new VContentOutputStream();
      Save saver = new Save(wordMLPackage, partLoader);
      saver.save(cos);
      String filename = "test"+".docx";
      cos.writeTo(request, response, filename, true);         
   }
   catch (Exception e)
   {
      //
   }
   finally
   {
      if (cos!=null) cos.close();
   }

Re: Can't open generated docx

PostPosted: Thu Jun 29, 2017 1:25 am
by Zanza
Thank you Jason, it was what you thought!
I genereted a new jar with your fix, and it work.

But do you know if it is due to the last update of MSWord?
Because some code start of not working whitout reason..

Filan : Here is the version that fix my bug : https://we.tl/Z7wEb58P7S

Re: Can't open generated docx

PostPosted: Thu Jun 29, 2017 4:21 pm
by filan
thanks Zanza. that worked for me too. Guess i'll have to wait for 3.3.5.
we have 3.3.3 in production mode, i've testet it with 3.3.4 but i did not come to my mind to test with the nightly builds
does anyone know when 3.3.5 stable will come out?

Re: Can't open generated docx

PostPosted: Thu Jun 29, 2017 6:55 pm
by jason
A nightly build incorporating this fix can now be found at https://www.docx4java.org/docx4j/docx4j ... 170629.jar

I hope to release 3.3.5 tomorrow

Re: Can't open generated docx

PostPosted: Thu Jun 29, 2017 10:43 pm
by filan
thanks Jason, that would be great , since we are getting the library via maven.

I've already tested it with the .jar from Zanza, and it worked, so I think there is no need to test it again against your .jar

Re: Can't open generated docx

PostPosted: Wed Jun 12, 2019 8:39 pm
by lucash9
Could somebody help me with my file. I can't open it after using docx4j.