2

I am having a problem in my app where it reads a PDF from disk, and then has to write it back to a different location later.

The emitted file is not a valid PDF anymore.

In very simplified form, I have tried reading/writing it using

var bytes = File.ReadAllBytes(@"c:\myfile.pdf");
File.WriteAllBytes(@"c:\output.pdf", bytes);

and

var input = new StreamReader(@"c:\myfile.pdf").ReadToEnd();
File.WriteAllText("c:\output.pdf", input);

... and about 100 permutations of the above with various encodings being specified. None of the output files were valid PDFs.

Can someone please lend a hand? Many thanks!!

TimH
  • 1,012
  • 1
  • 14
  • 24

2 Answers2

3

In C#/.Net 4.0:

using (var i = new FileStream(@"input.pdf", FileMode.Open, FileAccess.Read))
   using (var o = File.Create(@"output.pdf"))
      i.CopyTo(o);

If you insist on having the byte[] first:

using (var i = new FileStream(@"input.pdf", FileMode.Open, FileAccess.Read))
   using (var ms = new MemoryStream())
   {
        i.CopyTo(ms);
        byte[] rawdata = ms.GetBuffer();

        using (var o = File.Create(@"output.pdf"))
           ms.CopyTo(o);
   }

The memory stream may need to be ms.Seek(0, SeekOrigin.Origin) or something like that before the second CopyTo. look it up, or try it out

sehe
  • 374,641
  • 47
  • 450
  • 633
  • @usr Depends on the use. If the instream is going to be a network stream, this is a bit ... _impossibru_. Likely this is about what the OP is trying to do, otherwise File.Copy is clearly the winner – sehe Dec 07 '12 at 21:32
  • Ok, I agree for the case of arbitrary streams. – usr Dec 07 '12 at 21:33
  • 1
    Although I think @JayRiggs answer is technically the "correct answer", given the phrasing of the question, this is a "better" answer, as it's a lot more efficient than reading the whole file into a byte array and blasting it back out. – JerKimball Dec 07 '12 at 21:53
  • Thanks for the help! The output file is 0 bytes, however. – TimH Dec 10 '12 at 14:24
  • @TimH Did you add the `Seek`? See http://msdn.microsoft.com/en-us/library/system.io.stream.seek.aspx – sehe Dec 10 '12 at 15:37
3

You're using File.WriteAllText to write your file out.

Try File.WriteAllBytes.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • This. You need to match up read/write "types" - reading as bytes and writing as text won't work. – JerKimball Dec 07 '12 at 21:51
  • I believe that each of my code samples does that. First sample uses byte[], second sample uses string. Am I missing something? – TimH Dec 10 '12 at 14:20