过长邮寄地址折行打印技术
在邮政详情单打印过程中,经常遇到一个这样的问题,就是地址过长,无法在一行打印完成,需要分成两行或多行来打印。而分行打印,不能简单地进行字符的截取,这里涉及到汉字的截取会出现半个汉字导致的乱码问题,还有一个就是地址中会有数字,而数字大多数是门牌号等,我们不想把这样的数字分成两半来打印。
为此,设计了一个函数,作用是把这地址格式化。需要两个参数,一个是地址字符串,另外一个是一行需要打印的字符长度。
function strWrap1(const ostr: widestring; const mint: integer): string;
var
i: integer;
nstr, tmp: widestring;
tmpstr, tmps: string;
function isbd(tmp: widestring): boolean;
begin
if pos(tmp, ', 。 ;0123456789#') > 0 then
result := true
else
result := false;
end;
begin
nstr := '';
tmpstr := '';
for i := 1 to length(ostr) do begin
tmp := copy(ostr, i, 1);
if (tmp = #10) or (tmp = #13) then continue;
tmps := tmp;
if (length(tmpstr) >= mint ) then begin//字符数够了
if (length(tmpstr) = mint ) and (length(tmps) = 1) then begin //字母
if isbd(tmp) then begin
nstr := nstr + tmp
end
else begin
nstr := nstr + '?' +tmp;
tmpstr:=''; //复位
end;
end else begin
tmpstr := tmp;
if isbd(tmp) then begin //
nstr := nstr + '?' + tmp ;
tmpstr := ''; //复位
end else //非句号
nstr := nstr + '?' + tmp;
end;
end else begin
tmpstr := tmpstr + tmp;
nstr := nstr + tmp;
end;
end;
result := nstr + '?';
end;
在自己的实际打印程序中,先是调用该函数来格式化该地址,再循环打印每一行。
…
toaddr := strwrap1(toaddr,20);
h:=0;
while pos('?',toaddr)>0 do
begin
tmptoaddr := copy(toaddr,1,pos('?',toaddr)-1);
delete(toaddr,1,pos('?',toaddr)); //打印后删除
PrintText(X,Y + h, tmptoaddr ,'PInitPoint.txt',12);
h := h +5; //每行之间的距离
end;
…