Reputation: 1032
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!!
Upvotes: 2
Views: 8757
Reputation: 53593
You're using File.WriteAllText
to write your file out.
Try File.WriteAllBytes
.
Upvotes: 3
Reputation: 393064
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
Upvotes: 3