Often there is a need to read word file contents, so by considering that requirement I decided to write the article. Let's learn step-by-step how to read a word file and display its content in a Textbox.
The most common scenarios 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"
- "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.
I hope this article is useful for all readers, if you have a suggestion then please contact me.