How to read a Doc or PDF file

  • Thread starter Thread starter Shayer
  • Start date Start date
S

Shayer

Hello All

I am trying to read a MSDoc file and PDF file using Streamreader and then
display the content into a richtext box. But cant do that.

Anyone can pls help me out

Thanks

Shayer
 
Docs and pdfs have special layouts. You'll need specific readers for that.
You can open your Doc using word then selecting all text and copy it to your
richtextbox. For pdfs I never tried it but I guess there's something
similar.

Yves
 
Thanks for ur advice

But how can i open the doc and read the content and display in the
richtextbox

How can u do that

ANy sample code will be helpful

Thanks

regards
Shayer
 
Shayer said:
Thanks for ur advice

But how can i open the doc and read the content and display in the
richtextbox

How can u do that

ANy sample code will be helpful

Thanks

regards
Shayer

First of all add a reference to the word library. It should be located under
'COM' under 'Microsoft Word x.xx Object Library' (with x.xx your version).

Then the following code should do the trick :

/*************************************************/

object filename = @"c:\test.doc";
object save = false;
object oMissing = System.Reflection.Missing.Value;

Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;

// Open word document
// different versions of word may have more or less oMissings
oDoc = oWord.Documents.Open(ref filename, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

// Select everything
oDoc.Select();

// Copy everything
oWord.Selection.Copy();

// Clean up the RTB
richTextBox1.Text = "";
// Paste the entire text with format
richTextBox1.Paste();

// Close word
oDoc.Close(ref save, ref oMissing, ref oMissing);
oWord.Quit(ref save, ref oMissing, ref oMissing);

/*************************************************/

HTH

Yves
 
phoenix said:
First of all add a reference to the word library. It should be located under
'COM' under 'Microsoft Word x.xx Object Library' (with x.xx your version).

Then the following code should do the trick :

/*************************************************/

object filename = @"c:\test.doc";
object save = false;
object oMissing = System.Reflection.Missing.Value;

Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;

// Open word document
// different versions of word may have more or less oMissings
oDoc = oWord.Documents.Open(ref filename, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

// Select everything
oDoc.Select();

// Copy everything
oWord.Selection.Copy();

// Clean up the RTB
richTextBox1.Text = "";
// Paste the entire text with format
richTextBox1.Paste();

// Close word
oDoc.Close(ref save, ref oMissing, ref oMissing);
oWord.Quit(ref save, ref oMissing, ref oMissing);

/*************************************************/

HTH

Yves

If the document is rather large you may think about opening it the same way
as above but saving it immediatly in rtf format. You can then open it
through your richtextbox. After you made the changes you can always use the
same way to overwrite the original doc if that's what you're planning.

Yves
 
Thanks Mate

Its a perfect solution. Thanks a lot

I am still toying the idea how can i read the content off PDF

Thanks once again

regards

Shayer
 
Back
Top