c# - 为 Windows 客户端(而非 Web 应用程序)打印的最佳方式?

标签 c# .net printing

从 c#/.net 打印内容的最佳方式是什么?

问题是关于单个页面以及包含大量页面的报告。

如果能得到一份最常见的打印库列表,其中包含每个库的主要功能和陷阱,那就太好了。

[更新] 适用于标准 Windows 客户端(或服务器),不适用于 Web 应用程序。

最佳答案

对于报告,我使用 RDLC 控件。

对于其他一切,我使用 .NET 中固有的打印对象。

编辑 固有的打印对象都可以在 System.Drawing.Printing 命名空间中找到。当您在 WinForms(或 WPF)应用程序中使用 PrintDialog 或 PrintPreviewDialog 时,您正在将控制权交给这些对象。

基本概念是您正在向打印机绘图。最简单的形式是:

Sub MyMethod()
     Dim x as New PrintDocument
     AddHandler x.PrintPage, AddressOf printDoc_PrintPage
     x.Print
End Sub
Sub printDoc_PrintPage( sender as Object,  e as PrintPageEventArgs)
      Dim textToPrint as String= ".NET Printing is easy"
      dim printFont as new Font("Courier New", 12)
      dim leftMargin as int= e.MarginBounds.Left
      dim topMargin as int = e.MarginBounds.Top
      e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin)
End Sub

这里发生的事情是,当我的对象 (x) 收到打印命令时,它会引发“PRINT PAGE”事件(旨在一次打印 1 页)。然后,此事件使用 PrintPageEventArgs 的图形属性将相关字符串直接绘制到后台打印程序。

Here's one tutorial ,然后在 Google 上快速搜索“.NET 打印教程”会返回超过 20 万个结果。

关于c# - 为 Windows 客户端(而非 Web 应用程序)打印的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/371384/

相关文章:

c# - 用于验证 Windows UNC 路径的正则表达式

c# - 没有中央服务器的 P2P 握手

ruby - 如何在 ruby​​ 中打印多维数组?

C程序打印错误输出

c# - Stripe .net "The signature for the webhook is not present in the Stripe-Signature header."

c# - 按 CultureInfo 格式化字符串

c# - 无法使用枚举隐式转换类型

.net - 选择散列函数以获得最佳性能

c# - 是否强制执行 WCF 数据流限制?

java - 获取有关打印作业的信息