delphi中实现黑标单据的打印(原创)

news/2024/11/29 20:48:07/

对于很小的票据,打印起来很难套格式,连续打印时错位会很严重,这样我们可以用黑标检测来解决这个问题,当打印机打印完一张票据时就检测黑标,这样打印机将会停在检测到的黑标后面,打印一下张时将不会跑位。

黑标打印需要的条件:

1、打印机要支持黑标发票。打印说明书中可以查到。

2、行矩要跟据打印机来定,一般打印字体一样时,不同的打印机行矩会一样的。

下面是一个示例:

unit PrintHBUnit;

interface
            
uses SysUtils, IniFiles, windows, Forms, Dialogs, Classes;

//定义打印的记录类型

type
  TPrintObj = record
    Hbsbh: string;
    Dffsny: string;
    Khmc: string;
    Khdz: string;
    Dbcbh1: string;
    Jsbl1: string;
    Scbm1: string;
    Bcbm1: string;
    Dbcbh2: string;
    Jsbl2: string;
    Scbm2: string;
    Bcbm2: string;
    Dbcbh3: string;
    Jsbl3: string;
    Scbm3: string;
    Bcbm3: string;
    YDFLBH1: string;
    YDFLBH2: string;
    YDFLBH3: string;
    Fldl1: string;
    Fldl2: string;
    Fldl3: string;
    Fldj1: string;
    Fldj2: string;
    Fldj3: string;
    Shdl1: string;
    Shdl2: string;
    Shdl3: string;
    FlDF1: string;
    FlDF2: string;
    FlDF3: string;
    DDDF1: string;
    DDDF2: string;
    DDDF3: string;
    HjDL: string;
    HjDF: string;
    Scjy: string;
    Bcss: string;
    Bcxz: string;
    Bcjy: string;
    Ssdx: string;
    Cby: string;
    Sky: string;
    Fphm: string;
    Rq: string;
    sswyj: string;
    dyqdf : string;
    BlackMark: string;
    tbdf :string;
    dddf:String;//电度电费
    fldf:String;
  end;

var
  PrintObj: TPrintObj;
  MyFile: TIniFile;
  I:integer;//为1时代表已取电价,否则取电价
  mldjList:Tstrings;
  ydflbhList:Tstrings;

procedure SetPrintObj;
procedure pdrPrintObj(zs_bz:String='0');
procedure GetBlackMark;
function JeToDx(v_je: real): String;// 报表用的金额大写转换函数


implementation

uses   DB;


function JeToDx(v_je: real): String;// 报表用的金额大写转换函数
var
  l,i: integer;
  je_zs, je_xs, dx, dw, s, s1, dx_je: string;
  bFs:boolean;
begin
  If v_je=0 Then
  Begin
    Result:='零元整';
    Exit;
  End;
  if v_je<0 then
  begin
    bFs:=True;
    v_je:=(-1)*v_je;
  end
  else bFs:=False;

   je_zs:='';
   je_xs:='';
   if round(v_je)*100=v_je*100 then je_zs:=FloatToStr(v_je)
   else
   begin
      s:=FloatToStr(v_je*100);
      je_zs:=Copy(s,1,length(s)-2);
      je_xs:=Copy(s,length(je_zs)+1,2);
      if je_zs='' then je_zs:='00';
      if je_xs='' then je_xs:='00';
   end;
   dx:='零壹贰叁肆伍陆柒捌玖';
   dw:='元拾佰仟万拾佰仟亿';
   dx_je:='';
   if je_zs<>'' then
   begin
      l:=length(je_zs);
      s1:='';
      for i:=1 to l do
      begin
         s:=Copy(dx,StrToInt(Copy(je_zs,i,1))*2+1,2);
         if s<>'零' then
         begin
            dx_je:=dx_je+s;
            dx_je:=dx_je+Copy(dw,(l-i)*2+1,2);
         end
         else if s1<>'零' then dx_je:=dx_je+'零';
         if i=(l-4) then
         begin
            s:=Copy(dx_je,length(dx_je)-1,2);
            if s='零' then dx_je:=Copy(dx_je,1,length(dx_je)-2);
            if s<>'万' then dx_je:=dx_je+'万';
         end;
         s1:=s;
      end;
      s:=Copy(dx_je,length(dx_je)-1,2);
      if s='零' then dx_je:=Copy(dx_je,1,length(dx_je)-2);
      if s<>'元' then dx_je:=dx_je+'元';
   end;
   if je_xs='' then dx_je:=dx_je+'整'
   else
   begin
      if Copy(je_xs,1,1)<>'0' then
      begin
         if Copy(je_xs,2,1)<>'0' then
         begin
             if length(je_xs)=1 then je_xs:='0'+je_xs;
            dx_je:=dx_je+Copy(dx,StrToInt(Copy(je_xs,1,1))*2+1,2)+'角';
            dx_je:=dx_je+Copy(dx,StrToInt(Copy(je_xs,2,1))*2+1,2)+'分';
         end
         else dx_je:=dx_je+Copy(dx,StrToInt(Copy(je_xs,1,1))*2+1,2)+'角整';
      end
      else
      begin
         dx_je:=dx_je+'零'+Copy(dx,StrToInt(Copy(je_xs,2,1))*2+1,2)+'分';
      end;
   end;
   if copy(dx_je,1,2)='元' then dx_je:='零'+dx_je;
   if bFs then dx_je:='负'+dx_je;    
   Result:=dx_je;
