开发者

How do I get the C# Winforms Webbrowser control to show PDF content embedded in the html file as binary content?

开发者 https://www.devze.com 2022-12-26 08:18 出处:网络
I am using a Winforms webbrowser control to display HTML content stored in my SQL DB table. However,开发者_运维百科 some of my contents are PDFs which I intend to store as binary data in the SQL DB an

I am using a Winforms webbrowser control to display HTML content stored in my SQL DB table. However,开发者_运维百科 some of my contents are PDFs which I intend to store as binary data in the SQL DB and feed to the Webbrowser control. I wish to avoid the hassle of storing the binary content in a temporary file on the client machine and then create html container code to reference this temporary PDF file. Is there a way inwhich I can embed the PDF binary content into the html directly and show this html on the webbrowser control directly?

I found these previous threads referring to a COM 'hack'. Is there a simpler, easier way? Thanks! How do I get a C# WebBrowser control to show jpeg files (raw)?

Thanks, uniball


I wouldn't say implementing an asynchronus pluggable protocol is a hack. It is the standard approach on Windows to implement a new protocol that IE will be able to use. The registering of the protocol handler does require fiddly setup though.

But the simplest/easiest way is to actually use a temporary file, and simply link to it from your html. It's not a hassle providing you take care to use the relevant .NET functions to ensure your code doesn't make assumptions about folder/file paths etc.

  1. Use GetTempPath() to get the temp folder.
  2. Create a pdf file in the temp folder with a unique name using Guid.NewGuid()
  3. Write your binary PDF data to the file.
  4. Update the html elements href or src attribute to point the full path of the temp file.
  5. Load the html into the WebBrowser control.
  6. Delete the temporary pdf file when no longer needed.

Another possible approach would be to Base64 encode your binary data and store it inside a DIV/Input element. Next add an ActiveX control or some other client side broswer component to your html page that knows how to look for the Base64 data and can turn it back into a binary stream (and display as a PDF). I'm not aware of a 3d party control that does this however.

0

精彩评论

暂无评论...
验证码 换一张
取 消