我的打印全用EHLIB实现,它可以直接打印DBGRID和套打,下面是我套下的实现,希望对你有帮助:
function TFormFsBkd.PrintOut(VPrinter: TVirtualPrinter): Boolean;
var
SysManager: TIniFile;
PageWidth,PageHeight,Orientation:Integer;
NoTop,NoLeft,RQToP,RQLeft,BKBMTop,BKBMLeft:Integer;
BKDWQCTop,BKDWQCLeft,BKDWYHZHTop,BKDWYHZHLeft,BKDWKHYHTop,BKDWKHYHLeft:Integer;
SKDWQCTop,SKDWQCLeft,SKDWYHZHTop,SKDWYHZHLeft,SKDWKHYHTop,SKDWKHYHLeft:Integer;
DXJETop,DXJELeft,XXJETop,XXJELeft:Integer;
YTTop,YTLeft,ZCXMTop,ZCXMLeft:Integer;
DWMMTop,DWMCLeft:integer;
DWMCString:string;
pointx,pointy:Real;//每毫米多少点
PrintRq:string;
begin
SysManager :=TIniFile.Create(ExtractFilePath(Application.ExeName)+'SysManager.InI');
PageWidth:=SysManager.ReadInteger('FsBkd', 'PageWidth', 0);
PageHeight:=SysManager.ReadInteger('FsBkd', 'PageHeight',0);
Orientation:=SysManager.ReadInteger('FsBkd', 'Orientation',0);
PrintSetup(VPrinter,PageWidth,PageHeight,Orientation);
//获取打印机信息通常分辨率是指一英寸的点数,1英寸=2.54厘米
//取得每一英寸的点数,分x与y
if VPrinter.Printers.Count > 0 then
begin
pointx := GetDeviceCaps(VPrinter.Handle, LOGPIXELSX);
pointy := GetDeviceCaps(VPrinter.Handle, LOGPIXELSY);
end else
begin
pointx:= DefaultPrinterPixelsPerInchX;
pointy := DefaultPrinterPixelsPerInchY;
end;
//计算每毫米多少点
pointx := pointx / 25.4;
pointy := pointy / 25.4;
DWMMTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'DWMMTop', 0));
DWMCLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'DWMCLeft',0));
DWMCString:=SysManager.ReadString('FsBkd', 'DWMCString','');
NoTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'NoTop', 0));
NoLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'NoLeft',0));
RQTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'RQToP',0));
RQLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'RQLeft',0));
BKBMTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'BKBMTop',0));
BKBMLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'BKBMLeft',0));
BKDWQCTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', ' BKDWQCTop',0));
BKDWQCLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'BKDWQCLeft',0));
BKDWYHZHTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'BKDWYHZHTop',0));
BKDWYHZHLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'BKDWYHZHLeft',0));
BKDWKHYHTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'BKDWKHYHTop',0));
BKDWKHYHLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'BKDWKHYHLeft',0));
SKDWQCTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', ' SKDWQCTop',0));
SKDWQCLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'SKDWQCLeft',0));
SKDWYHZHTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'SKDWYHZHTop',0));
SKDWYHZHLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'SKDWYHZHLeft',0));
SKDWKHYHTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'SKDWKHYHTop',0));
SKDWKHYHLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'SKDWKHYHLeft',0));
DXJETop:=trunc(pointx*SysManager.ReadInteger('FsBkd', ' DXJETop',0));
DXJELeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'DXJELeft',0));
XXJETop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'XXJETop',0));
XXJELeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'XXJELeft',0));
YTTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'YTTop',0));
YTLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'YTLeft',0));
ZCXMTop:=trunc(pointx*SysManager.ReadInteger('FsBkd', 'ZCXMTop',0));
ZCXMLeft:=trunc(pointy*SysManager.ReadInteger('FsBkd', 'ZCXMLeft',0));
SysManager.Free;
//VPrinter.NewPage;
VPrinter.begin
Doc;
VPrinter.Canvas.Font.Name := '宋体';
VPrinter.Canvas.Font.Size := 11;
VPrinter.Canvas.TextOut(DWMCLeft,DWMMTop,Trim(DWMCString));
VPrinter.Canvas.TextOut(NoLeft,NoTop,Trim(EditLine_DJH.Edit1.Text));
PrintRq:=LeftStr(Trim(EditLine_RQ.EditDate.Text),4)+'年'+copy(Trim(EditLine_RQ.EditDate.Text),6,2)+'月'+RightStr(Trim(EditLine_RQ.EditDate.Text),2)+'日';
VPrinter.Canvas.TextOut(RQLeft,RQTop,Trim(PrintRq));
//VPrinter.Canvas.TextOut(BKBMLeft,BKBMTop,Trim(EditLine_BM.Text));
VPrinter.Canvas.TextOut(BKDWQCLeft,BKDWQCTop,Trim(EditLine_QC.Text));
VPrinter.Canvas.TextOut(BKDWYHZHLeft,BKDWYHZHTop,Trim(EditLine_YHZH.Edit1.Text));
VPrinter.Canvas.TextOut(BKDWKHYHLeft,BKDWKHYHTop,Trim(EditLine_KHYH.Edit1.Text));
VPrinter.Canvas.TextOut(SKDWQCLeft,SKDWQCTop,Trim(EditLine_SKQC.Text));
VPrinter.Canvas.TextOut(SKDWYHZHLeft,SKDWYHZHTop,Trim(EditLine_SKYHZH.Edit1.Text));
VPrinter.Canvas.TextOut(SKDWKHYHLeft,SKDWKHYHTop,Trim(EditLine_SKKHYH.Edit1.Text));
VPrinter.Canvas.TextOut(DXJELeft,DXJETop,Trim(EditLine_DXJE.Edit1.Text));
VPrinter.Canvas.TextOut(XXJELeft,XXJETop,Trim(EditLine_XXJE.Text));
VPrinter.Canvas.TextOut(YTLeft,YTTop,Trim(EditLine_YT.Text));
VPrinter.Canvas.TextOut(ZCXMLeft,ZCXMTop,Trim(EditLine_ZCXM.Text));
Application.ProcessMessages;
if VPrinter.Aborted then
Exit;
VPrinter.EndDoc;
//VPrinter.NewPage;
end;