Skip to content Skip to sidebar Skip to footer

Generate Print Preview Of A Web Page In Asp.net

This is the only code i am using to implement print functi

Solution 1:

Basically there is no functionality available until now but you can achieve by creating a copy of same html file with name print.htm with the Print css and open this file on button Print Preview click as a popup page and render it, This will preview the page :

<html> 
<head> 
<title></title> 
//do it in simple way.. 
<script LANGUAGE=”JavaScript”> 
function displayMessage(printContent) { 
var inf = printContent; 
win = window.open(”print.htm”, ‘popup’, ‘toolbar = no, status = no’); 
win.document.write(inf); 
win.document.close(); // new line 
} 
</script> 
</head> 
<body> 
<div id=”printarea”>Print this stuff.</div> 
<a href=”javascript:void(0);” onclick=”displayMessage(printarea.innerHTML)”>Print Preview</a>

</body> 
</html> 

Solution 2:

I really don't see this as practical. Your web page has no knowledge of font size (zoom level), page margins, orientation or anything else you'd need to know to construct a print preview image.

In addition, it's possible for a page to reference images and other content from other sites. How will your code know what is needed here?

Fortunately, the browsers I use (IE and Chrome) provide print preview, so you'd get it for free anyway. But if you're trying to implement print preview manually, I just don't see that as a practical goal.


Solution 3:

One way is to use the mediaType print in your css. This css will only be applied when you open you page in print mode. Add following class in your css file:

@media print {
// printer friendly page css
}

Another way is by using pure javascript. there are bunch of articles on internet for this:

If you are having a common template for printer friendly pages. I would suggest you to create a seperate page altogether for print version and when user clicks on link navigate it to your new printer friendly page. I would recommed this approach as this is foolproof and you have complete control over it, rest of approaches have some pitfalls.


Post a Comment for "Generate Print Preview Of A Web Page In Asp.net"