How Internet Explorer Prepare Print Preview window - c#

I am wondering how Internet Explorer, Mozilla Firefox or any other browser generate print preview window of an web page loaded into the browser.
The preview image will have various changes such as banners and adv are removed, will have white background and black text and etc.
We would like implement similar print preview window using C# WebBrowser control and i don't want to use default browser Print preview feature such as ExecWB command or any other.
Please give us some light on this.
Thanks,
Ramanand Bhat.

You could try to alter the styles by accessing and modifying the HTMLDocument LINK elements.
HtmlDocument document = WebBrowser1.Document;
foreach (HtmlElement element in document.GetElementsByTagName("LINK"))
{
string cssMedia = element.GetAttribute("Media");
if (cssMedia == "print")
element.SetAttribute("Media", "screen"); //sets print styles to display normally
else
element.SetAttribute("Media", "hidden"); //hides normal styles
}
This will change your print-styles to display in screen view (i.e. as a normal stylesheet without having to use the print-preview window) and your screen-styles to not be shown (as they don't have a Media type of screen anymore)
This is sample code so doesn't have any error checking. It might also have some syntax errors but it should be a start to achieve your goal.

To print a screen you need to set up a call to window.print() in javascript.
Print screen
It will then use whatever css you have assigned as 'print' in the page to render the page as a preview

As far as I know, the banners, advertisements, et cetera are not removed by the browser during a print preview. CSS governs the appearance when the media is print.

Related

Switch HTML page to media print mode

Is there a way to switch an HTML page to "print" mode?
When I print, a #media print style-sheet gets applied to the page.
I want to use the page with that print style applied to export to PDF as well. Ideally, if I could get the HTML that gets sent to the printer when I use window.print() I could export it to PDF. I use a server side library and C# to export to PDF.
Any ideas on how I can do this?
Thank you!
As per W3C standars there is no distinctive print mode for web pages except that you can define different CSS rules for #screen and #print media.
If you want to render a page as it is shown in print view somewhere it is show in screen view; use #print CSS for #screen also.

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.

Print webpage not shown some images and some background color im webpage

I code for printing the webpage.I am printing the webpage using javascript and the code is like
function print()
{
window.print();
}
I call this function in onclientclick of Button.but when i see the print it doesnt show me some of images and background color.It doestnot happen with all.It shows me some of images and some of images are missing.same with background color.
This is likely Internet Explorer's own setting.
In IE8 and earlier, there's a setting under Tools.Internet Options, Advanced tab, Printing section, which controls if background images and colours are printed.
In IE9, it's now a setting in the File.Page Setup... dialog options.

how to open screen on full screen

how i can open my asp.net program on full screen (like i press F11)
Through C# code ?
thanks
Use javascript
Button1.Attributes.Add("onclick","window.open('Default.aspx','','fullscreen=yes')");
not exactly full screen but pretty similar
Have a look at this. Instead of linking, you can redirect your user from your default page with the proper javascript.
Maybe you can send a F11 key pressed command from your application once it's loaded to force your browser to go full screen?
I doubt if there is a clean way of doing this as the F11 fullscreen command is handled by your internet browser.
I think OP is talking about asp.net application so the "program" here is browser.
If it is true, you need client-side javascript to do that. something like this:
<script language="JavaScript1.2">
top.window.moveTo(0,0);
if (document.all)
{ top.window.resizeTo(screen.availWidth,screen.availHeight); }
else if (document.layers || document.getElementById) {
if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth)
{
top.window.outerHeight = top.screen.availHeight;
top.window.outerWidth = top.screen.availWidth;
}
}
</script>
document.all works for Internet Explorer while document.layers works for Other browsers.
top refers to the window that is on top of all other windows in the window object hierarchy.
top.window.moveTo(0,0) will display the top window at these coordinates ie. the upper left corner of the screen.
resizeTo(x,y) will resize the window to the size specified.
availWidth and availHeight are properties of the screen object which can detect the available screen space.
Hope this helps...

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