C# WebBrowser won't display embedded object - c#

This may be a dumb question, but I just can't get it to work. I'm trying to embed a pdf file into a webbrower in a c# program. Simple right? I thought so, but it proved otherwise.
Here's the html code:
<object classid="" type="application/pdf" width="400" height="300" id="pdf1">
<param name="src" value="Test.pdf" />
<div style="text-align:center; color:#CCCCCC" >No Preview Available.</div>
</object>
And that's all thats in the html file. The funny thing is, this works perfectly in IE (which as I understand is what the WebBrowser uses). I've tried implementing the object tag the non-IE way, with an src attribute and whatnot, but it did the same thing. What's happening is the WebBrowser control is just displaying No Preview Available meaning that the pdf was not successfully embedded. Here's the c# code:
wbPreview.Navigate("I:/Documents/Visual Studio 2008/Projects
/PlanReferenceDatabase/test.html");
Can someone tell me why the web browser in c# cannot display the pdf, but the other browser can?

Try formatting the tag like this:
<object type="application/pdf" data="myPdfFile.pdf" class="yourPdfClass" width="550px" height="800px"/>
Include the data attribute in the tag and it seems to work just fine - just tried this in my browser control on a IE8 / IE9 machine and it displays inline.

I solved my problem. Just in case anyone else has the same one, here's what I did:
First I set the "target platform" property under the build tab in visual studio to x86 because as it turns out, 64bit IE can't render PDFs.
Second I used the embed tag instead of the object tag, because IE requires you to use "classid" and all sorts of Active X stuff you don't want when you use the object tag.

Related

WPF WebBrowser not render external html content properly

In WPF application the WebBrowser control doesn't render external html url properly.
While source assigned with external url, page get loaded.
But html hover effects & svg animations are not working.
Stuck at this point (VisualStudio 2015 environment).
Help me to step up.
Thanks in advance.
<StackPanel>
<WebBrowser Source="https://www.amcharts.com/demos/simple-pie-chart/"
OverridesDefaultStyle="False" MaxHeight="500"/>
</StackPanel>
If I understand correctly, you can not modify the HTML content of the web page.
You can use the following trick instead:
In registry:
Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION for 64-bit or 32-bit only machines.
Create a new DWORD key, and name it the name of your application e.g. "myapp.exe" and then edit the value of the key. There are many different values you can add depending on the IE version you want to emulate. I entered 11001 (as a decimal value - 0x2AF9 in HEX) which emulates IE 11 (many more values located at: http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx#browser_emulation).

WPF WebBrowser Control will not load video in a locally hosted HTML page

I have a WPF application written in C#. This application has two WPF WebBrowser controls next to each other. Each one navigates to a different locally-hosted HTML file. Each HTML file has a video element which points to an .mp4 video file stored in the same directory.
My problem is that the video content does not render in the WebBrowser controls. If I use the exact same URL and put it into Internet Explorer 11 on the same PC, the video content runs perfectly fine. I know the actual navigations of the WebBrowser controls are working because the page half-renders. I can see the background color show correctly but that is it. If I right-click inside one of the controls and view the source, I can see the correct HTML there.
Attempting to apply the recommendation in an answer to this question to Internet Explorer 11, I tried having the WebBrowser controls in my executable run like IE11 using the information in this link with no success.
I'm not sure if this is relevant, but I already have "Allow active content to run in files on My Computer" checked in Internet Options on Internet Explorer.
Here is the HTML markup for the video:
<div id="video_holder">
<video id="video" width="1200" height="900" autoplay>
<source src=".\FANCY.mp4" type="video/mp4">
</video>
</div>
And here is the code that calls the Navigate() method of the WebBrowser controls (the constructor for this Form receives the urls from another class - I'm pretty sure the problem isn't here, though, as I can see that the actual navigation seems to be working and the HTML starts to render):
public partial class TwoPaneWindow : Window
{
private string leftUrl;
private string rightUrl;
public TwoPaneWindow(string left, string right)
{
leftUrl = left;
rightUrl = right;
}
public void StartWindow()
{
InitializeComponent();
this.Show();
LeftBrowser.Navigate(leftUrl);
RightBrowser.Navigate(rightUrl);
}
}
Can anyone tell what is going on? I am hosting this on IIS installed on Windows 8.1. Thank you in advance for any help you can offer.
Finally figured this out. Even though I said I tried the answer to this question, I was actually using the FEATURE_BROWSER_EMULATION key incorrectly. I attempted to add FEATURE_BROWSER_EMULATION to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl, when in fact it needs to be modified at HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION. Once I created a new DWORD entry for my application (sharing its name) with a value of 0x2AF8, the WebBrowser controls immediately started working correctly. A newbie mistake I'm sure, but hopefully this helps someone else in the future.

Windows phone webbrowser Control is Blank?

Im trying to open a URL(Some link) in Webbrowser Control.
The link return a html page which contain Google Graph , but my Webbrowser Control is Blank and dont display any thing on it. It works fine on WebBrowserTask and on my pc so their is no problem in this link but it is blank on webBrowser Control Any Idea How i can Do this ??
public GraphPage()
{
InitializeComponent();
webBrowser1.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Browser_Navigated);
webBrowser1.Navigating += new EventHandler<NavigatingEventArgs>(Browser_Navigating);
loadPage(getBaseUrl(graphType));
}
private void loadPage(String url )
{
webBrowser1.IsScriptEnabled = true;
webBrowser1.Source = new Uri("Link");
}
As mentioned by user112553, set IsScriptEnabled true. Can be done under the XAML-code or in the code-behind with
XAML
<phone:WebBrowser x:Name="Browser" IsScriptEnabled="True" />
Code-Behind
Browser.IsScriptEnabled = true;
I encountered a similar situation, with Windows Phone 8 and a HTML page using JQuery.
IsScriptEnabled=true wasn't enough (the page didn't render properly).
I solved adding to the html page a
doctype declaration:
<!DOCTYPE html>
<html>
...
Seems like the WebBrowser component refuses to render HTML5 pages without explicit defining the document-type.
Since it's a common problem with rendering pages in IE<11 when not defining this tag, the cause of why my scripts didn't run could be many and most likely a reference to a HTML5 tag which was not handled correctly.
ref: http://msdn.microsoft.com/en-us/hh779632.aspx
Since windows phone 8.0 is based on Internet Explorer 10, it makes sense, the confusing part with debugging this behavior is that Internet Explorer on your phone renders the page perfectly. Still the WebBrowser component will not.
If this is documented in the API specifications, it should be easier to find, because I was not able to find any information that would point me to this solution at all, this would be mostly because my pages was rendered in WebViews for Android and IOS without any problems.
Thanks to Antonio Pelleriti for providing this solution.

