Read any document (like .doc, .rtf , txt ) in ASP.Net , C#

Here is the code that helps to read any document (like .doc, .rtf , txt )from specified location.

Here is the code that helps  to read any  document (like .doc, .rtf , txt )
from 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 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 9.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 .

Then here is the code u need to add in your summit button: :

protected void Submit1_ServerClick(object sender, EventArgs e)

{       

Word.ApplicationClass wordApp = new Word.ApplicationClass();

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

string filePath = inputbox.Value;

object file = filePath;

object nullobj = System.Reflection.Missing.Value;

// here on Document.Open there should be 9 arg.

Word.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); 

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

Word.Document doc1 = wordApp.ActiveDocument;

string m_Content = doc1.Content.Text;

// the content is stored into the textbox.

m_Textbox.Text = m_Content;

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

}


Hope this helps,


Cheers,

Jay

By J S   Popularity  (44442 Views)