通过将原始数据发送到打印机以 Arial 字体或任何其他字体进行打印
private void printfunction(string cmd)
{
string command = cmd;
// Create a buffer with the command
Byte[] buffer = new byte[command.Length];
buffer = System.Text.Encoding.ASCII.GetBytes(command);
// Use the CreateFile external functo connect to the LPT1 port
SafeFileHandle printer = CreateFile("LPT1:", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
// Aqui verifico se a impressora é válida
if (printer.IsInvalid == true)
{
MessageBox.Show("Printer not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Open the filestream to the lpt1 port and send the command
FileStream lpt1 = new FileStream(printer, FileAccess.ReadWrite);
lpt1.Write(buffer, 0, buffer.Length);
// Close the FileStream connection
lpt1.Close();
}
我一直在使用上面的代码函数将原始数据发送到支持 ESC/POS 的 EPSON TM88III 打印机。
默认情况下,我在打印机中只发送了 3 种字体。但我不想用 ARIAL 字体打印。如何以 Arial 字体打印。
请不要建议我使用 Windows 打印后台处理程序或打印机驱动程序。我想通过发送原始数据进行打印。
我们怎样才能做到这一点?
编码是使用 Visual Studio 2008 在 C#.NET 中完成的。
private void printfunction(string cmd)
{
string command = cmd;
// Create a buffer with the command
Byte[] buffer = new byte[command.Length];
buffer = System.Text.Encoding.ASCII.GetBytes(command);
// Use the CreateFile external functo connect to the LPT1 port
SafeFileHandle printer = CreateFile("LPT1:", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
// Aqui verifico se a impressora é válida
if (printer.IsInvalid == true)
{
MessageBox.Show("Printer not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Open the filestream to the lpt1 port and send the command
FileStream lpt1 = new FileStream(printer, FileAccess.ReadWrite);
lpt1.Write(buffer, 0, buffer.Length);
// Close the FileStream connection
lpt1.Close();
}
I've been using the code function above to send raw data to my ESC/POS supported EPSON TM88III printer.
I've only 3 sent of fonts by default in printer. But I wan't to print in ARIAL FONT. How can we print in Arial font.
Please don't suggest me to use windows print spooler or printer driver. I want to print by sending raw data.
How can we do this?
The coding is done in C#.NET using Visual Studio 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过将打印机置于图形模式并发送像素数据,这在技术上是可能的。您必须在程序中创建单色位图,Bitmap 和 Graphics 类可以完成这项工作。您可以将 Graphics.DrawText 与使用 Arial 初始化的 Font 一起使用,以按照您想要的方式获取文本。将位图像素编码为打印机命令是非常重要的部分,请确保有一本合适的打印机编程手册。
否则,这正是打印机驱动程序所做的事情。速度也会一样慢。
This is technically possible by putting the printer in graphics mode and sending pixel data. You'll have to create a monochrome bitmap in your program, the Bitmap and Graphics classes can get the job done. You'd use Graphics.DrawText with a Font initialized with Arial to get the text the way you want it. Encoding the bitmap pixels into printer commands is the non-trivial part, be sure to have a decent programming manual for the printer.
This is otherwise exactly what the printer driver does. It will be just as slow.
据我所知,TM88 的 Windows 驱动程序只是将打印输出作为位图发送到打印机,因为 TM88 本身不支持除固定宽度文本、条形码和位图以外的任何内容。
您可以使用转义码在衬线字体和无衬线字体之间切换,但它们都是固定宽度的。
As far as I know, the TM88's windows driver simply sends the print output as a bitmap to the printer, because the TM88 does not natively support anything more than fixed-width text, barcodes, and bitmaps.
You can use escape codes to switch between serif and sans-serif fonts, but they will both be fixed-width.
那是不可能的。按照惯例,原始文本使用等宽字体(例如 Courier)打印。
如果您只需要它与您的特定打印机型号配合使用,您可以尝试使用 Postscript,但您的打印机不太可能安装 Arial; Helvetica 的可能性更大。
That cannot be done. Raw text is, by convention, printed using a monospace font (such as Courier).
If you only need it to work with your particular printer model, you could try using Postscript, but it is unlikely that your printer will have Arial installed; Helvetica is more likely.
根据您的打印机,您应该使用其他代码。
这个关于 EPSON ESC/P 的维基百科条目解释了很多。
将“ESC k 1”(ESC =代码27)发送到打印机,将其设置为Sans Serif字体。但我认为这不成正比。
如果您确实想使用自己的字体。您可以将其(黑/白)渲染为图像/位图对象。将打印机置于图形模式,然后逐像素打印。 (实际上您按 8 或 9 像素打印)。
Depending on your printer, you should use other codes.
This wikipededia entry on the EPSON ESC/P explains a lot.
Sending "ESC k 1" (ESC = code 27) to the printer, puts it in Sans Serif font. But I don't think it is proportional.
If you really want to use your own font. You could render it (in black/white) to an Image/Bitmap object. Put the printer in graphics mode, and then print pixel by pixel. (Actually you print per 8 or 9 pixels).