print an asp .net page without considering IE version - c#

I have an ASP.NET site with a print option (onclick = window.print())
the problem is that when using IE 7 it gets fine on the page
but when users using IE 6 print the page they get it larger than the page is

It depends up on the default print settings set on the client machine.
This has nothing to do with ASP.NET.
Also, OnClick = Window.Print() is a JavaScript function which would be executable in the client machine.
You can use the CSS to control the way the page needs to be printed/viewed.
#media print {
BODY { font-size: 10pt }
}
#media screen {
BODY { font-size: 12pt }
}
#media screen, print {
BODY { line-height: 1.2 }
}

You cannot control the printing so you get the same result regardless of client, that's one of the "beauties" of HTML.
IE7 for instance introduced enhanced user-controlled printing options to "fix" some of the problems while printing web sites. This is still in the hands of the user though, and caters for their idea how the page in question would be best printed, and not really something the page itself or web developer can control.
If you need something for controlled print, use a suitable format instead - like PDF (generate it on the fly if needed).

Related

WPF Webbrowser - Access to Document Body on IE11

I'm developing a Desktop application which stores the GUI layer on html files as embedded resources. And I have a WPF Web Browser that manages the user interface lifecycle. I'm able to do all the job with no problems. But I've recently found out that when enabling the FEATURE_BROWSER_EMULATION to IE 11 (11000), the document body becomes inaccessible. Then, when using IE 10 (10000) everything works nicely.
The example below shows how to get the document body OffsetHeight:
dynamic document = (this.wbContent.Document as dynamic);
if ((document == null) || (document.body == null)) return 0;
return document.body.OffsetHeight;
The HTML is:
<html>
...
<body style="width: 170px; height: 240px">
...
</body>
</html>
When using IE 11 it throws an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'.
When using IE 10 it returns 240.
Have you got any idea about how to access body on IE 11?
As I did not get any answer about solving the problem, I had to trick in order to work around the bug. What is did is to put a simple JavaScript on the html file, so I call the method window.resizeTo(a, b). After this event is called, the application get back working, but I actually don't know why.

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.

Make a page printer friendly aspx page?

I'm making a pretty simple site using a masterpage and a header and left menu user control. When a user prints the page, I want it to only print the content. Is there a more efficient solution than creating a version of the page that isn't registered to the master page & user controls? Thanks!
CSS media queries with "print" selectors is one common option.
#media print {
.header
{
display:none
}
}
Check out documentation for details w3c, MSDN #media rule, MDN

HTML Print Page Fitting

I have some HTML files that need to be printed out, but they are 2 pages long. I need them to be 1 page long and I don't want to have to fiddle with the HTML code endlessly to shrink it just a tiny bit. I know each HTML file can be opened and zoom resized in any HTML reader and then printed out and the problem is solved, however there are... A LOT of files and having to manually open each file, resizing it and then printing out each one individually isn't really a good solution for me. More HTML files will be generated.
If I could create a quick little application that takes an HTML file and shrinks the file down from 2 pages to 1 page ( will always be 2 pages ) or if there is some way to programmatically do a 'print to fit page' function? I can't find anything in this regards to print functions in default C# libraries yet.
Here's a good tutorial that walks through what I was talking about in my comment: http://webdesign.about.com/cs/css/a/aa042103a.htm
Garrison Neely suggested the correct answer in my beliefs, just posting some links here:
#media print {
body { font-size: 10pt }
}
#media screen {
body { font-size: 13px }
}
#media screen, print {
body { line-height: 1.2 }
}
http://www.w3.org/TR/CSS2/media.html
http://coding.smashingmagazine.com/2011/11/24/how-to-set-up-a-print-style-sheet/

How Internet Explorer Prepare Print Preview window

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.

Categories

Resources