Click here to Skip to main content
Click here to Skip to main content

Creating print preview page dynamically in ASP.NET

, 29 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on creating a print preview page dynamically by using JavaScript code. You do not need to make any ASPX page to view print preview, and you can also print any portion of the print page.

Sample Image - PrintPreview1.gif

Sample Image


If you want to show a print preview page before printing any page, then you have to make a page like the one that currently is showing.

Or, if you want to print a particular section of that page, like only a DataGrid, HTML table, or any other section, and you also need to preview that in a separate page before printing, you have to create a separate print preview page to show, which is more difficult for you.

I have introduced a technique to avoid this problem. You do not need to create a separate page for print preview. You just use the JavaScript code in Script.js to create a print preview page dynamically. It will take less time to implement and so is faster. Hopefully, it will be helpful for you.


I was developing a report module in my existing project. The report contents are generated dynamically by giving input (like generate by status, date range, etc). And, there is a print button to print. My client wanted to view a print preview page before printing, but we had already completed this module then. It was a really hard situation for my developers to build a print preview page for all the reports. I got this idea during that situation.

Using the code

You will just add the Script.js file in your project. The following code has been written in that file.

Source code

//Generating Pop-up Print Preview page
function getPrint(print_area)
    //Creating new page
    var pp =;
    //Adding HTML opening tag with <HEAD> … </HEAD> portion 
    pp.document.writeln('<HTML><HEAD><title>Print Preview</title>')
    pp.document.writeln('<LINK href=Styles.css type="text/css" rel="stylesheet">')
    pp.document.writeln('<LINK href=PrintStyle.css ' + 
                        'type="text/css" rel="stylesheet" media="print">')
    pp.document.writeln('<base target="_self"></HEAD>')

    //Adding Body Tag
    pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0"');
    pp.document.writeln(' leftMargin="0" topMargin="0" rightMargin="0">');
    //Adding form Tag
    pp.document.writeln('<form method="post">');

    //Creating two buttons Print and Close within a HTML table
    pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right>');
    pp.document.writeln('<INPUT ID="PRINT" type="button" value="Print" ');
    pp.document.writeln('<INPUT ID="CLOSE" type="button" ' + 
                        'value="Close" onclick="window.close();">');

    //Writing print area of the calling page
    //Ending Tag of </form>, </body> and </HTML>

The getPrint(print_area) function takes the DIV ID of the section you want to print. Then, it creates a new page object and writes the necessary HTML tags, and then adds Print and Close buttons, and finally, it writes the print_area content and the closing tag.

Call the following from your ASPX page. Here, getPrint('print_area') has been added for printing the print_area DIV section. print_area is the DIV ID of the DataGrid and the other two will work for others DIVs. Whatever areas you want to print must be defined inside of DIV tags. Also include the Script.js file in the ASPX page.

'calling getPrint() function in the button onclick event
btnPrint.Attributes.Add("Onclick", "getPrint('print_area');")
btnTablePP.Attributes.Add("Onclick", "getPrint('Print_Table');")
btnPagepp.Attributes.Add("Onclick", "getPrint('Print_All');")

Download the source code to get the getPrint() function.

I have used the following code in the demo project to generate a sample DataGrid:

Private Sub PopulateDataGrid()
    'creating a sample datatable
    Dim dt As New System.Data.DataTable("table1")
    Dim dr As Data.DataRow
    dr = dt.NewRow
    dr("UserID") = "1"
    dr("UserName") = "Ferdous"
    dr("Phone") = "+880 2 8125690"
    dr = dt.NewRow
    dr("UserID") = "2"
    dr("UserName") = "Dorin"
    dr("Phone") = "+880 2 9115690"
    dr = dt.NewRow
    dr("UserID") = "3"
    dr("UserName") = "Sazzad"
    dr("Phone") = "+880 2 8115690"
    dr = dt.NewRow
    dr("UserID") = "4"
    dr("UserName") = "Faruk"
    dr("Phone") = "+880 2 8015690"
    DataGrid1.DataSource = dt
End Sub

Use the following code in a separate style sheet page. See PrintStyle.css if you want to hide the Print and Close buttons during printing.


Points of interest

I was facing a problem during print. Print was not working the first time I clicked it. I solved that problem by reloading by using location.reload(true); on that page.

Revision history

  • 4-19-2006:
    • Second revision. Incorporated readers comments: Fleshed out several concepts, modified a few sections (sorry BigJim61, I think I have discussed more in my article now).
  • 4-17-2006:
    • First revision. Added a new section.
  • 4-15-2006:
    • Original article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

MJ Ferdous
Architect Brain Station-23
Bangladesh Bangladesh
Ferdous has industry level experience with SharePoint and has done several presentations and workshops on SharePoint. He also worked as SharePoint Consultant (CREDEM Italy, Robi etc) and Trainer (BASIS, JAXARA IT, LEADS Co.). He is currently working as SharePoint Architect at BrainStation-23. He also worked as Technical Project Manager for Congral LLC for managing revolutionizing Patient Centric Healthcare applications at the same company since 2009.
He worked on many international projects during his professional life. The major projects are included below:
Internal Enterprise Portal for Robi, Dhaka
nVision Solution for nSales A/S, Denmark
Shared Care Plan for Congral LLC, USA
Internet Banking Portal for the Bank of CREDEM, Italy
Document Management (DMS) for the Bank of CREDEM, Italy
MES solution for Rockwell Automation, Italy
Tourism for Travel Curve Inc., USA
and so on...
He is the author of several technical articles with over 49 articles published on where he is the Technical Author for the site and author of as well. He is also founder of SharePoint Expert group.
Looking for a Offshore Development or partnership.............. in any development in Dot.Net & Sharepoint 2007,2010 Platform.
Search him in google by 'MJ Ferdous' to get all links, articles, profile etc
Contact him:
Production troubleshooting, maintainability and scalability
SharePoint 2007/2010
Dot.Net Application
Project Management
Document Management Solution
Follow on   Twitter

