TX Text Control ActiveX

Printing

Topic

In most cases, one line of code sends a TXTextControl document to the printer. When an application needs more control, such as letterhead paper for the first page or a preview window, it prints the document page by page to a device context of its choice.

The Visual Basic 6 samples start with the one-line print job and a print dialog box, then build a page-by-page printing routine and extend it with paper bins and a print preview.

Change Paper Bins

A typical requirement of office applications is to print the first page of a document on letterhead paper and the remaining pages on plain paper. For this, you usually use a printer with two separate trays for the two types of paper. The application sends the first page to the first paper bin and then switches to the second bin for the remaining pages.

Two lines that set the paper bin extend the printing code of the previous section:

Printer.PaperBin = vbPRBNLower ' select first tray
For CurPage = 1 To TXTextControl1.CurrentPages
   Printer.Print
   ' select second tray
   if CurPage = 2 then Printer.PaperBin = vbPRBNUpper
   TXTextControl1.PrintDevice = Printer.hDC
   TXTextControl1.PrintPage CurPage
   If CurPage <> TXTextControl1.CurrentPages Then Printer.NewPage
Next CurPage
Printer.EndDoc

The code works as follows:

  • Before the loop, Printer.PaperBin selects the first tray, vbPRBNLower.
  • From page 2 on, it selects the second tray, vbPRBNUpper.

Show a Print Preview

PrintPage prints not only to printers but to any control that has an hDC property. A Picture Box, for instance, serves as a print preview window: assign its hDC property to the PrintDevice property, and PrintPage prints to the Picture Box instead of the printer.

The following code prints page 1 to a Picture Box named Picture1. The PrintZoom property scales down the printout when the Picture Box is smaller than a full printer page:

TXTextControl1.PrintDevice = Picture1.hDC
TXTextControl1.PrintZoom = 25 ' scale down to 25%
TXTextControl1.PrintPage 1

The TXWords sample program has a full print preview form that works this way. Its source code is in the Samples\vb6\TXWords folder.

The print preview of the TXWords sample showing two pages side by side, with the Print, Next Page, Prev Page, One Page, Zoom In, Zoom Out and Close buttons, in the look of TX Text Control 10

Key Members

MemberDescription
PrintDocPrints a range of pages in a number of copies on the default printer.
PrintPagePrints one page to the device that PrintDevice sets, without ejecting it.
PrintDeviceSets the device context that PrintPage prints to: a printer or a control such as a Picture Box.
PrintZoomScales the printout, for instance for a preview.
CurrentPagesReturns the number of pages, the last page to print.

The programming category Printing and Print Preview lists all members for printing.