cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.87k stars 2.92k forks source link

Wpf/win not rendering html string #734

Closed jitsingh89 closed 9 years ago

jitsingh89 commented 9 years ago

hi I am using chromium web browser control to render html/xhtml/xml file to get height and width of this file. I am able to render html file from html page url like(www.google.com).

//browser = new ChromiumWebBrowser("www.google.com") // { // Dock = DockStyle.Fill, // }; but I have to render pages after reading the file because It's epub file.

StreamReader sr = new StreamReader("C:/Users/deependra.singh/Documents/Desktop/ePub2_Sample05_Biology_SampleChapter_EN/OPS/ch01.xhtml"); string html = sr.ReadToEnd();

browser = new ChromiumWebBrowser(html ) { Dock = DockStyle.Fill, }; I want to get height and width of renders data in cef browser. I need height/width on after complete rendering so please bind the complete method also.

thanks

sylvain-hamel commented 9 years ago

have you tried browser.LoadHtml(html, "my page"); ?

amaitland commented 9 years ago

https://github.com/cefsharp/CefSharp/blob/master/CefSharp/IWebBrowser.cs#L67

jitsingh89 commented 9 years ago

thanks sylvain-hamel , I have try browser.LoadHtml(html, "my page").

But I don't have page url .I have only stream. So How can I render the stream or html string in cefbrowser.

sylvain-hamel commented 9 years ago

the 1st param is the content as a string, try LoadHtml("<h1>hello</h1>", "my page")

jitsingh89 commented 9 years ago

hi ,

this is pages where i am rendering html string in web browser control

MainView.xaml

namespace CefSharp.MinimalExample.Wpf.Views { public partial class MainView : UserControl { private ChromiumWebBrowser chBrowser; public MainView() { InitializeComponent(); // DataContext = new MainViewModel(); try { chBrowser = new ChromiumWebBrowser(); chBrowser.Height = 300; chBrowser.Width = 640; StreamReader sr = new StreamReader("C:/Users/deependra.singh/Documents/Desktop/ePub2_Sample05_Biology_SampleChapter_EN/OPS/ch01.xhtml"); string html = sr.ReadToEnd();

I am able to render if i am using url chBrowser.LoadHtml(html, "file:///C:/Users/deependra.singh/Documents/Desktop/ePub2_Sample05_Biology_SampleChapter_EN/OPS/ch01.xhtml");

but I don't have url I have only html string

chBrowser.LoadHtml(html, "mypages");

not rendering this code.

            grdLoadContent.Children.Add(chBrowser);
            chBrowser.FrameLoadEnd += BrowserFrameLoadEnd;
        }
        catch (Exception ex)
        {

        }
    }

    private void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
    {

    }
}

}

MainView.xaml

<UserControl x:Class="CefSharp.MinimalExample.Wpf.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="300">

<Grid x:Name="grdLoadContent" Height="300" Width="640">

</Grid>

amaitland commented 9 years ago

Solve one problem at a time. https://github.com/cefsharp/CefSharp/issues/703#issuecomment-67818940

jitsingh89 commented 9 years ago

hi amaitland , I have only one problem.i am describing my code . I need to get height width of the rendered html. .So please help me

amaitland commented 9 years ago

I am able to render if i am using url chBrowser.LoadHtml(html, "file:///C:/Users/deependra.singh/Documents/Desktop/ePub2_Sample05_Biology_SampleChapter_EN/OPS/ch01.xhtml");

but I don't have url I have only html string

chBrowser.LoadHtml(html, "mypages");

not rendering this code.

Your last post quite clearly says your having problems with loading html, I'm providing clarification on the LoadHtml method. It needs a valid url starting with http://, it doesn't have to exist, it can even be http://www.google.com.

For your second problem, you have to execute some javascript to get width/height. For how to execute javascript read the FAQ.

Before asking questions here please take the time to research

In general please don't past large chunks of code in comments, a Gist would be preferable.

jitsingh89 commented 9 years ago

hi amaitland , I am able to render html in browser control. But I am not getting height ,width,scroll-able height and scroll-able width of render html page. please tell me how to get after complete loading of page.It;s big html file.please share me solution

