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

Lo siguiente que se debe hacer para ser capaz de utilizar las impresoras es:

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

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

Esto es simple y straight forward tal como se muestra a continuación:

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

Utilizando el objeto Printer existente

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