C# Setting InnerHtml to "" for an element which is updated by server causes element to stop updating

I have a C# Windows Forms Application with a webBrowser within it. It visits a Wikia chatroom, where it then locates the element which contains the chat output. This element looks like:
<div style="" id="Chat_XXXXX" class="Chat">
<ul>
(chat text)
</ul>
</div>
The purpose of the program is to retrieve the chat text every once in a while for logging purposes. This is easily done by parsing the "InnerHtml" of the "Chat_XXXXX" element. However, I also need to clear the text from the window when I do this (for various reasons, I cannot leave the text in the window). I figured I would just erase the chat text portion of the element, as this is how it is done with a handy javascript file called "chat hacks" for Wiki chat (here). Or at least, I think that's how it does it. If you look at the function "clearWindow" in that file, you can see what it does:
NodeChatController.prototype.clearWindow = function() {
this.viewDiscussion.chatUL.html('');
this.inlineAlert(i18n['cleared']);
}
I have tried setting the InnerHtml of "Chat_XXXXX" using the following three strings (not all at the same time, of course):
HtmlDocument document = webBrowser1.Document;
document.GetElementById("Chat_XXXXX").InnerHtml = ""
document.GetElementById("Chat_XXXXX").InnerHtml = "<ul></ul>"
document.GetElementById("Chat_XXXXX").InnerHtml = "<ul><li class=\"inline-alert\"> Window cleared. </li></ul>"
However, although these clear the window (and in the case of the last one prints a message), the chat no longer updates as new messages show up. The only fix is to reload the page, which isn't an option, because reloading the page brings in a whole load of chat history (which I'm trying to avoid). I've also tried importing that javascript mentioned above into the page using:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
scriptEl.SetAttribute("type", "text/javascript");
scriptEl.SetAttribute("src", "https://db.tt/66q8UQbY");
head.AppendChild(scriptEl);
This javascript creates a button which clears the chat window. The button clears the window just fine, but again, the chat no longer updates. I know this button works correctly without stopping further incoming chat in "regular" browsers (Chrome, Firefox, Opera, etc). I've used it many times, and the script itself is quite popular in the Wikia community. I've already followed the steps here: "WPF WebBrowser Control - position:fixed Element jumps while scrolling (Windows 8)" to get the browser to act as close as it can (?) to Internet Explorer. I've already checked the Body field of the document after altering the InnerHtml to make sure that my replacements didn't alter anything important. Just for clarity, here's an example of what is contained in the (chat text) portion of my original example:
<li class="you" data-user="UserNameHere" id="entry-c812">
...avatars and junk...
<span class="message">hello</span>
</li>
I honestly have no idea what could be causing the chat element to stop updating after it has been edited (especially since it works outside of this program), so I don't know what information to include. Whatever you need, I'll provide it. Here's the javascript from Wikia which generates the chat output window: chat_js2. Look for "Chat_" to find the part which originally generates the window. I don't know where the output is updated in that file though.

How to disable the browser scrollbar in a Silverlight application?

In my Silverlight application I need to permanently disable the browser scroll bar. When I run the application the browser scroll bar is visible. So I need to disable this one.
Please let me know in which file we should do and the code for disabling the scroll bar.
u have to make your UserControle(root userControle) smaller that fit-up in to your browser that's way your browser scroll bar can disable.....
use silverlight navigation template
When using the default aspx-page to display the Silverlight application (the one generated by Visual Studio) usually no scroll bars should appear. But if that is the case that's the place where to look.
Sometimes the browser (especially the Internet Explorer) renders line breaks where there are none. So try to remove any line-breaks from the HTML-markup surrounding the <object>-element hosting the Silverlight application.
Example: Convert the following code
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
</object>
<iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>
</form>
to the following code by removing all linebreaks (I added the ... to make the code more readable here, leave the original parameters there of course)
<form ...><div><object ...></object><iframe ...></iframe></div></form>
In my case that did solve the issue.

Categories

Resources