thanks

jankurianski commented 9 years ago

Use JavaScript to get the width and height: http://stackoverflow.com/a/1147768/450141

Use EvaluateScriptAsync to execute the JavaScript and retrieve the values.

Here is an example of how to retrieve the values: https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Test/BrowserTest.cs#L21-L25

jitsingh89 commented 9 years ago

thanks jankurianski this is my code please update this if I am doing any wrong. string JsInjectionBlock = ""; public MainView() { InitializeComponent(); DataContext = new MainViewModel();

        string html = "";
        try
        {
            //ePub2_Sample03a_USgovernment_SE_Gr9-12_EN_U1/OPS/9780547451381_c01.html
            //ePub2_Sample05_Biology_SampleChapter_EN/OPS/ch01.xhtml
            StreamReader sr = new StreamReader("D:/Jitendra/Epubs/ePub2_Sample05_Biology_SampleChapter_EN/OPS/ch01.xhtml");
            html = sr.ReadToEnd();
            html = html.Replace("</head>", JsInjectionBlock);
            chBrowser.LoadHtml(html, "http://test/html");
            chBrowser.FrameLoadEnd += BrowserFrameLoadEnd;
            // chBrowser.ExecuteScriptAsync(JsInjectionBlock);

            // var task = chBrowser.EvaluateScriptAsync(JsInjectionBlock);
            //.Wait();
            //Assert.Equal(result, task.Result);

        }
        catch (Exception ex)
        {
        }
    }

    private void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
    {

        ChromiumWebBrowser ch = (ChromiumWebBrowser)sender;
        var task = ch.EvaluateScriptAsync(JsInjectionBlock);

//hi I want to get height width here.could you please correct me task.Wait(); }

jankurianski commented 9 years ago

Hi, the formatting looks really bad and I can't read the code properly. Can you paste it here instead and give me a link?

https://gist.github.com/

jankurianski commented 9 years ago

Here is a tip on how to program: try something simple and make it work. Keep adding things and make it more complicated until you get what you want:

Step 1. Use JavaScript to calculate "1 + 1". Read the value in C# and print it to the console. Step 2. Use the Stackoverflow answer I gave you above. Replace the JavaScript to get the height of the document. It is still just a number - you should see it in your console. Step 3. Google to find how to get the width of a HTML document in JavaScript. Modify your JavaScript to return both height and width in an array. Read the array in C#. Print out both numbers.

jitsingh89 commented 9 years ago

hi,please see this link for my problem

https://gist.github.com/jitsingh89/ffc9332cba3ee65b5cb5

amaitland commented 9 years ago

@jitsingh89 Have you tried the steps that @jankurianski graciously provided above? At least show us that you've attempted to solve your own problem. I'm content that adequate background information has been provided.

jitsingh89 commented 9 years ago

hi amaitland , please check this which i have try.

https://gist.github.com/jitsingh89/ffc9332cba3ee65b5cb5

amaitland commented 9 years ago

https://github.com/cefsharp/CefSharp/blob/master/CefSharp/IWebBrowser.cs#L103

amaitland commented 9 years ago

You've injected your function, now call it.....

amaitland commented 9 years ago

Step 1. Use JavaScript to calculate "1 + 1". Read the value in C# and print it to the console.

Have you tried Step 1 yet?

jitsingh89 commented 9 years ago

thank amaitland , I am new in this technology how could i get width when i inject the js. could please share the code.

amaitland commented 9 years ago

https://github.com/cefsharp/CefSharp/blob/master/CefSharp/IWebBrowser.cs#L97

I really don't think your taking the time to look into this adequately......read the above description.

Please stop asking for an answer and attempt to figure it out.

jitsingh89 commented 9 years ago

hi I have try this code in FrameLoadEnd event but I am not getting width.

ChromiumWebBrowser ch = (ChromiumWebBrowser)sender; //hi I am trying to get here height and width var task = ch.EvaluateScriptAsync(JsInjectionBlock);

amaitland commented 9 years ago

https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#2-how-do-you-call-javascript-method-that-returns-a-result