Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Jun 02, 2023 8:52 am

Hello team,

I was implementing a functionality that inserts document content using Spire.Doc and encountered a bug when creating the output file.

I've also reproduced the issue using a blank document and the code below:
Code: Select all
string sourcePath = //Insert path of blank document here
byte[] fileByteArr = System.IO.File.ReadAllBytes(sourcePath);
using (MemoryStream mem = new MemoryStream())
{
       mem.Write(fileByteArr, 0, fileByteArr.Length);
       Spire.Doc.Document doc = new Spire.Doc.Document(mem, FileFormat.Docx);
       mem.Position = 0;
       doc.SaveToStream(mem, FileFormat.Docx);

       string path = @"D:\res.docx";
       using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
       {
              mem.WriteTo(fs);
       }
}


When the created word doc is opened, an "unreadable content" error is displayed. (screenshot attached)

Spire.Doc v11.5.6
.Net Framework 4.7.2
Visual Studio Community 2019 running on Win10

jgmondero
 
Posts: 2
Joined: Tue Feb 07, 2023 2:42 am

Mon Jun 05, 2023 8:12 am

Hi,

Thank you for bringing the issue with our product to our attention. After testing your code, we have identified the problem.

It appears that you are writing the byte array of a file into a MemoryStream object, which you then load using our product. You set the position of the MemoryStream object to 0 and use our product to output the stream. However, after processing the stream with our product, the length of the resulting stream is different from the length of the original MemoryStream object. Despite this discrepancy, you continue to save the processed stream to the MemoryStream object, leading to the issue you described.
before.png
after.png

To resolve this, we recommend that you create a new MemoryStream object specifically for saving the document, rather than reusing the existing MemoryStream object. This should prevent any issues arising from discrepancies in stream lengths.

Please let us know if you have any further questions or concerns.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Thu Jun 08, 2023 3:43 am

Hi,

Greetings from E-iceblue.
How is the issue now? Is it been solved? We are looking forward to your feedback.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc

cron