Read Word File Contents and Fill Textbox Using ASP.Net C#

In this article we will learn how to read a Word File and fill in a Textbox using ASP.NET.

Background

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.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Article by Vithal Wadje</title>  
  7. </head>  
  8. <body bgcolor="navy">  
  9.     <form id="form2" runat="server">  
  10.     <div style="color: White;">  
  11.         <h4>  
  12.             Article for C#Corner  
  13.         </h4>  
  14.         <br />  
  15.         <table width="100%">  
  16.             <tr>  
  17.                 <td>  
  18.                     <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server" Height="142px" Width="380px"></asp:TextBox><br />  
  19.                 </td>  
  20.             </tr>  
  21.         </table>  
  22.         <br />  
  23.         <table>  
  24.             <tr>  
  25.                 <td>  
  26.                     <asp:FileUpload ID="FileUpload1" runat="server" />  
  27.                 </td>  
  28.                 <td>  
  29.                     <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />  
  30.                 </td>  
  31.                 <td>  
  32.                     <asp:Button ID="Button1" runat="server" Text="Clear" OnClick="Button1_Click" />  
  33.                 </td>  
  34.             </tr>  
  35.         </table>  
  36.     </div>  
  37.     </form>  
  38. </body>  
  39. </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:
  1. using System.IO;  
  2. using Microsoft.Office.Interop.Word;  
  3. using System.Text; 
Now double-click on the upload button and write the following code:
  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2.    {  
  3.        //createting the object of application class  
  4.        Application Objword = new Application();  
  5.   
  6.        //creating the object of document class  
  7.        Document objdoc = new Document();  
  8.   
  9.        //get the uploaded file full path  
  10.        dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);  
  11.   
  12.        // the optional (missing) parameter to API  
  13.        dynamic NA = System.Type.Missing;  
  14.   
  15.        //open Word file document   
  16. objdoc = Objword.Documents.Open  
  17.               (ref FilePath, ref NA, ref NA, ref NA, ref NA,  
  18.                ref NA, ref NA, ref NA, ref NA,  
  19.                ref NA, ref NA, ref NA, ref NA,  
  20.                ref NA, ref NA, ref NA  
  21.                 
  22.                );  
  23.                
  24.   
  25.       //creating the object of string builder class  
  26.        StringBuilder sb = new StringBuilder();  
  27.   
  28.        for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)  
  29.        {  
  30.            string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();  
  31.   
  32.            if (Filedata != string.Empty)  
  33.            {  
  34.                //Append word files data to stringbuilder  
  35.                sb.AppendLine(Filedata);  
  36.            }  
  37.                
  38.        }  
  39.   
  40.        //closing document object   
  41.        ((_Document)objdoc).Close();  
  42.   
  43.        //Quit application object to end process  
  44.        ((_Application)Objword).Quit();  
  45.   
  46.        //assign stringbuilder object to show text in textbox  
  47.        TextBox1.Text =Convert.ToString(sb);  
  48.    } 
Now double-click on the reset button and write the following code:
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.    {  
  3.        TextBox1.Text =string.Empty;  
  4.    } 
The entire code of the default.aspx.cs will look as in the following:
  1. using System;  
  2. using System.IO;  
  3. using Microsoft.Office.Interop.Word;  
  4. using System.Text;  
  5.   
  6.   
  7. public partial class _Default : System.Web.UI.Page  
  8. {  
  9.     protected void Page_Load(object sender, EventArgs e)  
  10.     {  
  11.   
  12.     }  
  13.     protected void btnUpload_Click(object sender, EventArgs e)  
  14.     {  
  15.         //createting the object of application class  
  16.         Application Objword = new Application();  
  17.   
  18.         //creating the object of document class  
  19.         Document objdoc = new Document();  
  20.   
  21.         //get the uploaded file full path  
  22.         dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);  
  23.   
  24.         // the optional (missing) parameter to API  
  25.         dynamic NA = System.Type.Missing;  
  26.   
  27.         //open Word file document   
  28.  objdoc = Objword.Documents.Open  
  29.                (ref FilePath, ref NA, ref NA, ref NA, ref NA,  
  30.                 ref NA, ref NA, ref NA, ref NA,  
  31.                 ref NA, ref NA, ref NA, ref NA,  
  32.                 ref NA, ref NA, ref NA  
  33.                  
  34.                 );  
  35.                 
  36.   
  37.        //creating the object of string builder class  
  38.         StringBuilder sb = new StringBuilder();  
  39.   
  40.         for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)  
  41.         {  
  42.             string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();  
  43.   
  44.             if (Filedata != string.Empty)  
  45.             {  
  46.                 //Append word files data to stringbuilder  
  47.                 sb.AppendLine(Filedata);  
  48.             }  
  49.                 
  50.         }  
  51.   
  52.         //closing document object   
  53.         ((_Document)objdoc).Close();  
  54.   
  55.         //Quit application object to end process  
  56.         ((_Application)Objword).Quit();  
  57.   
  58.         //assign stringbuilder object to show text in textbox  
  59.         TextBox1.Text =Convert.ToString(sb);  
  60.     }  
  61.   
  62.     protected void Button1_Click(object sender, EventArgs e)  
  63.     {  
  64.         TextBox1.Text =string.Empty;  
  65.     }  

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.
  • For more details and explanation, download the Uploaded Zip file.
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.