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