Recent

Author Topic: print text  (Read 2970 times)

hamza

  • Jr. Member
  • **
  • Posts: 52
print text
« on: March 25, 2020, 05:45:05 pm »
hi
can you support me how to print the text using thermal printer  ??? i have win10 with lazarus free pascal , usb port.
any example??
thank

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: print text
« Reply #1 on: March 25, 2020, 07:41:26 pm »
Provided that your printer uses the normal driver mechanism of your operating system, preparations for printing shouldn't be different from the procedures for other printers. Did you read https://wiki.lazarus.freepascal.org/Using_the_printer?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

hamza

  • Jr. Member
  • **
  • Posts: 52
Re: print text
« Reply #2 on: March 25, 2020, 09:07:43 pm »
thank you its working now..

but how to print another 3 lines ?? as x, y position on the paper??

hamza

  • Jr. Member
  • **
  • Posts: 52
Re: print text
« Reply #3 on: March 26, 2020, 05:01:01 pm »
hi how can I draw the rectangular around the whole text???

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: print text
« Reply #4 on: March 27, 2020, 06:46:56 pm »
thank you its working now..

but how to print another 3 lines ?? as x, y position on the paper??

In order to make this more straightforward I have defined for my apps the procedures PrinterWrite and PrinterWriteln, somewhat similar to the traditional Pascal write and writeln procedures. PrinterWriteln automatically updates the position to begin in a new line.

Code: Pascal  [Select][+][-]
  1. procedure PrinterWriteln(H: integer; var currentX, currentY: integer;
  2.   theString: string; bold: boolean);
  3. begin
  4.   if bold then
  5.     Printer.Canvas.Font.Style := [fsBold]
  6.   else
  7.     Printer.Canvas.Font.Style := [];
  8.   Printer.Canvas.TextOut(currentX, currentY, theString);
  9.   Inc(currentY, H);
  10. end;
  11.  
  12. procedure PrinterWrite(var currentX, currentY: integer;
  13.   theString: string; bold: boolean);
  14. begin
  15.   if bold then
  16.     Printer.Canvas.Font.Style := [fsBold]
  17.   else
  18.     Printer.Canvas.Font.Style := [];
  19.   Printer.Canvas.TextOut(currentX, currentY, theString);
  20. end;
  21.  

currentX and currentY have to be defined by the calling procedure, function or method:

Code: Pascal  [Select][+][-]
  1. var
  2.   gTopMargin, gBottomMargin, gLeftMargin, gRightMargin: double; // these globals might as well be replaced by local or instance variables
  3.   gLineSpacing: integer;
  4.  
  5. procedure TestProcedure;
  6. var
  7.   H, ADPI, marginX, marginXr, currentX, currentY, lastY, returnPos, lastPos: integer;
  8.   resultLine, remainder: string;
  9. begin
  10.     gTopMargin := 2;
  11.     gLeftMargin := 2;
  12.     gRightMargin := 2;
  13.     gBottomMargin := 2;
  14.     gLineSpacing := 2;
  15.     ADPI := Printer.YDPI;
  16.     currentY := GetPoints(gTopMargin, ADPI);
  17.     marginX := GetPoints(gLeftMargin, ADPI);
  18.     marginXr := GetPoints(gRightMargin, ADPI) div 2;
  19.     Printer.Title := 'Test page';
  20.     currentX := marginX;
  21.     Printer.BeginDoc;
  22.     try
  23.       Printer.Canvas.Font.Name := gPreferences.PrintFont;
  24.       Printer.Canvas.Font.Size := 9;
  25.       Printer.Canvas.Font.Style := [];
  26.       Printer.Canvas.Pen.Color := clBlack;
  27.       Printer.Canvas.Pen.Width := 2;
  28.       H := (Printer.Canvas.TextHeight('X') + gLineSpacing);
  29.       PrinterWriteln(H, currentX, currentY, 'Caption', True);
  30.       PrinterWriteln(H, currentX, currentY, 'Line #1', False);
  31.       PrinterWriteln(H, currentX, currentY, 'Line #2', False);
  32.       PrinterWriteln(H, currentX, currentY, 'Line #3', False);
  33.       Printer.EndDoc;
  34.       caseIDBarCode.Destroy;
  35.     except
  36.       on E: Exception do
  37.       begin
  38.         Printer.Abort;
  39.         raise;
  40.       end;
  41.     end;
  42. end;
  43.  
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: print text
« Reply #5 on: March 27, 2020, 06:54:10 pm »
hi how can I draw the rectangular around the whole text???

The printer uses a canvas that works very similar to the canvas of a window on screen. Therefore, most methods for drawing on the screen (e.g. drawing rectangles) also work with printers. See https://wiki.freepascal.org/Drawing_with_canvas for details.

If you use my example from above you might want to use either gTopMargin, gBottomMargin, gLeftMargin and gRightMargin or currentX and currentY (or a combination of these variables) to get optimal coordinates of your rectangle.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018