How do I convert a pdf file to a byte[] and vice versa?
Asked
Active
Viewed 1.7e+01k times
3 Answers
160
// loading bytes from a file is very easy in C#. The built in System.IO.File.ReadAll* methods take care of making sure every byte is read properly.
// note that for Linux, you will not need the c: part
// just swap out the example folder here with your actual full file path
string pdfFilePath = "c:/pdfdocuments/myfile.pdf";
byte[] bytes = System.IO.File.ReadAllBytes(pdfFilePath);
// munge bytes with whatever pdf software you want, i.e. http://sourceforge.net/projects/itextsharp/
// bytes = MungePdfBytes(bytes); // MungePdfBytes is your custom method to change the PDF data
// ...
// make sure to cleanup after yourself
// and save back - System.IO.File.WriteAll* makes sure all bytes are written properly - this will overwrite the file, if you don't want that, change the path here to something else
System.IO.File.WriteAllBytes(pdfFilePath, bytes);

jjxtra
- 20,415
- 16
- 100
- 140
-
-
Just for reference, there is an open source C# pdf library here: http://sourceforge.net/projects/itextsharp/ – jjxtra May 06 '15 at 04:31
-
Could you explain how the path works? What if the file is in another directory? – Kellen Stuart Aug 14 '19 at 19:17
-
@KolobCanyon Paths use System.IO.Directory.GetCurrentDirectory() by default. You can always specify a full path to be safe. – jjxtra Aug 14 '19 at 19:40
-1
using (FileStream fs = new FileStream("sample.pdf", FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fs.Length];
int numBytesToRead = (int)fs.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fs.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;
}

Dragão Heremita
- 9
- 3
-4
Easiest way:
byte[] buffer;
using (Stream stream = new IO.FileStream("file.pdf"))
{
buffer = new byte[stream.Length - 1];
stream.Read(buffer, 0, buffer.Length);
}
using (Stream stream = new IO.FileStream("newFile.pdf"))
{
stream.Write(buffer, 0, buffer.Length);
}
Or something along these lines...

Paulo Santos
- 11,285
- 4
- 39
- 65
-
You forgot to take care ot the return value of the Read method. You have to loop the reading and read until you actually get all the data. – Guffa Mar 16 '10 at 11:57
-
@Guffa not quite, if you take a look I've used `stream.Length` that returns the length of the ENTIRE file stream, hence reading the file as a whole, not only as chunk of data. – Paulo Santos Mar 16 '10 at 19:47
-
2You are missing the point. Even if you request the entire stream from the Read method, it doesn't have to read the entire stream. It will read one byte or more, and return how many bytes were actually read. If you ignore the return value of the Read method, you may only get part of the file. – Guffa Mar 16 '10 at 20:06
-
@Guffa is correct: the FileStream.Read docs say: "an implementation is free to return fewer bytes than requested even if the end of the stream has not been reached". http://msdn.microsoft.com/en-us/library/system.io.filestream.read(v=vs.110).aspx – Ben M Jul 24 '14 at 23:42