C#.NET : How to convert PDF document to byte array[] and byte array to PDF document
Converting a pdf document will be very helpful for you when you need totransfer PDF document over different remote applications. Use following method for it. Don't forget add reference to System.IO;
private byte[] StreamPDFToByte()
{
string filename = "C:\\sample.pdf";
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length
byte[] byteData = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(byteData, 0, System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
return byteData; //return the byte data
}
On the other hand you may need to convert the byte array into a PDF file back. So use following method.
private void StreamByteToPDF(byte[] byteData)
{
string filename = "C:\\Newsample.pdf";
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
//Read block of bytes from stream into the byte array
fs.Write(byteData, 0, byteData.Length);
//Close the File Stream
fs.Close();
}
private byte[] StreamPDFToByte()
{
string filename = "C:\\sample.pdf";
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length
byte[] byteData = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(byteData, 0, System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
return byteData; //return the byte data
}
On the other hand you may need to convert the byte array into a PDF file back. So use following method.
private void StreamByteToPDF(byte[] byteData)
{
string filename = "C:\\Newsample.pdf";
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
//Read block of bytes from stream into the byte array
fs.Write(byteData, 0, byteData.Length);
//Close the File Stream
fs.Close();
}