How To Read word document in C#

Here is the code that helps to read any document (like .doc, .rtf , txt ) from the specified location. This is a web-based application and this code is written in C# as code behind in ASP.Net 2.0, where the word document is hard to upload from the client-side. Here is the code that uploads the document file and stores it into a string and from that I have placed that string into a textbox. The First Step is that we need to add a COM reference (that’s how we need to define the word application) to the project by right-clicking in the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 11.0 Object Library. Click Select
and OK.

Now u need to add the line

 <identity  impersonate="true"/>

Like:

<system.web>

<identity  impersonate="true"/>

in your web.config .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadWordDocument.aspx.cs"
  Inherits="ReadWordDocument" %>

<!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>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:FileUpload ID="FileUpload1" runat="server" /><br />
          <asp:Button ID="btnRead" runat="server" Text="Read Word Document" OnClick="btnRead_Click" /><br />
          <asp:TextBox ID="TextBox1" runat="server" Height="373px" TextMode="MultiLine" Width="500px"></asp:TextBox>
      </div>
  </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Word;

public partial class ReadWordDocument : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnRead_Click(object sender, EventArgs e)
  {

      ApplicationClass wordApp = new ApplicationClass();

      // Input box is used to get the path of the file which has to be 
      // uploaded into textbox.

      string filePath = FileUpload1.PostedFile.FileName;

      object file = filePath;

      object nullobj = System.Reflection.Missing.Value;

      // here on Document.Open there should be 9 arg.
   
      Document doc = wordApp.Documents.Open(ref file,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj);

      // Here the word content is copeied into a string which helps to
      // store it into  textbox.

      Document doc1 = wordApp.ActiveDocument;

      string m_Content = doc1.Content.Text;

      // the content is stored into the textbox.

      TextBox1.Text = m_Content;

      doc.Close(ref nullobj, ref nullobj, ref nullobj);
  }
}

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم