How to convert html/aspx webpage to word

Hi,

I’m a developer and I want to convert html/aspx webpage to word documents. Please guide how this can be accomplished?

Thanks

This message was posted using Email2Forum by Babar Raza.

Hi,

Thanks for your interest in Aspose.Words. You can use the following code to convert Html file into Word document:

Document doc = new Document(MyDir + "In.html");
doc.Save(MyDir + "Out.docx");

However, it is not guaranteed that the output Word document will look exactly the same as the input HTML. This is because Aspose.Words was originally designed to work with Microsoft Word documents, and HTML documents are quite different. That is why some HTML features are not supported upon importing HTML and some features are not supported upon exporting to HTML. You can find limitations upon HTML exporting/importing here:

https://docs.aspose.com/words/net/load-in-the-html-html-xhtml-mhtml-format/

https://docs.aspose.com/words/net/save-in-html-xhtml-mhtml-formats/

Secondly, I am afraid, you can not directly load an ASPX page into Aspose.Words’ DOM (Document Object Model). However, you can get the html representation of your ASPX page and then save that html to Word document or even to PDF format by using the following code snippet:

protected override void Render(HtmlTextWriter output)
{
    // Get HTML of the page
    System.IO.StringWriter oStringWriter = new StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
    base.Render(oHtmlTextWriter);
    StringReader reader = new StringReader(oStringWriter.ToString());
    MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd()));
    Document doc = new Document(stream);
    doc.Save(Response, "ASPX to word.docx", ContentDisposition.Inline, null);
}

I hope, this helps.

Best regards,