• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 40956
  • Last Modified:

Printing from a website (ASP & JavaScript)


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,
1 Solution
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.

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

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.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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).
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 http://www.meadroid.com/scriptx/sxdownload.asp   extract the zip file and upload ScriptX.cab 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.


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?

split-etm-Author Commented:
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
            document.all.divHide.style.visibility = '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!
how can you set the lenght of the footer using framing object (active x)?
The text I am triyng to set is too long
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
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
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


You may not have the WSH installed (Windows Scripting Host) -
Visit the official site at: http://msdn.microsoft.com/scripting/ 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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now