Skip to main content

How to open print dialog box to print page or part of the page in JavaScript

some time we need to print only a portion of page or whole page through Javascript, Javascript provide a function named print() to print the current page, so if you want to print whole page then just call window.print() function but if you want to print specific portion of the page then you need to do something extra.

To open print dialog box the current whole page matter just call window.print() function in button click event or link button click event.

How to open print dialog box to print whole page - Javascript


<input type="button" onclick="javascript:window.print()" value="Print Current Page" />


Below is the code, just open a new blank window with the help of window.open and write inner html of the specific portion that you need to print in new window then call print() function for this new window.

How to open print dialog box to print specific part / portion of the page - Javascript


function PrintPartOfPage(dvprintid)
 {
      var prtContent = document.getElementById(dvprintid);
      var WinPrint = window.open('', '', 'letf=100,top=100,width=600,height=600');
      WinPrint.document.write(prtContent.innerHTML);
      WinPrint.document.close();
      WinPrint.focus();
      WinPrint.print();
      //WinPrint.close()   
 }


Calling -

<input type="button" onclick="PrintPartOfPage('divPrnt')" value="Print Portion" />


Popular posts from this blog

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview, Scrollble gridview with fixed header through javascript, File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview.

We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)