4

I am using the following code to print a page within my application...

<html><body onload=""window.print();"">"
  sHtmlBody = sHtmlBody & "<body>"

The window.print() is working fine. I know once the print comes up I can manually go into the settings and remove headers and footer. On IE I know that I have to go to print preview and then remove the print headers.

However, is there some line of code which does this automatically so the users of the application don't have to do this?

EDIT:

 sHtmlBody = "<style type='text/css'>"
      sHtmlBody = sHtmlBody & " @media print{"
      sHtmlBody = sHtmlBody & " body{ background-color:#FFFFFF; background-image:none; color:#000000 }"
      sHtmlBody = sHtmlBody & " #ad{ display:none;}"
      sHtmlBody = sHtmlBody & " #leftbar{ display:none;}"
      sHtmlBody = sHtmlBody & " #contentarea{ width:100%;}"
      sHtmlBody = sHtmlBody & " }"
      sHtmlBody = sHtmlBody & " </style>"
      sHtmlBody = sHtmlBody & "<html><body onload=""window.print();"">"
      sHtmlBody = sHtmlBody & "<body>"
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Beginner
  • 28,539
  • 63
  • 155
  • 235

1 Answers1

7

you can do with the help of CSS , before print set the CSS of the page . for example:

<style type="text/css">
@media print{
  body{ background-color:#FFFFFF; background-image:none; color:#000000 }
  #ad{ display:none;}
  #leftbar{ display:none;}
  #contentarea{ width:100%;}
}
</style>

This code when added to the page hides the 2 divs with ids "ad" and "leftbar", plus makes additional changes to the rest of the document when it's printed.

IF you are asking about browser specific settings like print date and time , then i think it is not possible through code. check this out : Remove the default browser header and footer when printing HTML

Community
  • 1
  • 1
Pranav
  • 8,563
  • 4
  • 26
  • 42
  • 2
    I see no effect. For which browser should this have an effect? What container should be #ad, #leftbar and #contentarea? – Horcrux7 Apr 02 '15 at 06:48
  • Hi, The link you pointed to has some new answers that remove the browser specific settings. Essentially, add @page {margin:0;} to your css – mdiehl13 May 09 '20 at 08:58