Printing any information on a website has mostly been done by using the Javascript “window.print()” function. But what if you want to, for example, print to a label printer, or some other resource that requires specific formatting for the print job (and your CSS markup just doesn’t cut it)?
Well, with ASP.NET 2.0 we can accomplish such a feat using the System.Drawing.Printing namespace. Normally this namespace is popular for Forms based applications, but it can also be implemented into a web application to allow for greater customization and extension when printing.
The first thing to do is to import the System.Drawing.Printing namespace. This gives us direct access to the methods for controlling our printing. We also want to create a new PrintDocument object. This should be a privately declared object that would go above your methods / properties in your code-behind file. In VB.NET we must also declare the variable as WithEvents so we can have access to it’s methods. We would do this like so:
Private WithEvents myPrinter As New Printing.PrintDocument()
Next, we add a control that will fire the printing. For this example, we’ll use a button and give it an ID of “ButtonPrint”. This button can be placed from the designer view of the page. Then in the code behind file, create a method that will handle the button’s click event. In this sub routine we also need to set the printer name we want to use. For this example, the emphasis is on network printers (since so many people have trouble printing to them), so below is an example of the name of a network printer. Then, we call the print method to start the printing.
Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _ ButtonPrint.Click
myPrinter.PrinterSettings.PrinterName = “serverNameepson“
myPrinter.Print()
End Sub
Next we want to handle the myPrinter.PrintPage() event. Inside of this method, we have to draw our string onto the page. Here is an example of printing the current date and time.
Private Sub myPrinter_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles myPrinter.PrintPage
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim printFont = New Font(“Arial”, 10)
Dim sb As StringBuilder = New StringBuilder()
sb.Append(“DateTime: “ + DateTime.Now.ToString() + Environment.NewLine)
e.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, topMargin)
End Sub
This is probably the hardest section because of the e.Graphics.DrawString() method etc. Here’s what the parameters represent:
1. The string to draw.
2. The font to use.
3. The color of the string.
4. The x position to draw the string (in this case we’ve grabbed the margins and are drawing from there).
5. The y position to draw the string (same as the x position).
You’re going to have to play around with those things, and drawing multiple strings / shapes / images to get the hand of things.
One question that I had when I first learned this is what if I have multiple pages? What if I’m printing some huge table of values? Thankfully, the event has a hasMorePages() flag that can be raised when, for example, the next string you draw will exceed the height of the page. In this case, you’ll want some sort of global variable that will keep track of the index in your loop.
Printing can be a very cool feature to implement, but it can become extremely tricky to implement when you have more than one page. Hopefully this tutorial has shed some light of the basics of printing with ASP.NET and perhaps in the future I will go into more detail about multiple page printing.
Posted in ASP.NET 2.0