Delphi's precise printing 2001-11-08 · · Chen Liping ·· Yesky 1, outlined in the actual work of banks, taxes, postal and other industries, often involving remittance orders, savings documents with fixed formats in printing, Determine the location of the location print output related information on the document such as tax ticket. In this type of demand, it is the key to the accurate positioning documents and print information, which is the key to solve the problem. In general, developers have achieved actual needs by repeated testing on printers. So, is there a simple and effective way to achieve the above features? Second, the basic idea is analyzed by the characteristics of the above documents, which can be found that the information of such printouts is generally relatively short, and does not involve the excessive folding process, in addition, the position of its printout is relatively fixed. Therefore, we can measure the horizontal and longitudinal coordinates of each output information location by using a ruler, as the position of the information output. However, due to the difference between the actual output effect, there is always a deviation of theoretical and actual positions, and therefore, the program is required to have a certain flexibility for the end user to make the necessary positional adjustment as needed. Thus, a print profile can be set, and the offset of the horizontal coordinates and the ordinate, for the user to perform position correction, thereby providing a certain flexibility. Third, the program implementation of the precise printout output 1. Create a new unit file called MPRINT.PAS in Delphi and prepare the following programs, and add Printers slightly in the unit reference:
// Get a character height function CharHeight: Word; var Metrics: TTextMetric; begin GetTextMetrics (Printer.Canvas.Handle, Metrics); Result: = Metrics.tmHeight; end; file: // function AvgCharWidth obtain the average width of the characters: Word Var Metrics: TtextMetric; Begin getTextMetrics (Printer.canvas.Handle, Metrics); Result: = metrics.tmavecharWidth; End; File: // Get the physical size of the paper --- Unit: Point Function GetPhicalpaper: Tpoint; var Pagesize: TPOINT; Begin File: //pageSize.x; Paper Physical Width - Unit: Point File: //pagesize.y; Paper Physical Height - Unit: Point Escape (Printer.Handle, Getphyspagesize, 0, Nil, @ Pagesize); Result : = PageSize; End; File: // 2. Number logical width - Printable area file: // Take the logic size of the paper Function PaperLogicsize: Tpoint; var apoint: tpoint; begin apoint.x: = printer.pagewidth Apoint.y: = printer.pageheight; result: = apoint; end; file: // Paper horizontal to vertical horizontal proportion Function Hvlogincratio: Extended; var AP: tpoint; begin ap: = paperlogicsize; result: = ap. Y / ap.x; end; file: // Take the transverse offset of the paper - Unit: Point Function GetOffsetx: Integer; Begin Result: = getDeviceCaps (Printer.Handle, PhysicalOffSetx); End; File: // Take paper Longitudinal offset - Units: Point Function GetOffSensty: Integ Er; Begin Result: = getDeviceCaps (Printer.Handle, PhysicalOffSety); End; File: // mm unit Convert to inch unit function mmToinch (length: Extended): Extended; begin result: = length / 25.4; end; file: / / Inch units are converted to mm unit function inchtomm (length: out): extended; begin result: = length * 25.4; end; file: // acquired points in the horizontal direction Function HPointSperinch: Integer; Begin Result: = GetDegercaps Printer.Handle, Logpixelsx); End; File: // Number of grating VpointSperinch: Integer; Begin Result: = getDegercaps (Printer.Handle, Logpixelsy) End; File: // Transverse point Millimeter Function XpointTomm (POS: Integer): Extended; Begin Result: = POS * 25.4 / HPointSperinch; End; File: // Longitudinal Point Units Convert to mm Unit Function YpointTomm (POS: Integer): Extended;
Begin Result: = POS * 25.4 / vpointsperinch; end; file: // Set paper height - unit: MMProcedure setPaperHeight (Value: integer); var Device: array [0..255] of char; driver: array [0 .. 255] of char; port: array [0..255] of char; hdmode: thandle; pdmode: pdevmode; beginfile: // Custom Paper minimum height 127mmif value <127 Then value: = 127; file: // Custom paper maximum height 432mm if Value> 432 then Value: = 432; Printer.PrinterIndex: = Printer.PrinterIndex; Printer.GetPrinter (Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode: = GlobalLock (hDMode) ; if pDMode <> nil then begin pDMode ^ .dmFields: = pDMode ^ .dmFields or DM_PAPERSIZE or DM_PAPERLENGTH; pDMode ^ .dmPaperSize: = DMPAPER_USER; pDMode ^ .dmPaperLength: = Value * 10; pDMode ^ .dmFields: = pDMode ^. dmFields or DMBIN_MANUAL; pDMode ^ .dmDefaultSource: = DMBIN_MANUAL; GlobalUnlock (hDMode); end; end; Printer.PrinterIndex: = Printer.PrinterIndex; end; file: // set paper width: unit --mmProcedure SetPaperWidth (Value: integer) Var device: array [0..255] Of char; port: array [0..255] of char; hdmode: thandle; pdmode: pdevmode; beginfile: // Custom Paper minimum width 76mmif value <76 Then Value : = 76; File: // Custom Paper Maximum Width 216mm IF Value> 216 Then Value: = 216; Printer.printerIndex: = Printer.printerIndex; Printer.getPrinter (Device, Driver, Port, HDMode; if HDMode <> 0 then begin pDMode: = GlobalLock (hDMode); if pDMode <> nil then begin pDMode ^ .dmFields: = pDMode ^ .dmFields or DM_PAPERSIZE or DM_PAPERWIDTH; pDMode ^ .dmPaperSize: = DMPAPER_USER; file: // convert the millimeter 0.1mm unit pdmode ^ .dmpaperWidth: = value * 10; pdmode ^ .dmfields: =
pDMode ^ .dmFields or DMBIN_MANUAL; pDMode ^ .dmDefaultSource: = DMBIN_MANUAL; GlobalUnlock (hDMode); end; end; Printer.PrinterIndex: = Printer.PrinterIndex; end; file: // in (Xmm, Ymm) at a specified profile Information and font output string Procedure PrintText (x, y: extended; txt: string; configfilename: string; fontsize: integer = 12; var orx, ory: extended; px, py: integer; ap: tpoint; fn: tstrings ; FileName: string; offSetX, OffSetY: Integer; beginfile: // open the configuration file, the read horizontal and vertical offsets try Fn: = TStringList.Create; FileName: = ExtractFilePath (Application.ExeName) configFileName; if FileExists ( FileName) The begin fn.loadFromfile (file: // lateral offset OFFSETX: = STRTOINT (FN.VALUES ['x']); file: // Longitudinal offset OFFSETY: = STRTOINT (fn.values ['Y']); endelsebegin file: // If there is no configuration file, generate fn.values ['x']: = '0'; fn.values ['y']: = '0'; fn.savetofile (Filename); end; finally fn.free; end; x: = x offsetx; y: = y offsety; px: = round (Round (x * hpointsperinch * 10000/25) / 10000); py: = round (Round (Y * VpointSperinch * 10000 / 25.4) / 10000); py: = py - getoff SETY; File: // Because it is absolute coordinates, there is no need to convert to the Y-axis coordinate PX: = PX 2 * avgcharwidth; printer.canvas.font.name: = 'Song body'; Printer.canvas.Font.Size : = FONTSIZE; FILE: //printer.canvas.font.color: = CLGreen; Printer.canvas.TextOut (PX, PY, TXT); END; 2. Using the example to add a reference to the MPRINT unit in the main form, the following code is written in the onclick event of a command button (used to print the Postal Code 843300 in the corresponding square box on the postal remittance order):