Generate print preview of a web page in asp.net

前端 未结 3 1034
一个人的身影
一个人的身影 2020-12-22 08:04


This is the only c

相关标签:
3条回答
  • 2020-12-22 08:18

    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.

    0 讨论(0)
  • 2020-12-22 08:27

    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> 
    
    0 讨论(0)
  • 2020-12-22 08:29

    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.

    0 讨论(0)
提交回复
热议问题