将字符串直接打印到打印机

时间:2014-08-18 20:10:52

标签: delphi printing delphi-xe6

我需要将字符串直接打印到打印机中 我通过搜索找到了这段代码

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它只会显示一条消息! 是否需要发送带有打印字符串的自定义标题? 或者这里有什么问题?

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

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

起初我试图将字符串保存到文本文件并将其发送到内存但打印机打印文件顶部的文件名

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

更新:

此代码在我的电脑上完美运行 我认为打印机被检测到并且工作正常。

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

4 个答案:

答案 0 :(得分:5)

如何将一些文本直接写入默认打印机,如文本文件。

uses ..., Printers;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  lst: TextFile;
begin
  AssignPrn(lst);
  Rewrite(lst);
  try
    Writeln(lst, '      your card number is 1111 1111 1111 1111');
    Writeln(lst, '           your name is mr xxxx xxxxxxx');
    Writeln(lst, '        your nationality code is 9999999999');
    Writeln(lst, '------------------------------------------------------');
  finally
    CloseFile(lst);
  end;
end;

矩阵打印机特别有用。

答案 1 :(得分:3)

这也可以完成你的工作

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

也可以使用画布编辑样式

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

答案 2 :(得分:3)

如果打印机可通过网络使用:

procedure Print(const AText: string);
var
  F: TStreamWriter;
begin
  F := TStreamWriter.Create('\\printserver\printername', False, TEncoding.Default);
  try
    F.Write(AText);
  finally
    F.Free;
  end;
end;

(使用Delphi 2009测试)

答案 3 :(得分:0)

当需要直接向打印机发送命令时,可以使用“打印机”类。 要编写一个字符串,请使用以下代码:

procedure TForm1.btTextoNormalClick(Sender: TObject);
begin

  ComandoAnsiString := 'Example text';

  if not OpenPrinter(PChar(driverName), HandleImp, nil) then 
    Memo1.Lines.Add('Erro: Impressora não encontrada')

  else
  begin

  Documento.pDocName := PChar('Minha impressão');
  Documento.pOutputFile := nil;
  Documento.pDataType := 'RAW';


  StartDocPrinter(HandleImp, 1, @Documento);

  StartPagePrinter(HandleImp);


  WritePrinter(HandleImp, PAnsiChar(ComandoAnsiString), Length(ComandoAnsiString), CaracteresImpressos);

  EndPagePrinter(HandleImp);
  EndDocPrinter(HandleImp);
  ClosePrinter(HandleImp);


  end;

end;

要获取driverName,请使用ComboBox中的代码:

cbbDriver.Items := Printer.Printers;