By idiots, for idiots   Leave a comment

Gi’us a KISS.

Yesterday I wrote code to do printing in .net. It turns out that printing in .net, whilst more involved than VB6 (for plenty of good reasons), is actually quite simple and easy once you know how.

I mentioned that on Tuesday I read and failed to understand various .net printing tutorials. Maybe it was because I was being dense, maybe it just took a day for it to soak in. Maybe it was because all the tutorials were crammed full of unnecessary gumph like drawing code and XML comments (there are probably good reasons for XML comments, but obfuscating tutorial code isn’t one of them) and near-complete OO-programming tutorials, all of which get in the way and prevent you seeing what you actually want to see. After all, if you want to know how to use a screwdriver, you don’t watch a video of NASA assembling a space shuttle.

Let’s assume it was the latter, and so I present:

Colthor’s Guide to Printing in .net for the Easily Confounded

In .net (the sample code is written in VB.net, but should be exactly the same in C++, C# etc. and translation is left to the reader), the library class that does printing is the PrintDocument. It’s got lots of methods that you override to handle various events.

So, to perform very simple printing of a single page in .net you must:

1) Make a class that inherits from the PrintDocument class.
2) In this class, override the OnPrintPage method, and put any code drawing to the passed e.Graphics you want in it.
3) Set e.HasMorePages = false to say you have no more pages to print.

So a class to print “Hello World” in MS Sans Serif just inside the top-left corner of the page margin will give you something that looks like:



Imports System.Drawing.Printing

Public Class SimplePrint : Inherits Printing.PrintDocument

      Public Sub New()
            MyBase.New()
      End Sub

Protected Overrides Sub OnPrintPage(ByVal e As Printing.PrintPageEventArgs)
            MyBase.OnPrintPage(e)

            e.Graphics.DrawString("Hello World", New System.Drawing.Font("Microsoft San Serif", 10), System.Drawing.Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)

            e.HasMorePages = False
      End Sub

End Class


To make it actually print, you need to make an instance of the class, and then call the Print member function:



Dim pdoc As New SimplePrint
pdoc.Print()


Tada! Printing. Easy, huh?

Now, you probably don’t want it to just dump it to the default printer with the default settings, but pop up the print-setup dialogue to let the user choose. To do this:

1) Make an instance of the PrintDialog class.
2) Assign its document member variable to the instance of your PrintDocument class.
3) Show the dialogue box (call .ShowDialog()) and if it returns OK call the printing.

Which gives you:



Dim pdoc As New SimplePrint
Dim pdialog As New PrintDialog

pdialog.Document = pdoc

If pdialog.ShowDialog() = DialogResult.OK Then
      pdoc.Print()
End If



And there you go. You now know how to print in .net.

Some more info:

PrintDocument has several other members for you to override for different events. You can see them on the MSDN page here.

You’ll probably want to print more than one page. In that case – you’ll probably have guessed – for every page you’ve printed that isn’t the last one, set e.HasMorePages = true in OnPrintPage(). Keeping track of what you have and haven’t printed is up to you.

If you want to set defaults for the page setup dialogue, but still allow the user to override them if they want, you need to change members of pdialog.Document.DefaultPageSettings after you’ve assigned the PrintDocument to the PrintDialog class (ie. “pdialog.Document = pdoc” in the example above). For example, to make printing in landscape the default you’d add the line pdialog.Document.DefaultPageSettings.Landscape = True.

And there you go, from there you ought to be able to figure it out for yourself.

The End

Now, anybody with any programming knowledge is probably wondering “But that’s trivially easy, and fairly obvious. Why did he bother writing that? Surely anyone could figure it out?”. The answer: Because it’s trivially easy and fairly obvious. But every other tutorial I’ve seen has over-complicated it* to the point it’s made it seem like performing brain surgery on a rocket scientist. Hopefully this is more straightforward.

The rest of my day? Mostly playing games. I’ve got twelve tickets in the Steam prize thing now, I think that’s enough for everything I want.

* Or maybe they were over-simplifying it? Maybe the bits I needed to know were drowned out by all the noise I already knew, like how to make a class or what inheritence does.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s