Create New Document From HTML and Download to Client

Hi there, I am trying to create a document from an HTML source. Once the document is created, I want to send the document to the client’s browser. I don’t want to have to save it to the server.


I am just trying to get a basic sample working. However, when the file downloads it says it is empty. I also attached an image of what the file looks like when it tries to download to the Chrome browser.

Here is the code:

//Create a new document
Document doc = new Document();
//Create a document builder to begin building the document
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertHtml(“

Test HTML

”);

doc.Save(HttpContext.Current.Response, “ReportOut.docx”, ContentDisposition.Inline, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(SaveFormat.Docx));

Hey there, I found my answer though another forum post referenced here:

<a href="https://forum.aspose.com/t/68736

I modified the example code there to this and it worked, thanks.

var resp = System.Web.HttpContext.Current.Response;
// Create source document.
Document doc = new Document();
// Process data…
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(“

Test HTML

”);

// Create Memory Stream Object
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Pdf);
doc.Save(resp, “download.docx”, ContentDisposition.Inline, SaveOptions.CreateSaveOptions(SaveFormat.Docx));
resp.End();

Hi Coty,

Good to know that you have found the solution. Please feel free to contact us in case you have further comments or questions.

Best Regards,