Printing content of WebBrowser Control in C# - c#

I want to print data present in Webbrowser Control. My data is loaded from datatable which contains HTML formatted text. When I tried to print it with RichTextBox it was not able to recognize HTML controls and was not able to render.
But when I tried to print using webbrowser control, noting gets printed. How to achieve this.
webBrowser1.DocumentText = dt.Rows[0].Field<string>("WAIVER_TERMS");
webBrowser1.Print()

Did you try IWebBrowser2.ExecWB(OLECMDID_PRINT)? It should work, I've used succesully many time.

Related

Inserting a c# string in a webbrowser component, how can i do this?

I'm creating a program that will generate a HTML page with information pulled from a database, some HTML is loaded into a webbrowser component and this webbrowser gets printed.
So in c#:
- There's a webbrowser component
- With some HTML code added into it
I'm trying to insert c# strings, textpulled from a database on specific places in the HTML that gets displayed in the browser component, but i have no clue how. I know in PHP for example, i am able to do between HTML text for example. Does something similar exist for C#? I'm working in winforms.
I'm looking for something like:
<html>
...
...
<h1>$String1</h1>
...
...
</html>

webkit-sharp scroll to an HTML anchor tag

I have a webkit-sharp WebView which I am using to display HTMLvia the
LoadString method.
The webview is placed in a ScrolledWindow the ScrolledWindow is placed
in a Gtk Window.
I want to be able to tell the WebView to scroll to a specific part
of the HTML. Normally one would do this using an anchor.
I have defined an anchor and some JavaScript to jump to that anchor, I
call the JavaScript via the ExecuteScript method. This does nothing at
all.
I have also tried adding a button to the HTML that calls the
JavaScript. This also does nothing.
Is there something I can do to make this work, to make it so I can
scroll to a known location in the page?
Update: I can make this work by saving the HTML to a file and then loading from there using a URL which tells it to scroll. However I would like to avoid doing that because of the performance hit of writing the page to disk before displaying it.
How are you adding to ScrolledWindow? I am not GTK expert, but have discovered that it works if you call "add" method and does not work if you call add_with_viewport
Also you may not need to execute the javascript. It looks like you control the html page and can easily access the dom element and call click method on the element.Following sample python code looks for anchor with id "one" and scrolls to desired location without calling javascript.
from gi.repository import WebKit
from gi.repository import Gtk
from gi.repository import GLib, GObject
class WebkitApp:
def exit(self, arg, a1):
Gtk.main_quit()
def onLoad(self, view, frame):
doc = view .get_dom_document()
a = doc.get_element_by_id("one")
a.click()
def __init__(self):
win = Gtk.Window()
self.view = WebKit.WebView()
# Signal Connections
self.view.connect("onload-event", self.onLoad)
file = open("/tmp/epl-v10.html")
html = file.read()
self.view.load_string(html, "text/html", "UTF-8", "file:///tmp")
sw = Gtk.ScrolledWindow()
# sw.add_with_viewport(self.view)
sw.add(self.view);
win.add(sw)
win.maximize()
win.connect("delete-event", self.exit)
win.show_all()
app = WebkitApp()
Gtk.main()
The JavaScript to jump to that anchor, doesn't work while the page is still loading, By waiting for the page load to finish (there is an event for it) then running the script I made it work.
Given an element:
<a id='myElement'>I want to scroll to this line</a>
This can be scrolled to with this JavaScript:
document.getElementById('myElement').scrollIntoView();
Which can be executed in a WebView with the ExecuteScript method, e.g.:
_webView.ExecuteScript("document.getElementById('myElement').scrollIntoView();");

TextArea in WebBrowser

I have a WebBrowser object that I use to navigate to a forum site. Now, I am filling a form that has a textarea I need to interact with.
When I finished writing the topic and submit the value of the textarea, the value didn't set to what I wanted.
WebBrowser browser = new WebBrowser();
browser.Navigate("www.example.com");
browser.Document.All["textarea"].InnerText = "MyText";
browser.Document.All["SubmitButton"].InvokeMember("Click");
Now that didn't work, and one more important thing.. When I use the HTML visualizer to see the page while debugging, The area of the textarea in the visualizer is not displayed it says "This program cannot display the webpage" but the rest of the page is displayed normally.
I heard something about textarea that it is an HTML5 element and because the WebBrowser is an activeX that use the Internet Explorer so maybe I have a version of Internet Explorer that not support textarea elements? I have IE 10.
Can someone please tell how to set the value of a textarea properly.

How can I get all the visual attributes of a text in a HtmlElement inside a WebBrowser?

I just need exactly those visual attributes (font, size, color, etc.) that the WebBrowser itself use to render the text. They have to be somewhere otherwise the WebBrowser wouldn't know how to show the text.
Take a look at HtmlElement.Style - it's a list of CSS styles. You can get HtmlElements from the HtmlDocument (e.g. document.Links gives you all the links). Also, you should only get the HtmlDocument once the browser.DocumentCompleted event fires.

how to print the content of a label or panel in asp.net?

I have a label control in a page in asp.net 2.0 and a button Print.
Clicking on print button I need to print the content of the label and I need same for a panel also.
Is it possible to implement this?
If yes then how to implement that?
Please help.
What about Window.Print() method? Because execcommand method will not work other browsers that IE. Use CSS media option to control the print area.
You would need to add some client side javascript to your Print button to execute the brower's print command. The javascript below could be used to print the whole document page and would be a good place to start. Note: It isn't possible to do this without displaying the print dialog unless you use a third party component.
// Print Page
window.print();
If you wanted just to print certain sections of your page you can achieve this two ways. Firstly, you could render the content to be printed into a hidden iframe and then print just that frame. You would do this using the same code as above only from within the frame itself.
Secondly, you could use a media style print style sheet, a CSS that applies only when printing. Inside this sheet you would set the styles you wanted to print as normal and the styles you didn't want to print to "display:none". The link below contains more information on print stylesheets.
http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml
One more approach could be to open new window and populate the contents of div you wanted to print and have print link/button on that page.
Ex:
var win = window.open(...)
win.document.body.appendChild(document.getElementById('divToPrintId'))
This is how the code will look like this approach is used to print the content/part of the page.
Use below code in button onclick event
ClientScript.RegisterStartupScript(this.GetType(), "PrintOperation", "PrintGridData()", true);
Above link will call function named PrintGridData() which is written in head section as below
<script type="text/javascript">
function PrintGridData()
{
var prtGrid = document.getElementById('<%=GridView.ClientID %>');
prtGrid.border = 0;
var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1, status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML);
prtwin.document.close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
</script>
in this script it will only print div name GridView and other part will not be printed

Categories

Resources