Free Support Forum - aspose.com

ASPOSE.WORDS - Convert ASPX page to DOCX

Hi,
I would like to convert my .ASPX pages in a source folder to word documents (.DOCX) on the destination folder using ASPOSE.WORDS. Can this be done?

If this is not possible, I would like to convert the rendered .ASPX page from my local website to DOCX file. Please let me know if this can be achieved using ASPOSE.WORDS?

Thanks,
Siva

Hi Siva,


Thanks for your inquiry. 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)

{<o:p></o:p>

// Get HTML of the page<o:p></o:p>

System.IO.StringWriter oStringWriter = new StringWriter();<o:p></o:p>

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);<o:p></o:p>

base.Render(oHtmlTextWriter);<o:p></o:p>

StringReader reader = new StringReader(oStringWriter.ToString());<o:p></o:p>

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd()));<o:p></o:p>

Document doc = new Document(stream);<o:p></o:p>

doc.Save(Response, “ASPX to word.docx”, ContentDisposition.Inline, null);<o:p></o:p>

}<o:p></o:p>


I hope, this helps.

Best regards,

Hi Awais,
Thank you very much for your help and quick response. There are compilation errors for the Save method, I have attached the sample code project for your reference. I have downloaded the DLL version of Aspose.Words.dll and using the DLL from net3.5_ClientProfile directory. I have also tried the other DLL that we have from net3.5_ClientProfile_AuthenticodeSigned, it does not work in either cases. Could you please let me know what I was doing wrong with the attached code.

Regards,
Siva

I tried with the Aspose.Words.dll 2.0 dll with VS 2005, it worked but the output was not what I have on my Default.aspx page. It just adds the message “System.Web.UI.HtmlTextWriter” to the document, please take a look at the attached output document.

Thanks,
Siva

Hi Siva,


Thanks for the additional information.

First of all, the problem occurs because you are using Aspose.Words .NET 3.5 Client Profile that is why this overload of Save method is not available. The .NET 3.5 Client Profile assembly excludes System.Web and therefore HttpResponse is not available. This is entirely by design. If you need to use Aspose.Words in ASP.NET application, I would recommend you use .NET 2.0 and the following overload of Save method:
http://www.aspose.com/docs/display/wordsnet/Save+Overload_5

Secondly, please make the following change in your code (Default.aspx.cs) to be able to save the latest HTML string to Word document:

// replace our placeholders

string newContent = content.Replace("Option ", "Option Replace").Replace("Check Box 2", "Check Box2");

// write the new html to the page

writer.Write(newContent);

//System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(stringWriter);

StringReader reader = new StringReader(newContent);

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd()));

Document doc = new Document(stream);


I hope, this helps.

Best regards,