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


By J S
Printer Friendly Version
  

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




button
 
Article: Read a word document (.Doc , .rtf , .txt ) from ASP.Net and C#
J S posted at 08-Sep-06 06:55

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 ahas 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);
}


 
Error Msg An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in file_Management.exe
soma sundaram replied to J S at 11-Jan-07 06:49

I've to read the .doc files and displying contents in Richtextbox.. 

I've used the .doc file and using read purpose only ...Here i wrote the code

But I getting Error msg :

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in file_Management.exe"

 

Dim wordApp As Word.ApplicationClass = New ApplicationClass

Dim file As Object = myFileName

Dim Nothingobj As Object = System.Reflection.Missing.Value

doc = wordApp.Documents.Open(file, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj, Nothingobj)

doc.ActiveWindow.Selection.WholeStory()

doc.ActiveWindow.Selection.Copy()

Dim data As IDataObject = Clipboard.GetDataObject()

RichTextBox1.Text = data.GetData(DataFormats.Text).ToString()

doc.Close()

 

 

 


 
Working Perfect but With out images
Soumya Panda replied to J S at 16-Mar-08 03:59
Dear Sir,
               It is a very nice post by you. By using the above code , i can retrieve the word contents in asp.net , but one thing is... it is not retrieving the images that are present in the word documents.Any help regardung this would be greatly appriciated...

Thanking you.

 
Working Perfect but With out images
Soumya Panda replied to J S at 16-Mar-08 04:01
Dear Sir,
               It is a very nice post by you. By using the above code , i can retrieve the word contents in asp.net , but one thing is... it is not retrieving the images that are present in the word documents.Any help regardung this would be greatly appriciated...

Thanking you.
Email--  me.srpanda@gmail.com

 
Thanks.
s v replied to soma sundaram at 14-May-08 01:20
Thanks.. its working fine. :)

 
Hi i got the error while running this the above mentioned code
Harish Kumar replied to J S at 25-Jun-08 02:38
: CS1501: No overload for method 'Open' takes '12' arguments


this was the errro, how to resolve it,
please let me know the possible ways to resolve it as soon as possible.

thanks in advance,

 
Not able to read the Images from the WORD document
SudharshanRaj Talari replied to Soumya Panda at 22-Jul-08 08:30
Hi All,

I am facing same problem. using above code, iam able to read the content of the document but images are not reading from the document.

can any one have the solution for this, if so, please help me.
it is very urgent for me


Thanks in advance,

Sudharshan