2

I have some binary data in memory (which represents a PDF file). What I’d like to be able to do is to point the standard .NET browser control directly to this data. For example:

myLib mylib = new myLib();
object vbyt = mylib.GetPDFDocument();
webBrowser1.data = vbyt;

Is this possible, or do I need to save the PDF to a file and read from there?

EDIT:

I believe that the following should do what I need, but it just spits out garbage - can anyone tell me why / a way to get this to work?

byte[] vbyt = (byte[])mylib.GetPDFDocument();

MemoryStream ms = new MemoryStream();

ms.Write(vbyt, 0, vbyt.Length);
ms.Position = 0;

//conetent type = "application/pdf";            
webBrowser1.DocumentStream = ms; 
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269

2 Answers2

2

I believe there's a bit more too it than that, this page should help.

There is a duplicate question here.

Community
  • 1
  • 1
Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • FYI it is much easier to save it to a file and just browser.Navigate() to the PDF on disk. – Josh M. Mar 10 '11 at 12:46
  • Just found that answer and was reading up on it, you beat me! Yes saving the page is easier, but the OP seems to want to avoid doing that. – mdm Mar 10 '11 at 12:49
  • Agreed - that's why I posted a link explaining how to do it. But it is definitely much more involved than simply Navigating to the PDF. – Josh M. Mar 10 '11 at 12:56
  • Sorry, I think I must have misread 'FYI' as 'IMO' there... Ignore me! – mdm Mar 10 '11 at 12:57
1

The reason why setting DocumentStream produces garbage is discussed in this forum post: Can I pass binary data of a file to WebBrowser control?

A possible solution is to save the file locally and serve it via html file: stream a pdf to a .net webbrowser object on a winform

Community
  • 1
  • 1
Giorgi
  • 30,270
  • 13
  • 89
  • 125