1

My component will receive a pdf file as a filestream from which I will need to create a file.

For testing purposes I am trying to read a file using the filestream object and recreate it at a different location. But the recreated file is created blank. the recreated file has the same number of pages though...

This is the code

StreamReader sr = new StreamReader(_filePath);
str = sr.ReadToEnd();
File.WriteAllText(@"C:\recreated.pdf", str);

what am I doing wrong?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user20358
  • 14,182
  • 36
  • 114
  • 186
  • possible duplicate of [How do I save a stream to a file?](http://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file) – nawfal Feb 13 '14 at 10:12

1 Answers1

5

For a start, you're using a StreamReader even though PDFs are binary data. You don't want to write text - you want to read and write binary data.

See my answer to a similar question yesterday (not exact duplicate, but similar) for more details and code.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194