Professional Documents
Culture Documents
Printing
For desktop apps, printing is very similar to drawing to the screen. But instead of drawing into a Graphics
object of a Canvas control, you draw to a Graphics object created specifically for printing. There are methods
available to allow you to create new pages and get printer settings.
For web apps, printing is far more limited. Usually what works best is to simply create HTML, display it in an
HTML Viewer control and then print that.
Desktop Printing
Printer Settings
Before printing anything, you usually want to give the user the ability to select the printer they want to use.
This is done using the PrinterSetup class, which displays the Page Setup dialog for the operating system. In
addition, this class returns the settings so that you can use them again later without prompting the user.
If ps.PageSetupDialog Then
mPrinterSettings = ps.SetupString
End If
Since mPrinterSettings is a String, you can save it outside of your app (such as in a database or a file) for use
the next time the user prints.
The PrinterSetup class has properties for printer settings such as landscape, page size and resolution. The
Reference Guide topic for PrinterSetup has more details on these properties.
1
Printing and Reports
You can also use any printer settings that were previously stored from using the PrinterSetup class.
If ps.PageSetupDialog Then
mPrinterSettings = ps.SetupString ' Get new settings
Dim page As Graphics
' Use Printer Settings
page = OpenPrinterDialog(ps)
If page <> Nil Then
' Draw text on page 1 with 1 inch left/top margin
page.DrawString("Hello", ps.HorizontalResolution, ps.VerticalResolution)
page.NextPage
' Draw text on page 2 with 1 inch left/top margin
page.DrawString("World", ps.HorizontalResolution, ps.VerticalResolution)
End If
End If
Each time you call NextPage, the current page is sent to the printer and the graphics object (in this case page)
is cleared so that you can immediately begin drawing the new content. The final page gets sent to the printer
when the page goes out of scope.
To print without displaying the Printer dialog, you just call the OpenPrinter method:
2
Printing and Reports
The above code gives you a lot of control, but it is more work to track everything yourself. In the next section
you'll see how you can take advantage of StyledTextPrinter to make this easier. In addition, using a Report.
To print styled text, you first create a StyledTextPrinter object and then call the StyledTextPrinter method of
the TextArea (specifying the graphics to use and the width of the text) to get an instance of a
StyledTextPrinter that can be used for printing.
With this instance, you can call the DrawBlock method to draw the styled text to the page (specifying the
starting coordinates and the height).
This example prints styled text in a TextArea:
If p.PageSetupDialog Then
g = OpenPrinterDialog(p)
If g <> Nil Then
' Set width as 7.5 inches
stp = PrintTextArea.StyledTextPrinter(g, 7.5 * p.HorizontalResolution)
Do Until stp.EOF
' Fill the page with text
' at 10 inches height
stp.DrawBlock(0, 0, 10 * p.VerticalResolution)
If Not stp.EOF Then
' There is more text, so add a page
g.NextPage
End If
Loop
End If
3
Printing and Reports
End If
In order to support styled printing, the Text Area must have both its Multiline and Styled
properties ON (True).
If the text to print is larger than what will fit in the specified block, then you can iterate through the text until
it is all printed. You do this by checking the EOF property of the StyledTextPrinter class after each call to
DrawBlock.
This example prints the contents of a Text Area into two columns with a quarter inch spacing between the
columns:
Dim g As Graphics
Dim p As New PrinterSetup
If p.PageSetupDialog Then
g = OpenPrinterDialog(p)
' 1 inch margin
Dim leftRightMargin As Double = 1 * p.HorizontalResolution
Dim topBottomMargin As Double = 1 * p.VerticalResolution
' Page width after accounting for margins
Dim pageWidth As Double = p.Width - (leftRightMargin * 2)
' Column gap is 1/4 of the margin (so, 1/4 inch)
Dim columnGap As Double = leftRightMargin / 4
' Calculate the column width
' Subtract column gap from page with and
' split result 2
Dim columnWidth As Double = (pageWidth - columnGap) / 2
' Page size after accounting for margins
Dim pageHeight As Double = p.Height - (topBottomMargin * 2)
Dim stp As StyledTextPrinter
stp = PrintTextArea.StyledTextPrinter(g, pageWidth)
stp.Width = columnWidth
Dim columnToPrint As Integer = 1
Do Until stp.EOF
stp.DrawBlock(leftRightMargin + (columnWidth + columnGap) * (columnToPrint - 1), _
topBottomMargin, pageHeight)
If columnToPrint = 2 Then ' printing last column
If Not stp.EOF Then ' more text to print
g.NextPage
columnToPrint = 1
End If
Else ' more columns to print on this page
columnToPrint = columnToPrint + 1
End If
Loop
End If
HTML Printing
Another technique you can use is to create what you want to print using HTML, display it in an HTMLViewer
and then call its print method. This can sometimes be useful for simple reports. As a simple example, this
code loads an HTMLViewer with some HTML:
This code then displays the print dialog for the user to print the HTML:
HTMLViewer1.Print(True)
4
Printing and Reports
Example Projects
These example projects demonstrate desktop printing:
Printing and Reporting/Printing/GraphicsPrintingExample
Web Printing
Web printing is considerably simpler than desktop printing because it is much more restricted. Your web
application runs in a web browser, so you are limited by what a web browser can print.
Web browsers generally do a good job of printing HTML, so in order to generate something to print, you want
to first render it as HTML (either to a string or a file) and use an HTML Viewer control to display the HTML.
Once you have what you want displayed in an HTML Viewer, you can have a button that calls the Print method
of the HTML Viewer to ask the browser to print its contents:
HTMLViewer1.Print