在delphi中打印到非默认打印机 [英] Print to a non default printer in delphi

查看:112
本文介绍了在delphi中打印到非默认打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Delphi将打印作业发送到打印机.理想情况下,我希望能够做到这一点而无需用户从打印对话框中选择打印机.

I would like to send a print job to a printer using Delphi. Ideally I would like to be able to do this without having the user select the printer from a print dialog.

我希望能够使用Windows默认打印机以外的打印机.

I would like to be able to use printers other than the windows default printer.

我尝试通过打印机名称设置打印机:

I have tried setting the printer by the printer name :

Vcl.Printers.Printer.PrinterIndex := Vcl.Printers.Printer.Printers.IndexOf('My Printer Name');

但是,当我打印时,它会恢复为使用默认打印机

However when I print, it reverts to using the default printer

推荐答案

传递给 IndexOf 的名称必须与 Printer.Printers 完全匹配.代码>才能正常工作.如果它们不是确切(包括CASE),则 IndexOf 将返回 -1 ,表示使用默认打印机".

The name passed to IndexOf must match exactly with what's in Printer.Printers in order to work. If they're not exact, including CASE, IndexOf will return -1, which means "use the default printer".

对于特定示例,如果实际打印机名称为 HP LaserJet hp laserjet 5 .

For a specific example, using IndexOf('hp laserjet') will return -1 if the actual printer name is HP LaserJet or hp laserjet 5.

如果您未指定确切名称,则可以通过迭代列表进行部分匹配.典型的系统极不可能有太多可用的打印机来提高效率.我们有几十个,很好.

If you're not specifying the exact name, you can do a partial match by iterating the list. It's highly unlikely that the typical system has too many printers available for this to be efficient; we have a couple of dozen, and it's fine.

这是我们的情况:我们的办公室分为三个基本部门(财务,管理和客户服务).每家公司都有一台不同的打印机,上面装有针送纸(点矩阵)标签,但是我们有在所有部门中运行的应用程序.不必让应用程序知道正在运行哪个部门来选择标签打印机,我们只给打印机名称包含单词 Labels - Fiscal Labels Admin标签等.然后我们可以找到带有循环的合适打印机:

Here's the situation we have: Our office is divided into three basic departments (Fiscal, Admin, and Customer Service). Each has a different printer that holds pin-feed (dot matrix) labels, but we have apps that run in all departments. Instead of having the application know which department it's being run in to choose the label printer, we just give the printers names containing the word Labels - Fiscal Labels, Admin Labels, etc. We can then find the appropriate printer with a loop:

function GetLabelPrinterIndex: Integer;
var
  i: Integer;
begin
  for i := 0 to Printer.Printers.Count - 1 do
    if AnsiContainsText(Printer.Printers[i], `Labels`) then
    begin
      Exit(i);
    end;
  Result := -1;
end;

请注意:我将从您的引用中删除 VCL 前缀;这意味着您的代码将无法在所有平台上使用.如果仅确保在子句中使用 Printers ,则可以仅使用 Printers.Printer ,并更改目标平台(VCL Win32/64,FMX 32/64,OSX)将根据构建配置为您调整uses子句.

As a note: I'd remove the VCL prefix from your references; it means your code won't be available across platforms. If you just make sure that Printers is in your uses clause, you can use just Printers.Printer, and changing the target platform (VCL Win32/64, FMX 32/64, OSX) will adjust the uses clause for you based on the build configuration.

这篇关于在delphi中打印到非默认打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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