tags:

views:

3204

answers:

5

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 get a list of the most common printing libs containing the main features and gotchas of each of them.

[Update] for standard windows clients (or servers), not for web apps, please.

+2  A: 

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.

Stephen Wrighton
Could you expand on the inherent printing objects a bit more?
Tigraine
Yeah, that would be great!!
Sam
A: 

It depends a lot on the requirements of your application.

Even though it isn't the perfect tool (really far from that), Crystal Reports tends to be a good choice. It gives you the option of getting data directly from a Database or, if you already have a list of objects you want to print, you can pass them to the document and bind the object properties to the labels of the report.

But give us some more information of what you're trying to do, so you can receive better proposals.

Nelson Reis
Oh well, I've got loads of printing stuff coming at me: single pages, long reports, sticky labels, name it, I'll have to print it (soon). So I'd like to get an overview about printing solutions pro and contra.
Sam
+1  A: 

We used a set of third party DLLs from PDFSharp who in turn use DLLs from MigraDoc. I'm not privy to all the reasons that we went that direction (the decision was made by a senior developer), but I can tell you that:

  • It seems to be in active development.
  • It had most of the features we needed.
  • The source code is available. Although it used some patterns and conventions that I hadn't seen before, once I got on to them, it was fairly easy to make the changes. I added support for using the System.Drawing.Image directly rather than as saving files.
  • It is not documented well either internally or externally.
Robert Gowland
+1  A: 

Loads of stuff, you say. Hum, seems that you should use a solution with a designer, so you should look into Crystal Reports and RDLC. There's also the Reporting Services solution, but in that case you would need a server with SQL Server.

Crystal Reports seems to give you more choices, but needs a little more learning than RDLC.

I wouldn't recommend you create those in HTML + CSS, because of the limitations and the extra work you would have to throw at it.

Nelson Reis
+1  A: 

If you can build your output as a FlowDocument, you can turn it into XPS easily to get an "electronic" version, and the print the XPS.

Daniel Earwicker