C# Spire Document.SaveToStream not working

3.7k Views Asked by At

I have the following code but it is just creating a 0kb empty file.

using (var stream1 = new MemoryStream())
{
    MemoryStream txtStream = new MemoryStream();
    Document document = new Document();
    fileInformation.Stream.CopyTo(stream1);
    document.LoadFromStream(stream1, FileFormat.Auto);
    document.SaveToStream(txtStream, FileFormat.Txt);

    StreamReader reader = new StreamReader(txtStream);
    string text = reader.ReadToEnd();
    System.IO.File.WriteAllText(fileName + ".txt", text);
 }

I know the data is successfully loaded into document because if do document.SaveToTxt("test.txt", Encoding.UTF8); instead of the SaveToStream line it exports the file properly.

What am I doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

When copying a stream, you need to take care to reset the position to 0 if copying. As seen in the answer here, you can do something like this to your streams:

stream1.Position = 0;
txtStream.Position = 0;