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.
This type of requirement can be the result of various scenarios, the most common are as follows:
- To upload a resume and show the contents in a TextBox as summary.
- Save that resume contents into the database so later on it is useful for CV or resume parsing.
- In a blog or community website to upload file contents directly into the editor so it becomes faster to edit contents and save it.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "ReadWordFilesInFillTextBox" or another as you wish and specify the location.
- Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.
- Drag and drop two Buttons, a Fileuploader and TextBox control onto the <form> section of the Default.aspx page
- Set TextBox text mode to multiline.
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:
Now double-click on the upload button and write the following code:
Now double-click on the reset button and write the following code:
The entire code of the default.aspx.cs will look as in the following:
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.
- Do a proper validation such as if it has a file or not of the File Upload control when implementing.
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.