Comments and Discussions

QuestionThanks PinmemberBhargav Software7-Feb-14 17:51 
Generalthanks PinmemberMember 950731325-Feb-13 1:11 
Questionproblem during print PinmemberPadmasinh12-Dec-12 1:34 
AnswerRe: problem during print PinmemberMJ Ferdous30-Jan-13 20:01 
GeneralMy vote of 5 Pinmemberhadi552615-Jul-12 22:09 
GeneralMy vote of 5 PinmemberSnorri13-Mar-12 6:57 
Generalprint subject Pinmemberzahragh17-Feb-12 23:46 
GeneralRe: print subject PinmemberMJ Ferdous18-Feb-12 6:05 
GeneralMy vote of 5 PinmemberCodeHelper_PS24-Sep-11 1:18 
Questionneed to remove styles in print preview PinmemberSalini.P18-Sep-11 20:37 
AnswerRe: need to remove styles in print preview [modified] PinmemberMJ Ferdous18-Sep-11 23:03 
GeneralRe: need to remove styles in print preview PinmemberSalini.P19-Sep-11 1:21 
GeneralMy vote of 2 Pinmemberrutstyle19-Mar-11 6:08 
GeneralRe: My vote of 2 PinmemberVivekanandan1-Nov-11 8:20 
GeneralRe: My vote of 2 PinmemberMJ Ferdous18-Feb-12 6:08 
Questionlocation.reload Pinmemberrockstar_raph21-Feb-11 7:18 
AnswerRe: location.reload Pinmemberrockstar_raph21-Feb-11 7:49 
GeneralRe: location.reload PinmemberMJ Ferdous21-Feb-11 8:18 
QuestionProblem printing page using pop up window, please help!!! Pinmembersabitarai23-Aug-10 23:13 
GeneralPage Keeps Loading. Pingroupanup choudhari3-Aug-10 4:27 
GeneralSelected Area Printing Preview Pinmemberffowler2-Apr-10 11:29 
QuestionHow To Print 16Column Pinmembermdazeemuddin6-Mar-10 23:59 
GeneralThank you. Simply great PinmemberBSRK6-Jan-10 0:35 
GeneralRe: Thank you. Simply great PinmemberMJ Ferdous7-Jan-10 10:25 
GeneralWeb Links of this article PinmemberMJ Ferdous22-Dec-09 23:59 
GeneralGridview with paging PinmemberRaj Vijay8-Aug-09 7:48 
GeneralRe: Gridview with paging PinmemberMJ Ferdous9-Aug-09 5:06 
GeneralBrowser keep loading.... PinmemberNick Meng3-Aug-09 18:42 
I have tested with few different Internet Browser...the Print page is keep loading and never finish.
Do you have any ideas about this?

Nick Meng   Smile | :)
GeneralRe: Browser keep loading.... PinmemberMJ Ferdous9-Aug-09 6:21 
QuestionBlank Popup Page? Pinmembereggnturtle29-Jul-09 13:34 
AnswerRe: Blank Popup Page? PinmemberMJ Ferdous9-Aug-09 5:18 
QuestionHow to Hide a Column of Gridview before preview? [modified] Pinmemberasatucap23-Jun-09 23:54 
AnswerRe: How to Hide a Column of Gridview before preview? PinmemberMJ Ferdous9-Aug-09 5:15 
GeneralThank you! woks great PinmemberAlavri21-Feb-09 19:43 
GeneralRe: Thank you! woks great PinmemberMJ Ferdous24-May-09 23:54 
GeneralWorks ok [modified] PinmemberVH111-Feb-09 7:50 
GeneralGridview PinmemberNewbieAE30-Jan-09 4:42 
GeneralRe: Gridview PinmemberMJ Ferdous24-May-09 23:50 
GeneralRe: Gridview PinmemberBSRK6-Jan-10 0:33 
GeneralRe: Gridview PinmemberMJ Ferdous7-Jan-10 10:24 
QuestionHow to display multiline texbox for print preview? Pinmemberkrup7528-Jan-09 13:22 
AnswerRe: How to display multiline texbox for print preview? PinmemberMohammad Jannatul Ferdous28-Jan-09 22:46 
GeneralIt doesn't work on IE 7.0 PinmemberSavun30-Dec-08 22:34 
GeneralRe: It doesn't work on IE 7.0 PinmemberKenHeer11-Jun-09 5:11 
GeneralMy vote of 1 PinmemberNejat5-Dec-08 3:22 
QuestionDisable GridView hyperlink columns on popup window?? Pinmemberdcvs200516-Nov-08 15:19 
AnswerRe: Disable GridView hyperlink columns on popup window?? PinmemberMohammad Jannatul Ferdous17-Nov-08 7:11 
GeneralPrint Pinmemberjmatt00231-Oct-08 10:35 
QuestionRemove hyperlinks and Landscape?? Pinmembercbwarley13-Nov-07 12:00 
AnswerRe: Remove hyperlinks and Landscape?? PinmemberMohammad Jannatul Ferdous29-Oct-08 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 29 Oct 2008
Article Copyright 2006 by MJ Ferdous
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid