Hướng dẫn javascript printer control

Ý tưởng là đặt các nội dung được in bên trong điều khiển ASP.Net Panel và sau đó in nội dung trong đó.





    

    





    
Hello,
This is Mudassar Khan.
Hoping that you are enjoying my articles!

Trong Markup HTML ở trên tôi có control panel pnlContents có nội dung cần in và nút btnPrint trong đó có sự kiện OnClientClick gọi phương thức JavaScript PrintPanel() để in nội dung của Panel.

There may be times in which your website or application would like to improve the user's experience when printing content. There are a number of possible scenarios:

  • You wish to adjust layout to take advantage of the size and shape of the paper.
  • You wish to use different styles to enhance the appearance of your content on paper.
  • You wish to use higher resolution images for a better result.
  • You want to adjust the user experience of printing, such as presenting a specially-formatted version of your content before printing begins.

There may be other cases in which you want to manage the printing process, but these are some of the most common scenarios. This article provides tips and techniques for helping your web content print better.

Using a print style sheet

Add the following to your tag.

<link href="/path/to/print.css" media="print" rel="stylesheet" />

Detecting print requests

Browsers send beforeprint and afterprint events to let content determine when printing may have occurred. You can use this to adjust the user interface presented during printing (such as by displaying or hiding user interface elements during the print process).

Examples

Here are some common examples.

Open and automatically close a popup window when finished

If you want to be able to automatically close a popup window (for example, the printer-friendly version of a document) after the user prints its contents, you can use code like this:

DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>JavaScript Window Close Exampletitle>
    <script>
      function popuponclick() {
        const my_window = window.open(
          "",
          "mywindow",
          "status=1,width=350,height=150"
        );
        my_window.document.write("Print Me");
        my_window.document.write('');
        my_window.document.write(
          "

When you print this window, it will close afterward.

"
); my_window.document.write(""); }
script> head> <body> <p> To try out the <code>afterprintcode> event, click the link below to open the window to print. You can also try changing the code to use <code>beforeprintcode> to see the difference. p> <p><a href="javascript: popuponclick()">Open Popup Windowa>p> body> html>

View Live Examples

If you want to be able to print an external page without opening it, you can utilize a hidden