Best way to print for Windows Clients (Not Web Apps)?

后端 未结 7 1879
青春惊慌失措
青春惊慌失措 2021-02-05 18:43

What is the best way to print stuff from c#/.net?

The question is in regard to single pages as well as to reports containing lots of pages.

It would be great to

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 19:01

    For reports, I use the RDLC control.

    For everything else, I use the inherent printing objects within .NET.

    Edit The inherent printing objects are all found in the System.Drawing.Printing namespace. When you use the PrintDialog or the PrintPreviewDialog in a WinForms (or WPF) application, it is to these objects that you're turning over control.

    The fundamental concept is that you're drawing to the printer. The simplest form of this is:

    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
    

    What's happening here is that when my object (x) is sent the print command, it raises the "PRINT PAGE" event (which is designed to print 1 page at a time). This event then uses the Graphics attribute of the PrintPageEventArgs to draw the relevant string directly to the print spooler.

    Here's one tutorial, and a quick Google search for ".NET printing tutorial" returns a bit over 200K results.

提交回复
热议问题