C#中的点阵打印?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:42:44  来源:igfitidea点击:

Dot Matrix printing in C#?

c#printingxps

提问by Dale

I'm trying to print to Dot Matrix printers (various models) out of C#, currently I'm using Win32 API (you can find alot of examples online) calls to send escape codes directly to the printer out of my C# application. This works great, but...

我正在尝试使用 C# 打印到点阵打印机(各种型号),目前我正在使用 Win32 API(您可以在网上找到很多示例)调用将转义码直接发送到我的 C# 应用程序中的打印机。这很好用,但是...

My problem is because I'm generating the escape codes and not relying on the windows print system the printouts can't be sent to any "normal" printers or to things like PDF print drivers. (This is now causing a problem as we're trying to use the application on a 2008 Terminal Server using Easy Print [Which is XPS based])

我的问题是因为我正在生成转义码并且不依赖于 Windows 打印系统,所以打印输出无法发送到任何“普通”打印机或 PDF 打印驱动程序之类的东西。(这现在导致了一个问题,因为我们正在尝试使用 Easy Print [基于 XPS] 在 2008 终端服务器上使用该应用程序)

The question is: How can I print formatted documents (invoices on pre-printed stationary) to Dot Matrix printers (Epson, Oki and Panasonic... various models) out of C# not using direct printing, escape codes etc.

问题是:如何在不使用直接打印、转义码等的情况下,使用 C# 将格式化的文档(预印文具上的发票)打印到点阵打印机(爱普生、Oki 和松下……各种型号)。

**Just to clarify, I'm trying things like GDI+ (System.Drawing.Printing) but the problem is that its very hard, to get things to line up like the old code did. (The old code sent the characters direct to the printer bypassing the windows driver.) Any suggestions how things could be improved so that they could use GDI+ but still line up like the old code did?

** 只是为了澄清,我正在尝试 GDI+ (System.Drawing.Printing) 之类的东西,但问题是它很难像旧代码那样排列。(旧代码绕过 Windows 驱动程序将字符直接发送到打印机。)有什么建议可以改进事情,以便他们可以使用 GDI+ 但仍然像旧代码一样排列?

采纳答案by Renaud Bompuis

You should probably use a reporting tool to make templates that allow you or users to correctly position the fields with regards to the pre-printed stationery.

您可能应该使用报告工具来制作模板,使您或用户可以正确定位与预印信纸相关的字段。

Using dot-matrix printers, you basically have to work in either of 2 modes:

使用点阵打印机,您基本上必须以两种模式之一工作:

  • simple type-writer mode of line/column text where you send escape sequences to manage a small number of fonts that are included in the printer hardware and have to manage line returns, etc.
  • graphic output where the page is rasterized and the printer driver just drives the print head and pins to output the dots.
  • 行/列文本的简单打字机模式,您可以在其中发送转义序列来管理打印机硬件中包含的少量字体,并且必须管理换行等。
  • 图形输出,其中页面被光栅化,打印机驱动程序只驱动打印头和引脚输出点。

The first usage is mostly deprecated under Windows as it does not offer much in the way of controlling the output, and each printer having its own characteristics it becomes unwieldy and difficult for the software to predict and position things on the page (no WYSIWYG).

第一种用法在 Windows 下大多被弃用,因为它没有提供太多控制输出的方式,而且每台打印机都有自己的特性,软件在页面上预测和定位内容变得笨拙和困难(没有所见即所得)。

The second just uses a graphic page paradigm that makes positioning text and graphics independent of the actual capabilities of the printer.
When using pre-printed stationery, your job s to correctly position the data on the page.
Doing this by hand is resource-consuming and creating the layout in code is certainly not recommended since you'll get stuck with code to change should your printer, page format or printed stationery change.

第二种只是使用图形页面范例,使定位文本和图形独立于打印机的实际功能。
使用预印信纸时,您的作业需要正确定位页面上的数据。
手动执行此操作会消耗资源,并且当然不建议在代码中创建布局,因为如果您的打印机、页面格式或打印的信纸发生变化,您将不得不修改代码。

The best is to just use the standard printing model offered by .Net and a reporting tool that allows you to define models and templates where the correct text and graphics will be positioned, and then drive this from code.

最好的办法是只使用 .Net 提供的标准打印模型和报告工具,该工具允许您定义模型和模板,其中将放置正确的文本和图形,然后从代码中驱动它。

Visual Studio is shipped with a version of Crystal Reports but there are other, better reporting systems (I use the one from developer expressfor instance), some of them are even free.

Visual Studio 附带一个 Crystal Reports 版本,但还有其他更好的报告系统(例如,我使用developer express的一个),其中一些甚至是免费的

回答by Tim Robinson

Take a look at the System.Drawing.Printingnamespace.

查看System.Drawing.Printing命名空间。

回答by Arvo

If your printer has driver to install, then you can use normal Windows print system. Most printers, including POS ones, have working Windows drivers available. (Most dot-matrix printers are Epson compatible anyway.) Some POS printer drivers allow send escape codes directly to printer too (using special fonts); probably you don't need such functionality.

如果您的打印机需要安装驱动程序,那么您就可以使用普通的 Windows 打印系统。大多数打印机,包括 POS 打印机,都有可用的 Windows 驱动程序。(无论如何,大多数点阵打印机都与 Epson 兼容。)一些 POS 打印机驱动程序也允许将转义码直接发送到打印机(使用特殊字体);可能你不需要这样的功能。

If this is not the case, then you can add Generic/Text Only printer (driver) with help of Add Printer Wizard. Once done, you can configure certain commands (escape sequences) for it - Font size 10/12/17, Bold on/off, Underline on/off, job start/stop, paper feed and size select.

如果不是这种情况,那么您可以在添加打印机向导的帮助下添加通用/纯文本打印机(驱动程序)。完成后,您可以为其配置某些命令(转义序列) - 字体大小 10/12/17、粗体开/关、下划线开/关、作业开始/停止、进纸和尺寸选择。

I'm using Generic printer associated with FILE port to test various reports for POS printers - it's easy to look at text file to validate numbers in printout. Of course for formatting specific printer driver is needed.

我正在使用与 FILE 端口关联的通用打印机来测试 POS 打印机的各种报告 - 查看文本文件以验证打印输出中的数字很容易。当然,格式化需要特定的打印机驱动程序。

回答by Dale

It appears that what I would like to do is not possible.

看来我想做的事情是不可能的。

My choices for printing are unmanaged direct to printer printing (using winspool.drv) which allows me to do whatever I like to the printer and allows me to easily line things up.VB.NET ExampleOr I can use GDI+ (System.Drawing.Printing) which is complicated to get things to line up but will work with non-dot matrix printers like XPS and PDF printers.

我的打印选择是不受管理的直接打印到打印机(使用 winspool.drv),这使我可以对打印机执行任何我喜欢的操作,并允许我轻松地将事情对齐。VB.NET 示例或者我可以使用 GDI+(System.Drawing.Printing),这很复杂,但可以与 XPS 和 PDF 打印机等非点阵打印机一起使用。

回答by alexandrul

From my experience, it is easier to use two kinds of reports for the same data:

根据我的经验,对于相同的数据使用两种报告更容易:

  • one report for dot matrix printers using escape codes and anything else is required, which is saved in a text file and then printed using various methods (type file.txt > lpt1or selecting in code the default printer and using NOTEPAD /P file.txt) - see this pagefor more printing methods.
  • another report for laser/inkjet printers using a report builder tool (Crystal Reports, Report Manager, RLIBor anything available)
  • 使用转义码的点阵打印机的一份报告以及需要其他任何内容的报告,将其保存在文本文件中,然后使用各种方法打印(type file.txt > lpt1或在代码中选择默认打印机并使用NOTEPAD /P file.txt) - 请参阅此页面了解更多打印方法。
  • 使用报告生成器工具(Crystal Reports、Report ManagerRLIB或任何可用的工具)的激光/喷墨打印机的另一份报告

Since it is not uncommon to buy the right kind of printer for the right kind of report, this approach has the advantage of letting the customer decide: dot matrix printer for text reports in A3/A4 paper format (usually for the accounting department) or laser/inkjet printer for graphical reports.

由于为正确的报告购买正确类型的打印机并不少见,因此这种方法的优势在于让客户决定:A3/A4 纸张格式的文本报告的点阵打印机(通常用于会计部门)或用于图形报告的激光/喷墨打印机。

回答by alexandrul

i don't know how to use Escape sequence in C#. But i have all Escape Sequence for Generic / Text Only printer. Hope it helps.

我不知道如何在 C# 中使用转义序列。但我有通用/纯文本打印机的所有转义序列。希望能帮助到你。

Generic Print Escape Sequence 1) Set Line Spacing a) 1/8 inch - 27,48 b) 1/6 inch - 27,50

