一、概述
在銀行,稅務,郵政等行業的實際工作中,經常涉及到在印刷好具有固定格式的匯款單,儲蓄憑證,稅票等單據上的確定位置打印輸出相關的信息。在此類需求中,精確地定位單據并打印相關信息,是解決問題]的關鍵。一般情況下,開發者都是通過在打印機上通過重復的測試來達到實際需求。那么,有沒有簡單有效而又靈活的方法實現上述功能呢?
二、基本思路
分析上述單據的特征,可以發現:此類打印輸出的信息一般比較簡短,不涉及到文字過長的折行處理,另外,其打印輸出的位置相對固定。因此,我們可以通過用尺子以毫米為單位,測量好每個輸出信息位置的橫向和縱向坐標,作為信息輸出的位置。但由于不同打印機在實際輸出效果上,總是存在理論和實際位置的偏差,因此,要求程序具有一定的靈活性,供最終用戶根據需要,進行必要的位置調整。因此,可設置一打印配置文件,用于存儲橫坐標和縱坐標的偏移量,用于用戶進行位置校正,從而提供了一定的靈活性。
三、精確打印輸出的程序實現
1. 在Delphi中新建一個名為mprint.pas的單元文件并編寫如下程序,單元引用中加入Printers略:
//取得字符的高度 function CharHeight: Word; var Metrics: TTextMetric; begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmHeight; end;
file://取得字符的平均寬度 function AvgCharWidth: Word; var Metrics: TTextMetric; begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmAveCharWidth; end;
file://取得紙張的物理尺寸---單位:點 function GetPhicalPaper: TPoint; var PageSize : TPoint; begin file://PageSize.X; 紙張物理寬度-單位:點 file://PageSize.Y; 紙張物理高度-單位:點 Escape(Printer.Handle, GETPHYSPAGESIZE, 0,nil,@PageSize); Result := PageSize; end;
file://2.取得紙張的邏輯寬度--可打印區域 file://取得紙張的邏輯尺寸 function PaperLogicSize: TPoint; var APoint: TPoint; begin APoint.X := Printer.PageWidth; APoint.Y := Printer.PageHeight; Result := APoint; end;
file://紙張水平對垂直方向的縱橫比例 function HVLogincRatio: Extended; var AP: TPoint; begin Ap := PaperLogicSize; Result := Ap.y/Ap.X; end;
file://取得紙張的橫向偏移量-單位:點 function GetOffSetX: Integer; begin Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetX); end;
file://取得紙張的縱向偏移量-單位:點 function GetOffSetY: Integer; begin Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetY); end;
file://毫米單位轉換為英寸單位 function MmToInch(Length: Extended): Extended; begin Result := Length/25.4; end;
file://英寸單位轉換為毫米單位 function InchToMm(Length: Extended): Extended; begin Result := Length*25.4; end;
file://取得水平方向每英寸打印機的點數 function HPointsPerInch: Integer; begin Result := GetDeviceCaps(Printer.Handle, LOGPIXELSX); end;
file://取得縱向方向每英寸打印機的光柵數 function VPointsPerInch: Integer; begin Result := GetDeviceCaps(Printer.Handle, LOGPIXELSY) end;
file://橫向點單位轉換為毫米單位 function XPointToMm(Pos: Integer): Extended; begin Result := Pos*25.4/HPointsPerInch; end;
file://縱向點單位轉換為毫米單位 function YPointToMm(Pos: Integer): Extended; begin Result := Pos*25.4/VPointsPerInch; end;
file://設置紙張高度-單位:mm procedure 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; begin file://自定義紙張最小高度127mm if Value < 127 then Value := 127; file://自定義紙張最大高度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://設置紙張寬度:單位--mm Procedure SetPaperWidth(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; begin file://自定義紙張最小寬度76mm if Value < 76 then Value := 76; file://自定義紙張最大寬度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://將毫米單位轉換為0.1mm單位 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://在 (Xmm, Ymm)處按指定配置文件信息和字體輸出字符串 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; begin file://打開配置文件,讀出橫向和縱向偏移量 try Fn := TStringList.Create; FileName := ExtractFilePath(Application.ExeName) + ConfigFileName; if FileExists(FileName) then begin Fn.LoadFromFile(FileName); file://橫向偏移量 OffSetX := StrToInt(Fn.Values['X']); file://縱向偏移量 OffSetY := StrToInt(Fn.Values['Y']); end else begin file://如果沒有配置文件,則生成 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.4) / 10000); Py := Round(Round(Y * VPointsPerInch * 10000/25.4) / 10000); Py := Py - GetOffSetY; file://因為是絕對坐標, 因此, 不用換算成相對于Y軸坐標 Px := Px + 2 * AvgCharWidth; Printer.Canvas.Font.Name := '宋體'; Printer.Canvas.Font.Size := FontSize; file://Printer.Canvas.Font.Color := clGreen; Printer.Canvas.TextOut(Px, Py, Txt); end;
2. 使用舉例
在主窗體中加入對mprint單元的引用,在一命令鈕的OnClick事件中書寫如下代碼(用于在郵政匯款單上的相應方框內打印郵政編碼843300):
Printer.BeginDoc; PrintText(16, 14, '8', 'config.txt'); PrintText(26, 14, '4', 'config.txt'); PrintText(36, 14, '3', 'config.txt'); PrintText(46, 14, '3', 'config.txt'); PrintText(56, 14, '0', 'config.txt'); PrintText(66, 14, '0', 'config.txt'); Printer.EndDoc;
觀察結果,用尺子測量偏移量,在config.txt文件中修改X,Y的值即可。
其它,設置打印機和紙張類型從略。
四、結束語
筆者通過該方法,實現了郵政匯款單,儲蓄憑證,客戶信封等單據的精確打印,取得了較為滿意的效果。該程序在Windows98,Delphi5下調試通過。
|