ASP.NET - How can show a .doc or .docx or .odt file in a webpage?

Asked By abhay rout on 01-Jul-11 03:26 AM
Hi All,

I'm facing a problem while showing a .doc file in a iframe on a web page.It's showing pdf file in the iframe without having any problem.

How can i able to the .doc file ?Please help.any help would be appreciable.


Thanks,
Abhay
dipa ahuja replied to abhay rout on 01-Jul-11 03:51 AM
Try this code:

protected void Page_Load(object sender, System.EventArgs e)
  {
    ApplicationClass wordApp = new ApplicationClass();
    object file = Server.MapPath("d1.doc");
    object nullobj = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
    ref file, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj, ref nullobj,     
    ref nullobj, ref nullobj, ref nullobj);
    
    doc.ActiveWindow.Selection.WholeStory();
    doc.ActiveWindow.Selection.Copy();
    string sFileText = doc.Content.Text;
    doc.Close(ref nullobj, ref nullobj, ref nullobj);
    wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
    Response.Write(sFileText);
  }
dipa ahuja replied to dipa ahuja on 01-Jul-11 03:53 AM
Forget to tell u reference:


using Microsoft.Office.Interop.Word;

which you can add by menu:
Website-> Add reference -> COM -> Microsfot word 12.0 object library.
Web Star replied to dipa ahuja on 01-Jul-11 04:20 AM
if you want to only show than use iframe as follows

put an <iframe> element on the web page like this:

<
iframe id="frmWord" runat="server" style="height: 1200px; width: 800px;"></iframe>

then in the page load event I added:

frmWord.Attributes["src"] = http://localhost/MyApp/resume.doc;
abhay rout replied to dipa ahuja on 01-Jul-11 04:20 AM
Hi Dipa,

But I couldn't COM -> Microsfot word 12.0 object library.  as ms word isn't installed on my machine.
Can i do it in a other way?

Thanks,
Abhay
dipa ahuja replied to abhay rout on 01-Jul-11 04:23 AM
ohh then this code won't run...

install ms word or find the dll on net if you get it from internet
abhay rout replied to Web Star on 01-Jul-11 04:24 AM
It opens the file instead of showing it inside the webpage.
abhay rout replied to dipa ahuja on 01-Jul-11 04:26 AM
Which ddll?Can you please give me the link to download that dll?
dipa ahuja replied to abhay rout on 01-Jul-11 04:31 AM
download microsoft word object 12.0 library dll

But i think better way is to install ms office
Shahzad Latif replied to abhay rout on 18-Nov-11 12:21 AM
It is not always very straight forward to view the Word documents in a web page unless you have some plug in for Word which can show the Word document in the browser. However, one easier option could be to http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.document.save_overload_1.html and then show those images in the web page for viewing purposes. If you want to go this way then you may try using http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx. It doesn't require MS Office to be installed on your development or deployment machines in order to process Word documents.

Disclosure: I work as a developer evangelist at Aspose.