简体   繁体   English

如何使用代码删除打印页眉/页脚

[英]how to remove print header/footer with code

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. window.print()工作正常。 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. 在IE上我知道我必须去打印预览然后删除打印标题。

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>"

you can do with the help of CSS , before print set the CSS of the page . 你可以在CSS的帮助下,在打印之前设置页面的CSS。 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. 添加到页面时,此代码会隐藏带有“ad”和“leftbar”的2个div,并在打印时对文档的其余部分进行其他更改。

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 检查一下: 打印HTML时删除默认的浏览器页眉和页脚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM