Using the printer/es

From Free Pascal wiki
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

Se necesita seguir los siguientes pasos para poder utilizar las impresoras:

  1. Añadir el paquete Printer4Lazarus a los requerimientos del programa.
  2. Añadir la unit Printers a la sección uses en la unidad.
  3. Utilizar el objeto existente Printer.

Añadiendo el paquete Printer4Lazarus a los requerimientos del proyecto

El paquete Printer4Lazarus define una impresora básica y provee de un sistema de impresión independiente de la plataforma utilizada. Lo siguiente puede utilizarse por tanto en varias plataformas:

En el IDE de Lazarus, realizar lo siguiente:

  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. Hacer click en el pulsador Añadir, es el que tiene dibujado el signo +.
  3. Abre la solapa Nuevo requerimiento.
  4. En el cuadro de listado Nombre de Paquete selecciona Printer4Lazarus.
  5. Haz click en Crear Nuevo Requisito.
  6. Con esto ya se muestra Printer4Lazarus en la rama de Paquetes Requeridos.

Añadiendo la unidad Printers a la sección uses section de tu unidad

Esto es simple y fácil de hacer, pareciéndo el resultado a esto:

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).