直接将字符串打印到打印机 [英] print a string directly to printer

查看:120
本文介绍了直接将字符串打印到打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要直接在打印机中打印一个字符串

i通过搜索找到了此代码

i need to print a string directly into the printer
i found this code by searching

uses WinSpool, Printers

type
  TDoc_Info_1 = record
    pDocName: pChar;
    pOutputFile: pChar;
    pDataType: pChar;
  end;


procedure PrintSimpleText(sPrinter, sText: String);
var
  sTitle: String;
  hPrinter: THandle;
  PrnDocInfo: TDoc_Info_1;
  lst: TStringList;
  i: Integer;
  n: Cardinal;
  sTextLine: String;
  bFound: Boolean;
begin
  lst := TStringList.Create;
  try
    lst.Text := sText; //with CRLF
    //new doc
    sTitle := 'Raw print';
    ZeroMemory(@PrnDocInfo, SizeOf(TDoc_Info_1));
    PrnDocInfo.pDocName := PChar(sTitle);
    PrnDocInfo.pDataType := 'RAW';
    //find printer (if is installed in windows)
    bFound := False;
    for i:=1 to Printer.Printers.Count do
    begin
      if Pos(sPrinter, Printer.Printers.Strings[i-1])>0 then
      begin
        bFound := True;
        sPrinter := Printer.Printers.Strings[i-1];
        Printer.PrinterIndex := i-1; //set printer
        Break;
      end;
    end;

    if bFound then
    begin
      // open the printer
      if OpenPrinter(PChar(sPrinter), hPrinter, nil) then
      begin
        //start
        StartDocPrinter(hPrinter, 1, @PrnDocInfo);
        if StartPagePrinter(hPrinter) then
        begin
          //print by line
          for i := 1 to lst.Count do
          begin
            sTextLine := lst.Strings[i-1];
            if not WritePrinter(hPrinter, PChar(sTextLine), Length(sTextLine), n) then
              Break;
          end;
          //end of page
          EndPagePrinter(hPrinter);
          //end
          EndDocPrinter(hPrinter);
        end;
        ClosePrinter(hPrinter);
      end;
    end;
  finally
    lst.Free;
  end;
end;

运行如下:

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintSimpleText('pdfFactory Pro', 'Tis is a'#13#10'text');
  showmessage('aaaa');
end;

1)
,但是单击 Button1 它只是显示一条消息!
是否需要发送带有打印字符串的自定义标头?
还是这里的问题?

1) but by clicking Button1 it just show a message!!! is it required to send a custom header with the string for print? or what is the problem here?

2)
如果您认为这不是一个好方法,请告诉我一个更好的解决方案!我需要向打印机提交这样的字符串

2) also if you think this is not a good way tell me a better solution! i need to submit a string like this to the printer

------------------------------------------------------
      your card number is 1111 1111 1111 1111
           your name is mr xxxx xxxxxxx
        your nationality code is 9999999999
------------------------------------------------------
              your password is : 555555
-----------------------------------------------------

最初,我尝试将字符串保存到文本文件中并将其发送给Pronter,但打印机在文件顶部打印了文件名。文件

at first i tried to save string into a text file and send it to pronter but printer printed the file name at the top of the file

然后我尝试创建位图图像并将其发送到机器
,但是打印机是点矩阵并且不理解该图像!

then i tried to create a bitmap image and send it to the machine but the printer is a dot matrix and don't understand the image!!

更新:

此代码在我的PC上可以正常运行
i认为已检测到打印机且工作正常

this code work perfectly on my pc i think printer is detected and working fine.

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
ShellExecute(Handle, 'print', PChar(OpenDialog1.FileName), nil, nil, SW_HIDE) ;
end;


推荐答案

这也可以完成您的工作

  Printer.BeginDoc;
  Printer.Canvas.TextOut(0,0,'Place any text here');
  Printer.EndDoc;

也可以使用画布编辑样式

also with canvas you can edit styling too

   Printer.Canvas.Font.size:=18;
   Printer.Canvas.Font.style := [fsbold];

这篇关于直接将字符串打印到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