Difference between revisions of "Using the printer/es"

From Free Pascal wiki
Jump to navigationJump to search
Line 5: Line 5:
  
 
==Los pasos básicos==
 
==Los pasos básicos==
 +
The following you must do to be able to use printers.
 +
# Add the Printer4Lazarus package to your project requirements.
 +
# Add the Printers unit to the uses section of your unit.
 +
# Use the existing Printer object.
  
Lo siguiente que se debe hacer para ser capaz de utilizar las impresoras es:
+
===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.
# Añadir el paquete '''Printer4Lazarus''' a los requerimientos del programa.
 
# Añadir la unit '''Printers''' a la sección '''uses''' en la unidad.
 
# 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:
 
  
 +
In the Lazarus IDE, do the following:
 
# 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.
 
# 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.
 
# Click the '''Add''' button, the button with the plus sign at the top of the window.
 
# Click the '''Add''' button, the button with the plus sign at the top of the window.
Line 25: Line 21:
 
# ''Printer4Lazarus'' is now shown in the ''Required Packages'' branch.
 
# ''Printer4Lazarus'' is now shown in the ''Required Packages'' branch.
  
===Añadiendo la unidad '''Printers''' a la sección '''uses''' de tu unidad===
+
===Adding the Printers unit to the uses section of your unit===
Esto es simple y straight forward tal como se muestra a continuación:
+
This is simple and straight forward and could look like this:
 
 
 
<syntaxhighlight>unit MainUnt;
 
<syntaxhighlight>unit MainUnt;
 
   
 
   
Line 37: Line 32:
 
   Classes, SysUtils, Forms, Printers;</syntaxhighlight>
 
   Classes, SysUtils, Forms, Printers;</syntaxhighlight>
  
===Utilizando el objeto '''Printer''' existente===
+
===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:
 
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:
  

Revision as of 12:02, 13 March 2013

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