通用打印转义序列 1) 设置行距 a) 1/8 英寸 - 27,48 b) 1/6 英寸 - 27,50

2) Select Draft Quality a) 27,120,0 / 27,120,48

2) 选择草稿质量 a) 27,120,0 / 27,120,48

3) Letter Quality a) 27,120,1 / 27,120,49

3) 信件质量 a) 27,120,1 / 27,120,49

4) Double Height a) 27,119,n i) n = 1 On ii) n = 0 Off

4) 双倍高度 a) 27,119,n i) n = 1 开 ii) n = 0 关

5) Bidirectional Printing a) 27,85,n i) 0 - Both Way ii) 1 - One Way

5) 双向打印 a) 27,85,n i) 0 - 双向 ii) 1 - 单向

6) Increase character space a) 27,32,n (Increase by n / 12 inch)

6) 增加字符空间 a) 27,32,n(增加 n / 12 英寸)

7) Select Bold Font a) 27,69

7) 选择粗体 a) 27,69

8) Cancel Bold Font a) 27,70

8) 取消粗体 a) 27,70

9) Select Italic Font a) 27,52

9) 选择斜体 a) 27,52

10) Cancel Italic Font a) 27,53

10) 取消斜体 a) 27,53

11) Select a) 10cpi 27,8 b) 12cpi 27,77 c) 15cpi 27,103 d) 18cpi 27,103

