Hi,
I am using following code to extrcat Text from .doc file.
Code::
FileStream fileStream = new FileStream("F:\\Resume_Rajib_Ghosal.doc", FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader srd = new StreamReader(fileStream);
while (srd.Read() > 0)
{
string text = srd.ReadToEnd();
}
srd.Close();
But aftering extracting when i search kewords as xml,hidden,control,form,html as so on.., its not working properly. I mean to say in original file if i search xml kewords then they have no text. But in text if i search xml kewords then they have multiple values. And also it increment to content Length of .doc file and it contain invalid characters as ? ? 8??i ?i ?BN?? .
Give me some better resolution.
Thanks in advance.
Pankaj
Loading

Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 16, 2010, 4:15 AM EDT
add reference References->Add Reference to microsoft 9.0 com object
and you can write the code to read Word file like this:
Word.Application app = new Word.ApplicationClass(); object nullobj = System.Reflection.Missing.Value; object file = @"C:\Suneetha\Ques.doc"; Word.Document doc = app.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); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); string text = data.GetData(DataFormats.Text).ToString(); Console.WriteLine(text); doc.Close(ref nullobj, ref nullobj, ref nullobj); app.Quit(ref nullobj, ref nullobj, ref nullobj);Please mark as answer if it helps.
Ivan Vasilyev10 reputationPosted Jun 13, 2014, 2:16 AM EDT
You can read more about DOC binary file format on MSDN.
Now one way you can try to extract the text (as mentioned by Amit) is to call word automation functions with C# code, but note that this is not recommended on server side applications.
Another way you can try is to call functions of this word dll with C# code, for example like this:
DocumentModel doc = DocumentModel.Load("F:\\Resume_Rajib_Ghosal.doc");
string text = doc.Content.ToString();