C# 在.net 中的点阵打印机上打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/449777/
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
print on dot matrix printer in .net
提问by
anyone can please tell me the code how to print documents on dot matrix printer in C# windows application.
任何人都可以告诉我如何在 C# windows 应用程序中的点阵打印机上打印文档的代码。
回答by aku
How is this different from printing on inkjet or laser printer?
这与在喷墨或激光打印机上打印有何不同?
Preview and Print from Your Windows Forms App with the .NET Printing Namespace
回答by Michael Buen
You can print to dot matrix "graphically", which is built-in in Windows, albeit slower.
您可以“以图形方式”打印到点阵,这是 Windows 内置的,尽管速度较慢。
But if you only want to print pure text with simple formattings, you need to send escape commands to your dot matrix printer, which is faster than graphical printing. Different printers has different escape commands.
但是如果你只想打印简单格式的纯文本,你需要向你的点阵打印机发送转义命令,这比图形打印要快。不同的打印机有不同的转义命令。
Here are typical escape commands(for epson): http://www.printfil.com/manualen/c5.htm
以下是典型的转义命令(用于 epson):http: //www.printfil.com/manualen/c5.htm
This might help: https://web.archive.org/web/20051212193242/http://sacpcug.org:80/archives/0306/prc0603.html
这可能会有所帮助:https: //web.archive.org/web/20051212193242/http: //sacpcug.org: 80/archives/0306/prc0603.html
What I do in VB6 then was to print to Generic / Text Onlyprinter, you open the PRN or LPT1 as a file handle, then print escape commands on the file handle, all escape commands will be redirected to whatever printer is attached to LPT1 or PRN. You can do the same thing with C#, just open the PRN or LPT1 as a file, then print to it.
我在 VB6 中所做的是打印到通用/纯文本打印机,您打开 PRN 或 LPT1 作为文件句柄,然后在文件句柄上打印转义命令,所有转义命令将被重定向到连接到 LPT1 的任何打印机或PRN。你可以用 C# 做同样的事情,只需将 PRN 或 LPT1 作为文件打开,然后打印到它。
To add Generic / Text Onlyprinter, Control Panel > Printers > Add Printer. On manufacturer, select Generic, then on printers, select Generic / Text only.
要添加通用/纯文本打印机,请选择控制面板 > 打印机 > 添加打印机。在制造商上,选择 Generic,然后在打印机上,选择 Generic / Text only。
You can do the same (printing on Generic / Text Only) for Zebra printers which have their own escape commands for printing bar codes, which is faster than letting Windows print to it graphically.
您可以对 Zebra 打印机执行相同的操作(在Generic / Text Only上打印),这些打印机具有用于打印条形码的自己的转义命令,这比让 Windows 以图形方式打印更快。
回答by Joel_MMCC
When you print to an inkjet or laser printer, you generally do not use built-in fonts of the printer. You use Windows fonts. What happens is that the printer driver either builds an image of pixels (including the text) and sends it to the printer, or sends commands designed to draw lines, spline curves, and other shapes to the printer, with fonts expressed as lines and splines (outlines).
当您使用喷墨或激光打印机打印时,您通常不会使用打印机的内置字体。您使用 Windows 字体。发生的情况是打印机驱动程序要么构建像素图像(包括文本)并将其发送到打印机,要么将用于绘制线条、样条曲线和其他形状的命令发送到打印机,字体以线条和样条表示(轮廓)。
When you print to an older dot matrix printer, you can do it that way, but it's slow. Each line of text has to be “built” from pixels, and often the lines of text do not match up with the passes of the print head (especially for fonts much larger or smaller than 12-point).
当您打印到较旧的点阵打印机时,您可以这样做,但速度很慢。每行文本都必须由像素“构建”,并且文本行通常与打印头的通道不匹配(尤其是对于大于或小于 12 点的字体)。
The old way of using these printers, the way they were intended, was to send the actual ASCII codes of the text to the printer. Send the number 65 (decimal), and you get a capital (upper-case) “A” for instance. The number 49 (decimal) would print the digit “1” while the number 32 would be a blank space, 33 an exclamation point (“!”), and so on. One byte = one character. The dot-matrix printer had its own built-in font, and would look the ASCII code up in its font ROM, and from there determine the exact timings of which print wires would have to strike the page exactly when to produce those letters.
使用这些打印机的旧方式,即它们的预期方式,是将文本的实际 ASCII 代码发送到打印机。例如,发送数字 65(十进制),您将得到一个大写(大写)“A”。数字 49(十进制)将打印数字“1”,而数字 32 将是一个空格,33 是一个感叹号(“!”),依此类推。一个字节 = 一个字符。点阵打印机有它自己的内置字体,会在它的字体 ROM 中查找 ASCII 代码,并从那里确定哪些打印线必须准确地敲击页面的确切时间,以及何时产生这些字母。
By using ESCape codes, you could specify such effects as pseudo-boldfaced (basically striking the letter twice, with the second copy shifted to the right by only one dot width), double-wide (striking each column of wires twice in a row for each one time that it would normally be struck, thus doubling the width of the letter), underline (striking the bottom print wire throughout regardless of whether the letter shape calls for it at that point or not), and so on.
通过使用 ESCape 代码,您可以指定伪粗体(基本上是两次敲击字母,第二个副本仅向右移动一个点宽)、双宽(连续敲击每列电线两次以每次它通常会被敲击,从而使字母的宽度加倍),下划线(无论字母形状在该点是否需要它,都始终敲击底部印刷线),等等。
The printer's own ROM handled all of these mechanical details about print wires and such. All your program had to supply is the actual ASCII codes of the text (including control codes such as the number 13 [Carriage Return aka CR] to return the print head to the left margin [or, for a bidirectional printer, prepare to print the next line in the reverse order of the previous line], usually followed by the number 10 [Line Feed aka LF] to roll the paper up one line to prepare for printing the next line).
打印机自己的 ROM 处理了所有这些关于打印线等的机械细节。您的程序必须提供的只是文本的实际 ASCII 代码(包括控制代码,例如数字 13 [Carriage Return aka CR] 以将打印头返回到左边距 [或者,对于双向打印机,准备打印下一行与上一行相反的顺序],通常后跟数字 10 [Line Feed aka LF] 将纸卷起一行以准备打印下一行)。
If you wanted to print in fancy fonts that the printer didn't have, or print graphics, you had to use an ESCape code to set the printer into “graphics mode” in which you basically sent bytes whose bits would specify to fire the individual wires of the printhead under direct program control, rather than looking up character shapes in the printer's Font ROM. When you print normally from Windows using a printer-specific driver, this is usually what happens.
如果你想用打印机没有的花哨字体打印,或者打印图形,你必须使用 ESCape 代码将打印机设置为“图形模式”,在这种模式下,你基本上发送字节,其位将指定触发个人直接程序控制下打印头的电线,而不是在打印机的字体 ROM 中查找字符形状。当您使用特定于打印机的驱动程序从 Windows 正常打印时,通常会发生这种情况。
For daisy-wheel or other fixed-character printers (e.g.IBM Selectric type-ball mechanisms), the ASCII code would spin the wheel or ball to the proper position and then strike the ribbon and thus print the letter on the page, or send the right hammer up to hit the ribbon and thus the page (TeleType or old typewriter mechanism). It was not possible to do pixel graphics with these except by printing repeated periods and micro-advancing the print head and paper the width/height of a period instead of a character / row of text, respectively (which would generally wear out the period character of the daisy-wheel or ball really quickly, so many of them had metal-reinforced periods for that very reason).
对于菊花轮或其他固定字符打印机(例如IBM Selectric 型球机制),ASCII 代码会将轮或球旋转到适当的位置,然后敲击色带,从而在页面上打印字母,或发送右锤敲击丝带,从而敲击页面(TeleType 或旧打字机机制)。除了通过重复打印句点和微调打印头和纸张的宽度/高度而不是字符/文本行(这通常会磨损句点字符)之外,不可能用这些来制作像素图形菊花轮或球的速度非常快,因此他们中的许多人都有金属加固时期)。
回答by peter_mcc
Whilst it wasn't in C#, I have written Access reports that used the native fonts of an Epson printer. It was a few year ago - using Windows XP - but when the printer was selected as the default it was possible to choose the "native" fonts of the printer via the font chooser.
虽然它不是在 C# 中,但我已经编写了使用 Epson 打印机的本机字体的 Access 报告。那是几年前 - 使用 Windows XP - 但是当打印机被选为默认值时,可以通过字体选择器选择打印机的“本机”字体。
It was pretty neat - I could use any font I liked for the headings, which were slow to print. Then I could select the native printer font for the detail rows, which were fast. Doing it that way I had to be careful that all the "native" font detail stauff had exactly the same vertical alignment, otherwise it became slow again.
它非常整洁——我可以使用任何我喜欢的字体作为标题,但打印速度很慢。然后我可以为细节行选择本地打印机字体,这很快。这样做时,我必须小心所有“原生”字体细节都具有完全相同的垂直对齐方式,否则它会再次变慢。