Printing from a website (ASP & JavaScript)

Posted on 2004-04-21
Last Modified: 2011-08-18

I have an ASP page that draws up a report and display it to the user. If the user wishes to do so, he can click a "print" button which activates the printer dialog via javascript. This would be the same as if they selected "File > Print" from the browser menu.
My problem is that at hte top of the page, page numbers are added automatically, and at the bottom the URL of the page that was printed. Is there a way to get rid of this extra information, so that a "clean" document can be printed with only the results from the report.

Any help would be appreciated,
Question by:split-etm-

Expert Comment

ID: 10878299
Not printing via a browser.  You can give them a way to open the report in Excel via a CSV file or XLS file.  They can then print from there.

LVL 10

Accepted Solution

eeBlueShadow earned 250 total points
ID: 10878394
These details are added in the browser preferences (File->Page Setup in IE) and you can't change it using web scripting. Sorry :(

LVL 53

Expert Comment

ID: 10881798
If you are going across the Internet with this you best option is to give the user instructions of how to change their settings as outlined by eeBlueShadow.

If this is on an intranet you can use activeX to modify the settings programatically using:

I recommend against using the ActiveX solution across the Internet because it is IE only; may be blocked by some corporate firewalls, may generate a security alert and the user will have to agree to allow it.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Expert Comment

ID: 10910063
it is not possible to change the print settings used by IE using normal html and javascript. You can trigger the print event by using window.print(), but you cannot set the configuration when printing. Only alternative is to either print via Microsoft Word (set ContentType to word document and alter the document to what you want to output) or use an activeX script - but that will require the user to allow activeX scripts to execute (put your domain in the trusted domain list or change the IE settings to allow active X).

Expert Comment

ID: 10964891
I've used the ScriptX activeX script that Coboldinosaur has refered to, and it works great.

Yes, IE users will be prompted with a security alert, but if that works for you this is an ideal solution.  If you download the code from the link   extract the zip file and upload to your web space.  (Be sure to use Binary FTP mode if you are on windows)

Here is some example code to make it work, you just stick this right into your HTML output:



<!--- JavaScript to operate the activeX object -->
<script language="Javascript"><!--
function doprint() {
  //save existing user's info
  var h = factory.printing.header;
  var f = factory.printing.footer;
  //hide the button
  document.all("printbtn").style.visibility = 'hidden';
  //set header and footer to blank
  factory.printing.header = "";
  factory.printing.footer = "";
  //print page without prompt
  //restore user's info
  factory.printing.header = h;
  factory.printing.footer = f;
  //show the print button
  document.all("printbtn").style.visibility = 'visible';



<object id="factory" viewastext  style="display:none"
<input type="button" name="printbtn" onClick="doprint()" value="Print">


The above code will create a button with the text "Print" if the user clicks it, the printout will have no header/footer text added by the browser.  The user will be prompted to install the activeX by a security dialogue if their security settings are set to medium or high on internet explorer.  However, they will only need to click yes the first time they visit your page.



Expert Comment

ID: 11271101
The author of this post has not commented for quite some time.  Can we please get an update on the issue, or close the question?


Author Comment

ID: 11303987
Hi Guys,

Sorry yes i have not come back to give feedback on this question. i did find a solution with the help from eeBlueShadow, but i had to add some script to make it work the way i wanted. Thanks eeBlueShadow.
Thanks to all the rest as well for contributions, unfortunately i could not use the activex scripting solution, even though im sure it would have worked, because of some security/compatibility issues.

i am not sure if anyone would be interested, but here is what i did to solve my problem. after removing the header/footer, i added the following code to display a 'close' button, but without actually printing it on the reports...i needed this because the report window opens in a chromeless window, which does not display any standard window controls. i added this code to the document for the button:

<div id="divHide" name="divHide" style="visibility:hidden;">
      <font style="font-family:Verdana;font-size:7pt"><a href="#" onclick="javascript:window.close();">Close</a></font>

<script language=javascript>
      function window.onafterprint() {
            //after printing is done, set the close button
   = 'visible';

the button will at first be hidden, until after the document displayed the printing dialog. by using the window.onafterprint() event, i could enable the button's visibility, which would enable the user to close the window.

cheers & thanks again!

Expert Comment

ID: 11754833
how can you set the lenght of the footer using framing object (active x)?
The text I am triyng to set is too long

Expert Comment

ID: 13401199
I found this script somewhere on the net and it works (though you get a warning about unsafe ActiveX)
you should call ResetHeader() to restore the settings.

<script Language="vbscript">
    Dim WSHShell
    Dim myHeader
    Dim myFooter
    Set WSHShell = CreateObject("WScript.Shell")
    myHeader = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\header")
    myFooter = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer")

    WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", ""
    WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", ""

    Sub ResetHeader()
        Dim WSHShell
        Set WSHShell = CreateObject("WScript.Shell")
        WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", myHeader
        WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", myFooter
    End Sub

Expert Comment

ID: 13636728
I got an error here
(The ActiveX component cannot create the object WScript.Shell)

Do I have to download and register it  (Does it coast ?)

Many Thanks

Expert Comment

ID: 14473568
I got an error here
(The ActiveX component cannot create the object WScript.Shell)

Do I have to download and register it  (Does it coast ?)

I think you have the wrong idea here, but can I firstly ask for hw you are trying sto use this, if you are using ASP you will need to use



Expert Comment

ID: 14486654
You may not have the WSH installed (Windows Scripting Host) -
Visit the official site at: and you can download it by clicking on "Script Downloads"

This type of scripting is generally used with an intranet because Internet Explorer will ask you if you want to run the ActiveX script, etc... and most users will not run it + security risk, etc.... If you use this on an intranet site it will usually run without any problems (depending on your security settings).

HackerNeo, Since this is a client-side script (vbscript is client-side ASP is server-side) you do not need the Server.CreateObject, it is correct the way it is written. WSH is free and a simple install takes care of everything.

Also in the Registry under: HKCU\Software\Microsoft\Internet Explorer\PageSetup\
You will also find keys for the printer margins along with the header and footer (listed below)

header, footer, margin_top, margin_bottom, margin_left, margin_right

Hope this helps, I used this same script for an intranet app that had to modify the margins to print to a label printer.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question