Convert HTML String to DOCX Bytes

Want to check if it is possible to convert HTML string into docx format and return bytes.

Currently I’m able to convert HTML string into docx and save the file to my disk. And then read this file as byte array from disk. And later delete the physical file. I would like to avoid creating physical file as I just need byte array.

My current code to convert HTML string to docx file and save: (This code works perfectly fine).

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(html);
doc.Save(docFilePath);
byte[] b = File.ReadAllBytes(docFilePath);

Let me know if this is possible.

Thank you,
Shankar

@naru.shiva,

Sure, you can meet this requirement by using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(html);
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Docx);
byte[] b = stream.ToArray();

That worked. Thank you Awais!