在ASP.NET打印的最佳方法 [英] Best way to print in ASP.NET

查看:119
本文介绍了在ASP.NET打印的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不改变我用XSLT所需的模板后,保存在内存中一个HTML字符串。到发送给客户端打印机的最佳机制是什么?

I have a html string held in memory after transforming to my desired template with XSLT. What is the best mechanism to the send this to the client printer?

在previous项目,我无耻地欺骗,并创建了一个打印preVIEW屏幕,它基本上是白色背景的ASPX页面,我再使用印刷 Window.print()

In previous projects I have shamelessly cheated and created a print preview screen, which was essentially an ASPX page with white background that I then printed using Window.print().

干杯

推荐答案

我不认为一个打印preVIEW是欺骗的。由于您的字符串是最有可能在服务器上,你必须输出到客户端某种方式,并呼吁window.print()打印(即在ASP.NET code-后面创建)。有没有办法让一个网络服务器来访问客户端的打印机。然而,你也许可以简化使用一些技巧的东西:

I don't think a 'print preview' is cheating at all. Since your string is most likely on the server (ie created in ASP.NET code-behind), you must output it to the client somehow and call window.print() to print. There's no way for a webserver to access a client's printers. However you may be able to streamline things using a few tips:

- 使用@media在CSS标签在为网页提供了可打印的风格。在很多情况下,你可以用它来隐藏任何导航等等,并没有一个需要一个'打印preVIEW。我通常添加类似下面的我的.css文件:

-use @media tags in CSS to provide a printable style for your page. In many cases, you can use this to hide any navigation, etc. and not have a need for a 'print preview'. I usually add something like the following to my .css file:

.showwhenprinting {position:absolute; display:none;}
@media print
{
  .hidewhenprinting {position:absolute; display:none;}
  .showwhenprinting {position:relative; display:block;}
}

这将让大多数浏览器打印时隐藏块。例如:

This will let you hide blocks when printing in most browsers. For example:

<div class="someclass hidewhenprinting">Navigation Menu</div>
<div class="someclass showwhenprinting">Printed Page Title</div>

这种方法的另一个好处是它允许用户只需点击'打印',因为他们通常会在浏览器中。缺点是,一些旧的浏览器不支持它,在某些情况下(如果你想页眉/页脚为例)它不会给你足够的控制布局。

The other benefit of this approach is that it allows users to simply click 'print' as they normally would in the browser. The drawbacks are that some older browsers do not support it, and in some cases it does not give you enough control over the layout (for example if you want page headers/footers).

- 如果上面不给你你需要的输出(例如,你不想要的网页网址等对打印结果),另一种选择是使用一台打印机友好的格式,为PDF。要做到这一点,使用Reporting Services或类似的工具,用于生成您的PDF。

-if the above doesn't give you the output you need (for example, you don't want the page URL, etc. on the printed result), the other option would be to use a printer-friendly format such as PDF. To do this, use Reporting Services or a similar tool for generating your PDF.

-A最后的选择,我不会真的建议是一个隐藏的iframe添加到页面中,你的打印的HTML填充它,并调用印刷()在它。这可能工作,但需要一些JavaScript能够成功,所以你需要照顾,使其跨浏览器。这将有选择#1相同的缺点,但更难以得到的权利。

-A final option I wouldn't really recommend is to add a hidden iframe to the page, populate it with your "printable" HTML, and call print() in it. This may work, but would require some javascript to pull off, so you would need to take care to make it cross-browser. This would have the same drawbacks of option #1, but be more difficult to get right.

这篇关于在ASP.NET打印的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