end;

procedure GetBlackMark;
var
  BlackMark: string;
begin

//从INI文件中读取黑票类型
  MyFile := Tinifile.create(ExtractFilePath(Application.ExeName)+'ydgl.ini');
  try
    BlackMark := MyFile.ReadString('Print', 'PrintType', '');
    if Length(BlackMark) <> 0 then
      PrintObj.BlackMark := BlackMark
    else
    begin
      Application.MessageBox('已自动配置ydgl.ini文件,默认打印机类型为:BS-310K,请检查!', '提示', MB_OK);
      MyFile.WriteString('Print', 'PrintType', 'BS-310K');
    end;
  finally
    MyFile.Free;
  end;
end;

procedure SetPrintObj;
begin
  with PrintObj do
  begin
    Hbsbh := '0123456789';
    Dffsny := '200401';
    Khmc := '测试用户';
    Khdz := '南昌市';
    Dbcbh1 := 'A0001';
    Jsbl1 := '1';
    Scbm1 := '100';
    Bcbm1 := '150';
    Dbcbh2 := 'A0002';
    Jsbl2 := '1';
    Scbm2 := '10';
    Bcbm2 := '110';
    Dbcbh3 := '';
    Jsbl3 := '';
    Scbm3 := '';
    Bcbm3 := '';
    Fldl1 := '150';
    Fldl2 := '';
    Fldl3 := '';
    Fldj1 := '0.50';
    Fldj2 := '';
    Fldj3 := '';
    Shdl1 := '';
    Shdl2 := '';
    Shdl3 := '';
    FlDF1 := '75.00';
    FlDF2 := '';
    FlDF3 := '';
    DDDF1 := '75.00';
    DDDF2 := '';
    DDDF3 := '';
    HjDL := '150';
    HjDF := '75.00';
    Scjy := '0';
    Bcss := '100.00';
    Bcxz := '75.00';
    Bcjy := '25.00';
    Ssdx := '壹佰元整';
    Cby := '小王';
    Sky := '小李';
    Fphm := '001';
    Rq := '2004-01-30';
    sswyj:='0';
    dyqdf:='';
    dddf:='';
    fldf:='';
  end;
end;

procedure pdrPrintObj(zs_bz:String='0');
var
  lpt: TextFile;
  str,Other:String;
begin
  AssignFile(lpt, 'LPT1');
  try
    ReWrite(lpt);
    with PrintObj do
    begin
      BlackMark:='SP512MC';
      if UpperCase(BlackMark)=UpperCase('SP512MC') then
      begin
          Writeln(lpt, '');
          Writeln(lpt, Format('    %S', [sswyj]));
          Writeln(lpt, Format('    %S       %S', [Hbsbh, dffsny]));
          Writeln(lpt, Format('    %S', [Khmc]));
          Writeln(lpt, Format('    %S', [Khdz]));
          Writeln(lpt, Format('    %-11S%-11S%-11S', [Dbcbh1,Dbcbh2,Dbcbh3]));
          Writeln(lpt, Format('    %-11S%-11S%-11S', [Jsbl1,Jsbl2,Jsbl3]));
          Writeln(lpt, Format('    %-11S%-11S%-11S', [Scbm1,Scbm2,Scbm3]));
          Writeln(lpt, Format('    %-11S%-11S%-11S', [Bcbm1,Bcbm2,Bcbm3]));
          Writeln(lpt, '');
          Writeln(lpt, Format('%-11S%-11S%-11S%S', [Fldl1, Shdl1, Fldj1,
            Fldf1]));
          Writeln(lpt, Format('%-11S%-11S%-11S%S', [Fldl2, Shdl2, Fldj2,
            Fldf2]));
          Writeln(lpt, Format('%-11S%-11S%-11S%S', [Fldl3, Shdl3, Fldj3,
            Fldf3]));
          Writeln(lpt, Format('    %-20S%S', [Hjdl, Hjdf]));
          //zs_bz:走收标致"1"为走收,"0"非走收
          if zs_bz='0' then
          begin
            Writeln(lpt, Format('     %-20S%S', [Scjy, Bcss]));
            Writeln(lpt, Format('     %-20S%S', [Bcxz, Bcjy]));
            Writeln(lpt, Format('     %S', [Ssdx]));
          end
          else
          begin
            Writeln(lpt, Format('     %-20S%S', ['', Hjdf]));
            Writeln(lpt, Format('     %-20S%S', [Hjdf, '']));
            Writeln(lpt, Format('     %S', [JeToDx(StrToFloatDef(Hjdf,0))]));
          end;
          Writeln(lpt, Format('  %-15S%-15S%S', [Cby, Sky, Rq]));
          Writeln(lpt, '');
          Writeln(lpt, Format('       %S', [Fphm]));
          Writeln(lpt, Format('    %-24S%S', [Hbsbh, dffsny]));
          if zs_bz='0' then
          begin
            Writeln(lpt, Format('    %-24S%S', [Bcss, Bcjy]));
          end
          else
          begin
            Writeln(lpt, Format('    %-24S%S', [Hjdf+JeToDx(StrToFloatDef(Hjdf,0)), '']));
          end;
          Writeln(lpt, Format('     %-23S%S', [Sky, Rq]));

