I am working on a function that allows me to create a simple word document from a string. I am using DocumentFormat.OpenXml Version="2.20.0" to create the word document. I don't understand why I can't save my word document in a memory stream whereas I can save the word document in a file.
  public Task<byte[]> ConvertToWordAsync(string text)
    {
        if (text.IsNullOrEmpty())
            return Task.FromResult(Array.Empty<byte>());
        using var memoryStream = new MemoryStream();
        using var wordDocument = WordprocessingDocument.Create(memoryStream, WordprocessingDocumentType.Document);
        
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
        mainPart.Document = new Document
        {
            Body = new Body()
        };
        Body body = mainPart.Document.Body;
        Paragraph paragraph = new Paragraph();
        Run run = new Run();
        Text bodyText = new Text(text);
        run.Append(bodyText);
        paragraph.Append(run);
        body.Append(paragraph);
        wordDocument.Save();
        
        return Task.FromResult(memoryStream.ToArray());
    }
When I call this function, the memory stream is always empty. If i change
using var wordDocument = WordprocessingDocument.Create(memoryStream, WordprocessingDocumentType.Document);
To
using var wordDocument = WordprocessingDocument.Create("C:\\Workspace\\65.docx", WordprocessingDocumentType.Document);
I am able to open the word file.
I don't understand why I can't save the same word file to a memory stream. Do you have any idea about the solution of this problem ?
 
    