11) 选择 a) 10cpi 27,8 b) 12cpi 27,77 c) 15cpi 27,103 d) 18cpi 27,103

12) Set Right Margin a) 27,81,n

12) 设置右边距 a) 27,81,n

13) Set Left Margin a) 27,108,n

13) 设置左边距 a) 27,108,n

14) Form Feed a) 12

14) 表单馈送 a) 12

15) Condensed Printing a) 0F On b) 12 Off

15) 压缩打印 a) 0F 开 b) 12 关

16) Double Strike Printing a) 27,71

16) 双重打击印刷 a) 27,71

17) Cancel Strike Printing a) 27,72

17) 取消罢工印刷 a) 27,72

18) Under line a) 27,45,0 Off b) 27,45,1 On

18) 线下 a) 27,45,0 关闭 b) 27,45,1 开启

19) Double Width a) 27,84,0 Off b) 27,84,1 ON

19) 双倍宽度 a) 27,84,0 关 b) 27,84,1 开

回答by alexandrul

First Convert the Sequence commands into character then pass to printer

首先将序列命令转换为字符,然后传递给打印机

Example Bold Font 27,69

示例粗体 27,69

string.Format("{0}{1}",Convert.ToChar(27),Convert.ToChar(69));

string.Format("{0}{1}",Convert.ToChar(27),Convert.ToChar(69));

Perhaps a bit shorter as:

也许短一点:

string.Format("{0}{1}",(char)(27),(char)(69));

string.Format("{0}{1}",(char)(27),(char)(69));