How to Print File in wpf? - c#

How to print a specified file in WPF using PrintDialog.PrintDocument() method?
I want to Print the html page using print Dialog.

You'll need some kind of an html renderer in order to print it. either IEExplorer (look into shdocvw.dll and mshtml.dll) or Awesomium etc...
good luck.

It is possible by using the Print() method of the WebBrowser Class in C# to do this. Just put the content to the WebBrowser Control like so:
webBrowser1.DocumentContent = openfiledialog.FileName;
Then just call the "webBrowser1.Print();" method.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.print.aspx

Refer MIcrosft.mshtml com object
mshtml.IHTMLDocument2 doc = rptPreview.Document as mshtml.IHTMLDocument2; doc.execCommand("Print", true, null);

Related

webBrowser check is jquery dialog box is active

is there any possible way to check is in webBrowser1 is any jquery dialog active?
here's how looks the dialog which i want to check
i using c# webBrowser class, this is not in a simple html webpage
Have you tried this -
if($(".ui-dialog").is(":visible"))
{
//dialog is open
}
Use this:
$("#mydialog").dialog( "isOpen" )

How do I get the equivalent of CTRL-A / CTRL-C in a WPF WebBrowser

I'm new to WPF and also C# so I'll try to be as specific as possible so you'll understand.
What am I trying to do?
I have a WPF Page with a WebBrowser control on it. I am navigating to a specific URL which displays perfectly in the control. Now, I would like to programmatically select all and copy the content of the webpage to my clipboard.
What have I tried
dynamic doc = webbrowser1.Document;
var htmlText = doc.documentElement.InnerText;
This however removes some formatting like empty tablecolumns so it will not be the same data as CTRL-A / CTRL-C
I have also tried the above with InnerHTML and that gives me the HTML code. When I then paste that into an empty notepad and save it as .html file, externally open in IE and perform the CTRL-A / CTRL-C it gives me the desired result.
Any idea how to get the EXACT same result through code?!
Use the following code:
dynamic document = browser.Document;
document.ExecCommand("SelectAll", true, null);
document.ExecCommand("Copy", false, null);

Embedded WebBrowser in Windows Form C# project

I have a form with an embedded web browser control on it. I am currently using WebBrowser and use it like so:
webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(string.Empty);
String htmlContent = GetHTML();
doc.Write(htmlContent);
This writes the HTML correctly to the web browser control BUT it never clears the existing data and it just appends, so I end up with N web pages stacked on top of each other.
Is this the best control to use? If so why is it not clearing existing data?
You need to use:
HtmlDocument doc = this.webBrowser1.Document.OpenNew(true);
now the contents of the document will be cleared before writing.
All calls to Write should be preceded
by a call to OpenNew, which will clear
the current document and all of its
variables. Your calls to Write will
create a new HTML document in its
place. To change only a specific
portion of the document, obtain the
appropriate HtmlElement and set its
InnerHtml property.
Yes, it is.
You should be able to call the Clear method if you need to clear contents.
Check this article for in-depth details and sample code:
http://www.codeproject.com/KB/miscctrl/simplebrowserformfc.aspx
Call HtmlDocument.OpenNew between pages:
OpenNew will clear the previous loaded
document, including any associated
state, such as variables. It will not
cause navigation events in WebBrowser
to be raised.

Printing Background Colors from WPF WebBrowser

Currently, I'm printing the contents of a WPF WebBrowser like so:
mshtml.IHTMLDocument2 doc = WebBrowser.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);
My HTML content has tables with background colors. Currently, when I print the content, the background colors do not print -- everything is solid white. Is there a way to tell the WebBrowser to print the background colors as well?
Also, this still causes a print dialog to pop up. Does anyone know what the command is to print the contents dialog-less?
Thanks a lot!
Assuming you're using 'SHDocVw.WebBrowser', you can use the ExecWB command. To print without the dialog, use the OLECMDEXECOPT_PROMPTUSER (1) constant. You can also pass an IE print template (just an HTML file) for more control over how the page is displayed.
It's something like this (taken from this MSDN question)
browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
"print_template.html", ref nullObject);
As for the background, it appears to be one of the options you can specify in the print template's LayoutRect. All print dialog settings are stored in the registry, but a print template is preferable because it won't change system-wide settings.

Read HTML code from iframe using Webbrowser C#

How to read IFRAME html code using WebBrowser?
I have site with iframe, and after few clicks new URL opens inside this IFRAME with some portion of HTML CODE. Is there a possiblity to read this?. When I am trying to Navigate() to this URL, I am redirected to main page of this site (it is not possible to open this link twice).
Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0].Url;
Maybe there is something similar to:
Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0]. ... DOCUMENTTEXT;
Try:
string content = webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText;
You can also acquire various items via mshtml types:
Set a reference to the "Microsoft HTML Object Library" under COM references.
Set your using statement:
using mshtml;
Then tap into the mshtml API to snatch the source:
HTMLFrameBase frame = yourWebBrowserControl.Document.GetElementById( "yourFrameId" ).DomElement as HTMLFrameBase;
If "frame" isn't null after that line, it has a lot of items hanging off it for your use.
try:
string content = webBrowser1.Document.Window.Frames[0].Document.Body.InnerText
A WebBrowser Control window can contain more that one iframe and .net supports frame collection so why not use something like this:
// Setup a string variable...
string html = string.Empty;
// webBrowser1.Document.Window.Frames gets a collection of iframes contained in the current document...
// HTMLWindow is the iterator for the Collection...
foreach (HtmlWindow frame in webBrowser1.Document.Window.Frames)
{
html += frame.Document.Body.OuterHtml;
}
This way, maybe with a little adjustment you can get all you need from the iframe containers you need.

Categories