Reading Ms Word Document Display in the Page in ASP.NET

Introduction

This article contains description about how Word Document can be read and displayed in the screen using asp.net C#. reading Word document is very easy task , just have to follow some steps to get your task done. You have to follow only three steps are give bellow.

Steps

  1. Browse and read word document file from client machine
  2. Save it on the server
  3. Read file from server and display on the screen

Browse and Read Word Document from client machine

To browse I did nothing, just I used the browse or file up loader control available in the ASP.NET  server side controls list, the up loader controls in build asp.net controls helps to caching file information on the page .

Save it on the server and Read file from server and display on the screen

After browsing word document file don't refresh your page by any of the control, because in the every refresh the page loses his information and control information. Before the page's post back, save the browsed word document in the server. To save and view the word document I have take the help button click event and Microsoft.Office.Interop.Word dll.

 

  1. FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));  
  2. object filename = Server.MapPath(FileUpload1.FileName);  

The server control FileUplod's saveas function helps to save file in the server and Server.MapPath function is used to map the path of the server.

  1. Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();  

Above code create a object of application class which helps to open or create a word document. This class belongs Microsoft.Office.Interop name space.

  1. Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

Above code create the object of document class which keeps word content

  1. doc = AC.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);  
  2. TextBox1.Text = doc.Content.Text; 

above code opens the document that you have browsed set the content of the word document in to the textbox text.

HTML Code

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.     <div>  
  8.         <asp:TextBox ID="TextBox1" runat="server" Height="722px" Width="100%"  
  9.             TextMode="MultiLine"></asp:TextBox>  
  10.     </div>  
  11.     <div>  
  12.         <asp:FileUpload ID="FileUpload1" runat="server" BorderStyle="Double"  
  13.             Width="549px" /> <asp:Button ID="Button1" runat="server" Text="Button"  
  14.             onclick="Button1_Click" />  
  15.     </div>  
  16.     </form>  
  17. </body>  
  18. </html> 

C# Code  

  1. protected void Button1_Click(object sender, EventArgs e)    
  2. {    
  3.     Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();    
  4.     Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();    
  5.     object readOnly = false;    
  6.     object isVisible = true;    
  7.     object missing = System.Reflection.Missing.Value;    
  8.     try    
  9.     {    
  10.         doc = AC.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);    
  11.         TextBox1.Text = doc.Content.Text;    
  12.     }    
  13.     catch (Exception ex)    
  14.     {    
  15.         MessageBox.Show("ERROR: " + ex.Message);    
  16.     }    
  17.     finally    
  18.     {    
  19.         doc.Close(ref missing, ref missing, ref missing);    
  20.     }    
  21. }   

Summary

Above discussion we know how to read word document taking help of Microsoft.Office.Interop.Word


Similar Articles