Jerry Nixon @Work: View a document (as byte[]) in some viewer

Jerry Nixon on Windows

Friday, March 14, 2008

View a document (as byte[]) in some viewer

Here's the situation: You have a byte[] of a PDF and you want to show the user. How? Easy, just move it to a file and start a process against it. (note: this is a WinForms solution) Here, I'll show you what I mean:

static public void ShowDocument(byte[] letter, string extention)
{
if (letter == null)
throw new ArgumentNullException("letter");
if (string.IsNullOrEmpty(extention))
throw new ArgumentNullException("extention");

string _FileName;
_FileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), extention);

string _FolderName;
_FolderName = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);

string _FullPath;
_FullPath = System.IO.Path.Combine(_FolderName, _FileName);

if (string.IsNullOrEmpty(_FullPath))
throw new NullReferenceException("string.IsNullOrEmpty(_FullPath)");

using (System.IO.FileStream _FileStream
= new System.IO.FileStream(_FullPath, System.IO.FileMode.OpenOrCreate))
{
_FileStream.Write(letter, 0, letter.Length);
_FileStream.Close();
}

System.Diagnostics.Process.Start(_FullPath);
}