# Printing

> Print with PrintDoc or page by page with PrintPage and PrintDevice, switch paper bins for letterhead and preview pages in a Picture Box with PrintZoom.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Printing
- **1787 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/printing/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/printing/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/printing/llms-full.txt

---

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.

### Print a Document

The [PrintDoc](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printdoc-method/llms-full.txt) method detects the default printer and its settings automatically, so you only specify the range of pages and the number of copies:

```vbscript
TXTextControl1.PrintDoc "My Print Job", 1, _
   TXTextControl1.CurrentPages, 1
```

The code prints one copy of the current document and works as follows:

- The first parameter, "My Print Job", is a name of your choice under which the print manager shows the print job.
- The second and the third parameters are the range of pages to print, here from page 1 to the last page, which the [CurrentPages](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/currentpages-property/llms-full.txt) property returns.
- The last parameter is the number of copies to print.

#### Let the User Choose the Printer and the Pages

A Common Dialog box lets the user select the printer, the range of pages and the number of copies:

```vbscript
' Display a common print dialog
CmDialog1.Copies = 1
CmDialog1.FromPage = 1
CmDialog1.ToPage = TXTextControl1.CurrentPages
CmDialog1.Min = 1
CmDialog1.Max = CmDialog1.ToPage
CmDialog1.Flags = cdlPDHidePrintToFile Or cdlPDNoSelection
CmDialog1.ShowPrinter
' Print selected pages
TXTextControl1.PrintDoc Form.Caption, CmDialog1.FromPage, _
   CmDialog1.ToPage, CmDialog1.Copies
```

The *TXWords* sample of version 34.0 doesn't need this code: it calls [PrintDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printdialog-method/llms-full.txt), which opens the system print dialog box and prints the document, as [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/#print-and-preview-the-document) shows.

### Print Page by Page

The options of `PrintDoc` are enough for most applications. When you need more control over printing, for instance to use a different paper bin for the first page than for the following pages, use the [PrintPage](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printpage-method/llms-full.txt) method.

`PrintPage` does nothing but send a single page to the printer, which gives you as much flexibility as possible. It does not even eject the printed page, so you can print the contents of several `TXTextControl` controls on one sheet of paper.

`PrintPage` has one parameter, the page number, and you call it for each page to print in turn. Selecting the printer and controlling the print job takes some more properties and methods:

```vbscript
For CurPage = 1 To TXTextControl1.CurrentPages
   ' Initialize the VB Printer object
   Printer.Print
   ' Select a printer
   TXTextControl1.PrintDevice = Printer.hDC
   ' Print one page
   TXTextControl1.PrintPage CurPage
   ' Eject the page
   If CurPage <> TXTextControl1.CurrentPages Then Printer.NewPage
Next CurPage
' Tell Print Manager we are finished
Printer.EndDoc
```

The code works as follows:

- `Printer.Print` initializes the Visual Basic `Printer` object.
- [PrintDevice](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printdevice-property/llms-full.txt) selects the printer by its device context, `Printer.hDC`.
- `PrintPage` prints one page, and `Printer.NewPage` ejects it after every page but the last.
- `Printer.EndDoc` tells the print manager that the print job is finished.

The next two sections extend this basic routine.

### 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:

```vbscript
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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printzoom-property/llms-full.txt) property scales down the printout when the Picture Box is smaller than a full printer page:

```vbscript
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](https://docs.textcontrol.com/assets/img/tx-text-control-activex/print-preview-two-pages.5b1f27a1.webp)

### Key Members

| Member | Description |
|---|---|
| [PrintDoc](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printdoc-method/llms-full.txt) | Prints a range of pages in a number of copies on the default printer. |
| [PrintPage](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printpage-method/llms-full.txt) | Prints one page to the device that `PrintDevice` sets, without ejecting it. |
| [PrintDevice](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printdevice-property/llms-full.txt) | Sets the device context that `PrintPage` prints to: a printer or a control such as a Picture Box. |
| [PrintZoom](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/printzoom-property/llms-full.txt) | Scales the printout, for instance for a preview. |
| [CurrentPages](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/currentpages-property/llms-full.txt) | Returns the number of pages, the last page to print. |

The programming category [Printing and Print Preview](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/printing/llms-full.txt) lists all members for printing.

### Related Pages

- [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/llms-full.txt): the *TXWords* sample with its print dialog box and print preview.
- [Text Formatting and Views](https://docs.textcontrol.com/tx-text-control-activex/document-editor/viewing/llms-full.txt): the page view and page sizes in twips.
- [Using Text Frames](https://docs.textcontrol.com/tx-text-control-activex/document-editor/text-frames/llms-full.txt): a label printing program built from text frames.
- [Printing and Print Preview](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/printing/llms-full.txt): all members for printing.

---

## Nearby

- **Previous:** [XML Basics](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/basics/llms-full.txt)
- **Next:** [Character and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-activex/document-editor/formatting/llms-full.txt)
