TX Text Control ActiveX Documentation

Printing

Delphi provides a printer object that can be used to print the contents of a Text Control.

The following example sends the contents of a Text Control, which can be several pages long, to the default printer:

Example Code

procedure TForm1.Print1Click(Sender: TObject);
var wPages, No : Integer;
begin
   wPages := TXTextControl1.CurrentPages;
   Printer.BeginDoc;
   for No := 1 To wPages do begin
      TXTextControl1.PrintDevice := Printer.Canvas.Handle;
      TXTextControl1.PrintPage(No);
      if No <> wPages then
         Printer.NewPage;
   end;
   Printer.EndDoc;
end;

After storing the number of pages in a local variable called wPages, the printer object is initialized with the Printer.BeginDoc statement. The For .. do loop runs from 1 to wPages to print all of the pages. Inside the loop there are three lines of code which print a single page:

  • The device context handle of the printer object is assigned to Text Control's PrintDevice property. Without this step, a device context which is compatible to the screen device would be used, resulting in poor print quality.
  • The number of the page to be printed is passed as a parameter to the PrintPage method. This will also start the printing process.
  • The printer object's NewPage method is invoked to advance to the next page if there is one left.

Everything else, like calculating the line and page breaks, is done internally by Text Control. The formatting is based on the values of two groups of properties:

These properties are normally set in a page setup dialog box.

 
 
 

Products

Support

Downloads

Corporate

Buy Now