Using the printer/es

From Free Pascal wiki
Jump to navigationJump to search

Introducción

La impresión es fácil en FreePascal, pero solamente después de seguir algunos pasos requeridos, una vez que se comprenden estos primeros pasos ya se estaría en condiciones de pasar a conceptos más avanzados de impresión. Este artículo trata dichos pasos, que han sido recogidos de varios foros y ejemplos. Después de explicar los pasos básicos se realizará la impresión de unos textos para a continuación plantear sugerencias sobre impresión avanzada.


Los pasos básicos

The following you must do to be able to use printers.

  1. Add the Printer4Lazarus package to your project requirements.
  2. Add the Printers unit to the uses section of your unit.
  3. Use the existing Printer object.

Adding the Printer4Lazarus package to your project requirements

The Printer4Lazarus package defines a basic printer and provides platform independent printing. The following can thus be used on various platforms.

In the Lazarus IDE, do the following:

  1. In the Project menu, click Project Inspector. A window is shown with a tree view, one of the branches is named Required Packages. By default, the Required Packages branch shows the LCL package.
  2. Click the Add button, the button with the plus sign at the top of the window.
  3. Open the New Requirements page.
  4. From the Package Name list box, select Printer4Lazarus.
  5. Click OK.
  6. Printer4Lazarus is now shown in the Required Packages branch.

Adding the Printers unit to the uses section of your unit

This is simple and straight forward and could look like this:

unit MainUnt;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, Forms, Printers;

Using the existing Printer object

Assuming you want to click a button to print a text. On you form you put a button called PrintBtn and in the OnClick event you can now use the following:

procedure TForm1.PrintBtnClick(Sender: TObject);
const
  LEFTMARGIN = 100;
  HEADLINE = 'I Printed My Very First Text On ';
var
  YPos, LineHeight, VerticalMargin: Integer;
  SuccessString: String;
begin
  with Printer do
  try
    BeginDoc;
    Canvas.Font.Name := 'Courier New';
    Canvas.Font.Size := 10;
    Canvas.Font.Color := clBlack;
    LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));
    VerticalMargin := 4 * LineHeight;
    // There we go
    YPos := VerticalMargin;
    SuccessString := HEADLINE + DateTimeToStr(Now);   
    Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);
  finally
    EndDoc;
  end;
end;

Did I write basic and simple. The above example is somewhat complex. Next to the basic text output in the bold line, it also provides an example of formatting your text.

From begin to end; the following happens.

  • You can use the Printer object directly, without your own variable such as MyPrinter. So, the MyPrinter object is not really needed, it is just the way how I want to write my code.
  • With MyPrinter.BeginDoc you start printing, however nothing is sent to the printer until you finish with MyPrinter.EndDoc;.
  • The printer uses a Canvas to draw the output on. It is this drawing that ends up on the page. Canvas.Font is the font object for that moment. That is, the TextOut call we use later will output text using the settings of the font object of that moment.
  • Everything you draw on the canvas must be positioned using coordinates. So, we calculate a LineHeight to position text vertically. You could do the same for the horizontal position, which I left here to be LEFTMARGIN.
  • The text is drawn with the TextOut call.
  • This magnificent result is sent to the printer by MyPrinter.EndDoc.

In some forums it is suggested that the use of PrintDialog (the printer selection dialog) is required for good functioning, but I did not find this to be true (any more).