Read Word File Contents Using ASP.Net C#

Their often is a need to show uploaded Word file contents in a text box. So by considering that requirement I decided to write this article. So let us learn step-by-step how to read a Word file and display its content in a TextBox.
Requirements
This type of requirement can be the result of various scenarios, the most common are as follows:
  1. To upload a resume and show the contents in a TextBox as summary.
  2.  Save that resume contents into the database so later on it is useful for CV or resume parsing.
  3. In a blog or community website to upload file contents directly into the editor so it becomes faster to edit contents and save it.
Now let us see the preceding explanation by creating a sample web application as follows:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as "ReadWordFilesInFillTextBox" or another as you wish and specify the location.
  4. Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.
  5. Drag and drop two Buttons, a Fileuploader and TextBox control onto the <form> section of the Default.aspx page
  6. Set TextBox text mode to multiline.
Now the default.aspx page source code will looks as follows.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Article by Vithal Wadje</title>  
</head>  
<body bgcolor="navy">  
    <form id="form2" runat="server">  
    <div style="color: White;">  
        <h4>  
            Article for C#Corner  
        </h4>  
        <br />  
        <table width="100%">  
            <tr>  
                <td>  
                    <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server" Height="142px" Width="380px"></asp:TextBox><br />  
                </td>  
            </tr>  
        </table>  
        <br />  
        <table>  
            <tr>  
                <td>  
                    <asp:FileUpload ID="FileUpload1" runat="server" />  
                </td>  
                <td>  
                    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />  
                </td>  
                <td>  
                    <asp:Button ID="Button1" runat="server" Text="Clear" OnClick="Button1_Click" />  
                </td>  
            </tr>  
        </table>  
    </div>  
    </form>  
</body>  
</html>  
Now add the reference for Microsoft.Office.Interop by right-clicking the Solution Explorer to handle the Word file related process. I hope you have done that. The following namespaces are required to work with operations related to Word files:
    using System.IO;  
    using Microsoft.Office.Interop.Word;  
    using System.Text; 
Now double-click on the upload button and write the following code:
    protected void btnUpload_Click(object sender, EventArgs e)  
       {  
           //createting the object of application class  
           Application Objword = new Application();  
      
           //creating the object of document class  
           Document objdoc = new Document();  
      
           //get the uploaded file full path  
           dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);  
      
           //pass the optional (missing) parameter to API  
           dynamic NA = System.Type.Missing;  
      
           //open Word file document   
    objdoc = Objword.Documents.Open  
                  (ref FilePath, ref NA, ref NA, ref NA, ref NA,  
                   ref NA, ref NA, ref NA, ref NA,  
                   ref NA, ref NA, ref NA, ref NA,  
                   ref NA, ref NA, ref NA  
                    
                   );  
                   
      
          //creating the object of string builder class  
           StringBuilder sb = new StringBuilder();  
      
           for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)  
           {  
               string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();  
      
               if (Filedata != string.Empty)  
               {  
                   //Append word files data to stringbuilder  
                   sb.AppendLine(Filedata);  
               }  
                   
           }  
      
           //closing document object   
           ((_Document)objdoc).Close();  
      
           //Quit application object to end process  
           ((_Application)Objword).Quit();  
      
           //assign stringbuilder object to show text in textbox  
           TextBox1.Text =Convert.ToString(sb);  
       } 
Now double-click on the reset button and write the following code:
    protected void Button1_Click(object sender, EventArgs e)  
       {  
           TextBox1.Text =string.Empty;  
       } 
The entire code of the default.aspx.cs will look as in the following:
    using System;  
    using System.IO;  
    using Microsoft.Office.Interop.Word;  
    using System.Text;  
      
      
    public partial class _Default : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
      
        }  
        protected void btnUpload_Click(object sender, EventArgs e)  
        {  
            //createting the object of application class  
            Application Objword = new Application();  
      
            //creating the object of document class  
            Document objdoc = new Document();  
      
            //get the uploaded file full path  
            dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);  
      
            //pass the optional (missing) parameter to API  
            dynamic NA = System.Type.Missing;  
      
            //open Word file document   
     objdoc = Objword.Documents.Open  
                   (ref FilePath, ref NA, ref NA, ref NA, ref NA,  
                    ref NA, ref NA, ref NA, ref NA,  
                    ref NA, ref NA, ref NA, ref NA,  
                    ref NA, ref NA, ref NA  
                     
                    );  
                    
      
           //creating the object of string builder class  
            StringBuilder sb = new StringBuilder();  
      
            for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)  
            {  
                string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();  
      
                if (Filedata != string.Empty)  
                {  
                    //Append word files data to stringbuilder  
                    sb.AppendLine(Filedata);  
                }  
                    
            }  
      
            //closing document object   
            ((_Document)objdoc).Close();  
      
            //Quit application object to end process  
            ((_Application)Objword).Quit();  
      
            //assign stringbuilder object to show text in textbox  
            TextBox1.Text =Convert.ToString(sb);  
        }  
      
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            TextBox1.Text =string.Empty;  
        }  
    } 
Now run the application. The UI will look as follows:
In the preceding UI Browse control will be used to select the files from the physical location. On a upload button click, it will read the uploaded Word file and show it in the TextBox. The Clear button will clear the text box contents.
Now select the Word file and click on upload, it will show the file contents in the TextBox as follows.
 
Now you have seen how to read Word file contents and put it into a TextBox.
Notes
  • Do a proper validation such as if it has a file or not of the File Upload control when implementing.
Summary
From all the preceding examples you have learned how to read Word files and fill in a TextBox. I hope this article is useful for all readers, if you have a suggestion then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode