TPrinter

Fonte: source/classes/printer.prg

TPrinter is FiveWin's printer device context class. It wraps the Windows GDI printing API, providing methods for text output, lines, boxes, bitmaps, barcodes, charts, and more. TPrinter supports multiple coordinate systems (pixels, centimeters, millimeters, inches), print preview with metafile recording, and PDF output via multiple engines (I2PDF, HaruPDF, FWPDF).

Key DATA Members

DATATypeDescription
hDCNumericPrinter device context handle
hDCOutNumericOutput DC (may be metafile DC for preview)
oFontTFontCurrent font object
cDocumentCharacterDocument/job name
cModelCharacterPrinter model name
cFileCharacterOutput file (for PDF)
nPageNumericCurrent page number
nXOffsetNumericX offset for output
nYOffsetNumericY offset for output
nPadNumericText padding mode (PAD_LEFT, PAD_RIGHT, PAD_CENTER)
nOrientNumericPage orientation
lMetaLogicalUsing metafile recording (for preview)
lPreviewLogicalPrint preview mode
lStartedLogicalWhether printing has started
aMetaArrayArray of metafile handles (one per page)
oPreviewObjectPreview window object

Class DATA (PDF engines)

CLASSDATADefaultDescription
lUseI2PDF.F.Use I2PDF engine for PDF output
lUseHaruPDF.F.Use HaruPDF engine for PDF output
lUseFWPDF.F.Use FiveWin's built-in PDF engine
lNativeWord.F.Use native Word output

Paper Sizes (DMPAPER_*)

ConstantValueSize
DMPAPER_LETTER18.5 x 11 in
DMPAPER_LEGAL58.5 x 14 in
DMPAPER_EXECUTIVE77.25 x 10.5 in
DMPAPER_A38297 x 420 mm
DMPAPER_A49210 x 297 mm
DMPAPER_A511148 x 210 mm
DMPAPER_B412250 x 354 mm
DMPAPER_B513182 x 257 mm
DMPAPER_FOLIO148.5 x 13 in
DMPAPER_TABLOID311 x 17 in
DMPAPER_ENV_10204.125 x 9.5 in
DMPAPER_ENV_DL27110 x 220 mm
DMPAPER_ENV_C528162 x 229 mm

Paper Bins (DMBIN_*)

ConstantValueDescription
DMBIN_UPPER1Upper / Only One tray
DMBIN_LOWER2Lower tray
DMBIN_MIDDLE3Middle tray
DMBIN_MANUAL4Manual feed
DMBIN_ENVELOPE5Envelope feeder
DMBIN_AUTO7Auto-select
DMBIN_CASSETTE14Cassette

Orientations

ConstantValue
DMORIENT_PORTRAIT1
DMORIENT_LANDSCAPE2

Key Methods

Constructor & Lifecycle

MethodDescription
New( cDocument, lUser, lPreview, cModel, lModal, lSelection, cFile )Create printer. lUser=show dialog, lPreview=preview mode
StartPage()Begin a new page
EndPage()End the current page
End()End printing, close spool job
Preview()Show print preview window

Text Output

MethodDescription
Say( nRow, nCol, cText, oFont, nWidth, nClrText, nBkMode, nPad, cUnits )Print text at position (pixels)
CmSay( nRow, nCol, cText, oFont, nWidth, ... )Print text using centimeters
MmSay( nRow, nCol, cText, oFont, nWidth, ... )Print text using millimeters
InchSay( nRow, nCol, cText, oFont, nWidth, ... )Print text using inches
SayText( nRow, nCol, cText, nWidth, nHeight, oFont, cAlign, ... )Print text with alignment and optional outline
DrawText( cText, aRect, nFlags )Print text in a rectangle with flags

Graphics

MethodDescription
Line( nTop, nLeft, nBottom, nRight, oPen, cUnits )Draw a line
Box( nTop, nLeft, nBottom, nRight, anoPen, noBrush, aText, cUnits )Draw a rectangle
RoundBox( ... )Draw a rounded rectangle
Ellipse( nTop, nLeft, nBottom, nRight, ... )Draw an ellipse
Arc( nTop, nLeft, nBottom, nRight, ... )Draw an arc
AngleArc( x, y, r, angle, sweep, ... )Draw an angle arc
CmLine( nRow, nCol, nBottom, nRight, oPen )Line in centimeters
CmBox( nRow, nCol, nBottom, nRight, oPen )Box in centimeters

Images

MethodDescription
SayBitmap( nRow, nCol, xBitmap, nWidth, nHeight, nRaster, cUnits )Print a bitmap
PrintImage( nRow, nCol, uImage, nWidth, nHeight, lStretch, nAlpha, lTransp, lGray, cUnits, cAlign )Print any image format
PrintBarcode( cText, nRow, nCol, nWidth, nHeight, cUnits, cType, ... )Print a barcode
PieChart( aRect, aValues, aColors, aPen, ... )Print a pie chart

Page & Coordinate Setup

MethodDescription
SetPage( nPage )Set paper size (DMPAPER_* constant)
SetBin( nBin )Set paper bin (DMBIN_* constant)
SetLandscape()Switch to landscape orientation
SetPortrait()Switch to portrait orientation
SetCopies( nCopies )Set number of copies
SetSize( nWidth, nHeight )Set custom paper size
PageWidth( cUnits )Get page width
PageHeight( cUnits )Get page height
GetTextWidth( cText, oFont, cUnits )Measure text width
GetTextHeight( cText, oFont, cUnits )Measure text height

Printing Commands

PRINT oPrn [ NAME cDocName ] [ PREVIEW ] [ FILE cFile ] [ TO cPrinter ]

PAGE

   // ... printing commands here ...

ENDPAGE

ENDPRINT

Example: Simple Report

#include "FiveWin.ch"

function Main()

   local oPrn, oFont

   PRINT oPrn NAME "Invoice" PREVIEW

   DEFINE FONT oFont NAME "Arial" SIZE 0, -14 OF oPrn

   PAGE

      // Header
      oPrn:Say( 100, 100, "INVOICE #1001", oFont )
      oPrn:Say( 200, 100, "Date: " + DToC( Date() ), oFont )

      // Line separator
      oPrn:Line( 280, 100, 280, 2000 )

      // Column headers
      oPrn:Say( 320, 100,  "Item", oFont )
      oPrn:Say( 320, 800,  "Qty", oFont )
      oPrn:Say( 320, 1200, "Price", oFont )
      oPrn:Say( 320, 1600, "Total", oFont )

      oPrn:Line( 380, 100, 380, 2000 )

      // Data rows
      oPrn:Say( 420, 100,  "Widget A", oFont )
      oPrn:Say( 420, 800,  "10", oFont )
      oPrn:Say( 420, 1200, "$5.00", oFont )
      oPrn:Say( 420, 1600, "$50.00", oFont )

      oPrn:Say( 500, 100,  "Widget B", oFont )
      oPrn:Say( 500, 800,  "5", oFont )
      oPrn:Say( 500, 1200, "$12.00", oFont )
      oPrn:Say( 500, 1600, "$60.00", oFont )

      // Total
      oPrn:Line( 580, 100, 580, 2000 )
      oPrn:Say( 620, 1200, "TOTAL:", oFont )
      oPrn:Say( 620, 1600, "$110.00", oFont )

   ENDPAGE

   ENDPRINT

   oFont:End()

return nil

Example: Centimeter Coordinates

#include "FiveWin.ch"

function Main()

   local oPrn, oFont

   PRINT oPrn NAME "CM Report" PREVIEW

   DEFINE FONT oFont NAME "Calibri" SIZE 0, -12 OF oPrn

   PAGE

      // Using centimeters (easier for layout)
      oPrn:CmSay( 1.0, 1.0, "Report Title", oFont )
      oPrn:CmLine( 1.5, 1.0, 1.5, 18.0 )

      oPrn:CmBox( 2.0, 1.0, 5.0, 18.0 )

      oPrn:CmSay( 2.5, 1.5, "This text is at 2.5cm down, 1.5cm from left", oFont )
      oPrn:CmSay( 3.0, 1.5, "Page size: " + ;
         LTrim( Str( oPrn:CmHorzPhySize() ) ) + " x " + ;
         LTrim( Str( oPrn:CmVertPhySize() ) ) + " cm", oFont )

   ENDPAGE

   ENDPRINT

   oFont:End()

return nil

Example: Graphics and Images

#include "FiveWin.ch"

function Main()

   local oPrn, oFont, oPen

   PRINT oPrn NAME "Graphics Demo" PREVIEW

   DEFINE FONT oFont NAME "Arial" SIZE 0, -16 OF oPrn
   DEFINE PEN oPen WIDTH 3

   PAGE

      // Boxes
      oPrn:Box( 100, 100, 400, 600, oPen )
      oPrn:RoundBox( 100, 700, 400, 1200, 30, 30, oPen )

      // Ellipse
      oPrn:Ellipse( 500, 100, 800, 600 )

      // Image
      oPrn:PrintImage( 500, 700, "logo.png", 500, 300, .T. )

      // Barcode
      oPrn:PrintBarcode( "1234567890", 900, 100, 600, 200, , "EAN13" )

      // Pie chart
      oPrn:PieChart( { 1200, 100, 1800, 700 }, ;
         { 30, 45, 25 }, ;
         { CLR_RED, CLR_GREEN, CLR_BLUE } )

   ENDPAGE

   ENDPRINT

   oFont:End()
   oPen:End()

return nil

Example: Direct PDF Output

#include "FiveWin.ch"

function Main()

   local oPrn, oFont

   // Output directly to PDF file
   PRINT oPrn NAME "My Document" FILE "output.pdf"

   DEFINE FONT oFont NAME "Arial" SIZE 0, -14 OF oPrn

   PAGE
      oPrn:Say( 100, 100, "This goes directly to PDF", oFont )
   ENDPAGE

   ENDPRINT

   oFont:End()

   // Open the PDF
   ShellExecute( 0, "open", "output.pdf" )

return nil

Coordinate System Methods

MethodDescription
Cmtr2Pix( @nRow, @nCol )Convert centimeters to pixels (by reference)
Mmtr2Pix( @nRow, @nCol )Convert millimeters to pixels
Inch2Pix( @nRow, @nCol )Convert inches to pixels
Pix2Cmtr( nRow, nCol )Convert pixels to centimeters
Pix2Mmtr( nRow, nCol )Convert pixels to millimeters
Pix2Inch( nRow, nCol )Convert pixels to inches

PDF Delegation Architecture

TPrinter supports transparent PDF output through a call-log recording and replay mechanism. When one of the PDF-related class data flags (listed above) is enabled, every drawing command is captured as a call-log entry and replayed through the selected PDF backend at page completion. The application code does not change at all — the same TPrinter:Say(), TPrinter:Line(), and other drawing calls produce PDF output without any code modifications.

The delegation architecture works as follows:

  1. The aCallLog array stores every drawing operation performed on the printer device context.
  2. AddLog() records each individual call — Say, Line, Box, Ellipse, PrintImage, etc. — along with its parameters.
  3. When a PDF flag is active, AddLog() captures each call during the page and EndPage() replays the accumulated entries through the selected PDF backend.
  4. The same mechanism works for print preview: the metafile recording (lMeta) captures GDI calls for on-screen rendering, while the PDF flags capture the same logical operations for file output.

This design ensures that any application using standard TPrinter drawing commands can produce PDF output simply by setting one class data flag at the start of the print job, with no changes to the drawing logic itself.

Veja Também