How do I get a C # web browser control to display PDF content embedded in an html file as binary content?

I am using the Winforms Webbrowser control to display HTML content stored in my SQL DB table. However, some of my content are PDFs that I intend to store in binary data in a SQL database and load them into the Webbrowser control. I want to avoid the hassle of storing binary content in a temporary file on the client machine and then generate html container code to link to that temporary PDF file. Is there a way that I can directly embed PDF binary content in html and display that html in the webbrowser control directly?

I found these previous threads related to COM hacking. Is there an easier and easier way? Thank you! How to get C # WebBrowser control to display jpeg files (raw)?

Thanks, Uniball

+2


source to share


1 answer


I would not say that implementing an asynchronous pluggable protocol is a hack. The standard Windows approach is to implement a new protocol that IE can use. However, registering a protocol handler requires inconvenient configuration.

But the easiest / easiest way is to actually use a temporary file and just reference it from your html. It's not a problem when you take the trouble to use the appropriate .NET features to ensure that your code doesn't make assumptions about folders / files, etc.



  • Use GetTempPath () to get the temp folder.
  • Create pdf file in temp folder with unique name using Guid.NewGuid ()
  • Write your binary PDF data to a file.
  • Update the htf or src attributes of the html elements to indicate the full path of the temporary file.
  • Load the html into a WebBrowser control.
  • Delete the temporary PDF file if you no longer need it.

Another possible approach is to Base64 encode your binary data and store it inside a DIV / Input element. Then add an ActiveX control or some other client side broswer component to the html page that knows how to search for Base64 data and can turn it back into a binary stream (and display as PDF). I do not know how to do that.

+2


source







All Articles