//发送黑标指令
          Write(lpt, #12);
      end
    end;
  finally
    CloseFile(lpt);
  end;
end;
end.


http://www.ppmy.cn/news/554859.html

相关文章

拍婚纱照-秘籍

和狗销售打交道前&#xff0c;都应该提前办理个保号手机卡&#xff08;防骚扰电话&#xff09; 如果他们要留电话或加微信,让他们添加备用手机和备用微信&#xff08;设置免打扰&#xff09;&#xff0c;不要接他们电话&#xff0c;不要主动和他们联系&#xff0c;吊着他们 &am…

SMARTFORM A5单据打印(自定义纸张/针式打印机)格式问题

一张A5横放的单据&#xff0c;做SmartForm时很自然的选择了DINA5&#xff0c;然后横放&#xff0c;测试的时候一直用激光打印机&#xff0c;感觉挺正常。实际使用时发现异常&#xff0c;不得己自己定义了一个A5横放的自定纸型&#xff08;210mmX148mm&#xff09;&#xff0c;打…

单据自动匹配,一键打印……你想要的回单解决方案,浪潮全都有

企业取得银行回单后&#xff0c;将其与对应的单据、凭证匹配粘贴&#xff0c;一直是企业财务人员非常头疼的工作之一。这项工作看起来简单&#xff0c;但实际上需要取回单、查找单据凭证、打印、粘贴、归档等多步琐碎的工作才能完成&#xff0c;如果企业交易频繁&#xff0c;工…

C#打印单据

HTML&#xff1a; <form id"form1"> <div id"t_border"> <!--startprint--> <div id"first"> <span id"title">****汽车服务连锁施工单</span> …

商品订单打印模板html,【EXCEL】如何制作订单单据查询与打印模板?

摘要: 昨天有个同事让村长帮忙设置几个公式 ,需求是这样子的,有一个订单的明细表(真实数据涉及私密,以下数据纯属我后来虚拟的)她需要根据这个订单明细做一个单据查询(打印)的模板,如下图(虚拟)填写单据号后自动带出客户名称、发货日期、发货仓库及发货的明细,其中根据单…

单据的自定义纸张打印

近日做一项目&#xff0c;需要有大量的报表和单据打印&#xff0c;以前做过使用水晶报表来打印的&#xff0c;感觉太过麻烦&#xff0c;效果也不理想&#xff0c;这次查找资料中&#xff0c;找到了蜡人张的报表http://www.cnblogs.com/waxdoll/category/49402.html&#xff0c;…

c# printdialog 打印html,C#利用PrintDocument定制打印单据的小例子

前言 本文是利用PrintDocument定制打印单据的小例子&#xff0c;仅供学习分享使用&#xff0c;如果不足之处&#xff0c;还请指正。 涉及知识点&#xff1a; PrintDocument :从 Windows 窗体应用程序打印时&#xff0c;定义一种可重用的可发送到打印机上的对象。 PrintPreviewC…

打印单据(PDF)

1、首先需要下载两个包&#xff0c;点击&#xff1a; iText-5.0.5与iTextAsian包 2、正文 package com.service;import java.io.File; import java.io.FileOutputStream;import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font…