Free Support Forum -

Converting Word into HTML without saving file


I have developed a Word-style HTML editor which, with Aspose.Word, will allow users to create word documents through their browser. I would also like to enable the user to edit existing documents through their browser using the same editor.

The only way I have been able to “load” an existing document is through a number of steps:

a) Create a new Aspose.Word.Documnt (doc) object from the selected word document.
b) Call the Save method on new Document object setting the format as HTML.
c) Read the newly created HTML document into the browser.
d) Delete the newly created HTML document from the server.

I’m not keen on creating a temporary HTML file everytime an existing document edited. Is there an easier way to do this - ideally a method on Aspose.Word.Documnt which would just return the equivalent HTML string?


There is Document.Save that allows to save the document into a MemoryStream and then you can create a string from the bytes in the memory stream.

This still leaves the question open for images, you cannot save the images into the same memory stream or string. The images get saved into temporary files.

Thanks for your reply - I should have read the documentation more closely... As for images, there is no requirement to allow the insertion of images.

The problem I have now is when I try to access the MemoryStream - I get ObjectDisposedException: Cannot access closed a stream at the streamPosition = 0 line.

I am using the following code:

string docPath = Server.MapPath("") + \\AsposeWord.doc;

Aspose.Word.Document doc = new Aspose.Word.Document(docPath);

MemoryStream stream = new MemoryStream();

doc.Save(stream, SaveFormat.FormatHtml);

stream.Position = 0;

StreamReader reader = new StreamReader(stream);

string myDocument = reader.ReadToEnd();

What is interesting is if I change the SaveFormat to FormatText then there is no problem, but I need to use FormatHtml.

Maybe the HTML stream is getting closed, I’ll look at this issue. It should really not be closed by Aspose.Word.

What you can do is:

string s = System.Text.Encoding.ASCII.GetString(stream.ToArray());

That workaround seems to do the trick - thanks.

Just to clarify, the “FormatAsposePDF”, “FormatDocucument” and “FormatText” SaveFormats all appear to work fine when saving to a MemoryStream…only FormatHTML is causing problems.