Free Support Forum - aspose.com

Loading a docx and saving it again as docx results in corrupt file

Hi

We try to open a word document, perform a modification and save it again using aspose.words version 11.11.0.0.

When saving in doc format, we get a correct output.

When saving in docx format, word thinks, that the file is corrupt (The file UpdatedDoc.docx cannot be opened because there are problems with the content). It can repair the file and the content afterwards looks ok.

To solve the issue, we just created a very basic example. We created a docx file containing just the text "Test". Afterwards we loaded it with aspose and saved it again unmodified. We used the following code:

var path = Path.Combine(Path.GetTempPath(), "UpdatedDoc.docx");
if (File.Exists(path)) {
File.Delete(path);
}

MemoryStream outStream = new MemoryStream();
using (var stream = File.Open(@"TestEmptyDoc2.docx", FileMode.Open, FileAccess.Read)) {
var asposeDocX = new Document(stream);

OoxmlSaveOptions opt = new OoxmlSaveOptions(SaveFormat.Docx);
opt.Compliance = OoxmlCompliance.Ecma376_2006;
asposeDocX.Save(outStream, opt);
}
outStream.Flush();

byte[] outContent = outStream.GetBuffer();
using (var stream = File.Open(path, FileMode.CreateNew, FileAccess.ReadWrite)) {
stream.Write(outContent, 0, outContent.Length);
stream.Flush();
stream.Close();
}
Console.WriteLine(path);In the attachement, you can find both the original file (TestEmptyDoc2.docx) as well as the corrupt output file (UpdatedDoc.docx).

Thanks for you help.

Hi Markus,


Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 11.11.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system. The issue ID is WORDSNET-7596. Your request has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Moreover, please use the following code snippet as a workaround:

var path = Path.Combine(@“C:\Temp\out.docx”);

if (File.Exists(path))

{

File.Delete(path);

}

MemoryStream outStream = new MemoryStream();

using (var stream = File.Open(@"C:\Temp\TestEmptyDoc2.docx", FileMode.Open, FileAccess.Read))

{

var asposeDocX = new Document(stream);

OoxmlSaveOptions opt = new OoxmlSaveOptions(SaveFormat.Docx);

opt.Compliance = OoxmlCompliance.Ecma376_2006;

asposeDocX.Save(path, opt);

}


I hope, this helps.

Best regards,

Hi Markus,


It is to update you that our development team has finished working on your issue and has come to a conclusion that is your issue and the behaviour you’re observing is actually not a bug in Aspose.Words. The problem occurs because of a common mistake in MemoryStream usage. The GetBuffer method returns INTERNAL stream buffer and the size of this buffer is bigger than the actual stream length in common. In this case MemoryStream.ToArray() method will fix the problem.

Best regards,

The issues you have found earlier (filed as WORDSNET-7596) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan