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

Free Support Forum - aspose.com

Save Word Document to MemoryStream Causes Unreadable Content using .NET

Good day,

We’ve recently upgraded to Aspose.Words 21.6.0

We are generating a docx file from a docx template. When we save it to a stream and when we open the file, Word shows an error dialog that says “Word found unreadable content …”

Screen Shot 2021-07-01 at 4.55.17 PM.png (4.7 KB)

then I click yes and another dialog pops out

Screen Shot 2021-07-01 at 4.55.24 PM.png (67.5 KB)

When I try to look the footnotes and endnotes on the document, there is none.

However I discovered something. If I save it directly to my disk, the generated document has no error whatsoever.

Simplified code below:

byte[] byteArray = _aWSS3Service.DownloadFile("template_simplified.docx");
using (MemoryStream mem = new MemoryStream())
{
    mem.Write(byteArray, 0, (int)byteArray.Length);
    Document doc = new Document(mem);

    // some logic here

    mem.Position = 0;
    doc.UpdateFields();

    // this one creates the problem, 
    //file opens with error
    doc.Save(mem, SaveFormat.Docx);

    // saving it on the disk has no problem
    // file opens with no error
    doc.Save("C:\\test.docx", SaveFormat.Docx);

    return mem.ToArray();
}

The thing is, we cannot write on the disk since our web app is hosted on AWS. I tried writing to AWS S3 storage but same error on the file.

I’m attaching a zip file with 3 files on it

  1. Template file
  2. Sample file - with no error (product on saving it directly on the disk)
  3. Sample file - with error, saving it on stream/memory

for aspose forum.zip (565.7 KB)

@HughMac

Please create separate memory stream for output document as shown below to avoid this issue.

byte[] byteArray = File.ReadAllBytes(MyDir + "template_simplified.docx");
using (MemoryStream mem = new MemoryStream())
using (MemoryStream outputStream = new MemoryStream())
{
    mem.Write(byteArray, 0, (int)byteArray.Length);
    Document doc = new Document(mem);

    // some logic here
    doc.UpdateFields();

                    
    // this one creates the problem, 
    //file opens with error
    doc.Save(outputStream, SaveFormat.Docx);

    // saving it on the disk has no problem
    // file opens with no error
    doc.Save(MyDir + "disk.docx", SaveFormat.Docx);

    //write to file
    FileStream file = new FileStream(MyDir + "stream.docx", FileMode.Create, FileAccess.Write);
    outputStream.WriteTo(file);
    file.Close();
    outputStream.Close();
}
1 Like

Thank you for your response, Tahir. Certainly solved the problem

Here is my final code that worked. Hope it will help someone.

byte[] byteArray = _aWSS3Service.DownloadFile("template_simplified.docx");
using (MemoryStream mem = new MemoryStream())
using (MemoryStream outputStream = new MemoryStream())
{
    mem.Write(byteArray, 0, (int)byteArray.Length);
    Document doc = new Document(mem);

    // some logic here

    mem.Position = 0;
    doc.UpdateFields();

    doc.Save(outputStream, SaveFormat.Docx);
    return outputStream.ToArray();
}

@HughMac

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.