3

I need to print documents as output from a program I am writing in C#. Pure text does not suffice - I need tables, font choice, etc.

This sounds like something that should be extremely easy to accomplish, however, I am not sure how to do it.

Here are the options I'm considering:

  • DocX documents are easy to generate, however, I am not sure how to print them.
  • Crystal Reports, which has apparently been removed from .NET but can still be downloaded, seems to have the functionality. Any experiences with this?
  • I could generate an image or form and print that. However, the text would look bad when printed. I want crisp letters.
  • I could use XPS, but I think this wouldn't work on all printers? The program can't be picky printer-wise.
  • I could manually create whatever format the printer understands and manually send it somehow. This would probably be a lot of work and I'm not sure where to start.

Is there really no easier solution? And if not, what is the best option?

eboix
  • 5,113
  • 1
  • 27
  • 38
svinja
  • 5,495
  • 5
  • 25
  • 43
  • PDF may be a good option: http://stackoverflow.com/questions/465433/creating-pdf-files-at-runtime-in-c-sharp – Tim M. Dec 24 '11 at 22:26
  • You should also consider Html docs – L.B Dec 24 '11 at 22:28
  • If you want to print content of a RichTextBox, you can use [RichTextBoxPrintCtrl](http://support.microsoft.com/default.aspx?scid=kb;en-us;812425) control as a RichTextBox and print its content with images and font choices. I am not sure about tables. – sevenkul Dec 24 '11 at 22:36
  • Is this a WinForms application? – John Saunders Dec 24 '11 at 23:57
  • Yeah, it's WinForms. And L.B, I haven't thought of that, I think I'm going to try it with HTML. The WebBrowser control has a Print function and HTML is probably the easiest type of document to generate. – svinja Dec 25 '11 at 20:13

2 Answers2

3

It doesn't require that much work to send it to the printer manually. You just use the PrintDocument class and handle its events to do your drawing. You'll need to know GDI+, but that's the best and most efficient way. You can paint the results of a RichTextBox to it, for example.

Ry-
  • 218,210
  • 55
  • 464
  • 476
2

I really like ITextSharp as a way to produce custom reports as PDF documents. The library is somewhat complex, and you need to build everything from scratch (not like Crystal reports), but believe it is worth the effort.

You have the added benefit that you can save or email or download the PDF report which will enhance the functionality of your site.

I have achieved a lot by following this tutorial: http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp

agarcian
  • 3,909
  • 3
  • 33
  • 55
  • I think I will use this to export to PDF with less work, but it doesn't seem to help with printing. – svinja Dec 25 '11 at 20:14
  • You can create the PDF and then use this to send it to the printer. I have not tried it, but I think this might meet your needs... http://stackoverflow.com/questions/273675/print-existing-pdf-or-other-files-in-c-sharp – agarcian Dec 25 '11 at 20:46