gpt4 book ai didi

c# - 紧凑的框架套接字文档打印

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:44 25 4
gpt4 key购买 nike

我正在尝试从 Windows CE 设备将文档打印到网络激光打印机。因为我正在尝试打印示例文本,所以没有问题,而且我可以通过套接字进行打印,我的问题是我需要在文本下方打印一些文本和一个小图像,例如:

not formatted text
------------------
| IMAGE |
------------------
other not formatted text

我的第一个想法是构建一个 Rtf 文档并将其发送到打印机 trought sokets,但它显然打印文档文本内容而不是文本格式......我尝试了许多其他方法来编码要发送的数据但没有结果...有办法做到这一点,还是我只是在浪费时间?如果没有,有没有办法实现发送 PCL/PostScript 数据? (通常驱动程序是新激光打印机上基本命令的“标准”)。这是我用来在网络打印机上打印的功能:

public static string print_data_to_IP(string printerIP, int printerPort, byte[] myEncoded_bytes)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Not supported
//socket.NoDelay = true;

IPAddress ip = IPAddress.Parse(printerIP);
IPEndPoint ipEndPoint = new IPEndPoint(ip, printerPort);
socket.Connect(ipEndPoint);
socket.Send(myEncoded_bytes);
socket.Close();
return null;
}

myEncoded_bytes 由:

MemoryStream ms = new MemoryStream();
using (FileStream stream = File.Open("\\test.rtf", FileMode.Open))
{
byte[] stream_to_byte_array = new byte[stream.Length];
stream.Read(stream_to_byte_array, 0, (int)stream.Length);
ms = new MemoryStream(stream_to_byte_array);
stream.Close();
}
byte[] data = ms.ToArray();

谢谢你的帮助,

军团

P.S.:外部库是不允许的,所以我不能使用 printCE 或类似的“帮助”。

最佳答案

对不起,我试着更好地解释我是如何达到目标的,这是一个在@josef link 之后制作的示例函数。 ,我在选择的位置写了一些文本,并在屏幕中央画了一个红色框(注释解释了 pcl 命令的工作原理)。按照 PCL 手册,您可以根据需要轻松自定义文本和绘图 (HERE_AN_HP_MANUAL)。

        private void btn_pcl_printing_Click(object sender, EventArgs e)
{
string printer_ip = "192.168.1.6";

char command_start_character = '\u001B';
char carriage_return = '\u000D';
char line_feed = '\u000A';
string lane_escape_character = string.Concat(carriage_return, line_feed); //"\r\n";
#if PJL
string printer_process_name = "prova.TXT";
string raw_end_of_file = command_start_character + "%–12345X" + " @PJL EOF" + lane_escape_character;
raw_end_of_file += command_start_character + "%–12345X";
string raw_to_print = command_start_character + "%–12345X";
raw_to_print += " @PJL JOB NAME = \"" + printer_process_name + "\"" + lane_escape_character;
raw_to_print += " @PJL ENTER LANGUAGE = PCL" + lane_escape_character;
#else
//Reset the printer and start a new PAGE
string raw_to_print = command_start_character + "E";
#endif
//Set Printer resolution to 150 DPI
string raw_pcl_commands = command_start_character + "&u150D" + command_start_character + "*t150R";
//Write "Hello Wold!!" at the position 150x and 150y (the command is case sensitive).
raw_pcl_commands += command_start_character + "*p150x150Y" + "Hello World!!!";
//Another way to to what i've just done before
//raw_pcl_commands += command_start_character + "*p150X" + command_start_character + "*p150Y" + "Hello World!!!";
raw_pcl_commands += command_start_character + "*p150X" + command_start_character + "*p150Y" + "Hello World!!!";
//I choose to use RGB colors (v6), select an rgb color (a0,b0,c0) and save it an a palette (i0) that i select (i0S). My selected color is RED (255,0,0)
raw_pcl_commands += command_start_character + "*v6w255a0b0c0i0S";
//Another way to to what i've just done before
//raw_pcl_commands += command_start_character + "*v6W";
//raw_pcl_commands += command_start_character + "*v255a0b0C" + command_start_character + "*v0i0S";
//Draw a rectangle 300*150
raw_pcl_commands += command_start_character + "*c300a150b5P";


//COMANDI PCL
raw_pcl_commands += command_start_character + "E";
raw_to_print += raw_pcl_commands;
#if PJL
raw_to_print += raw_end_of_file;
#endif

byte[] myEncoded_bytes = Encoding.UTF8.GetBytes(raw_to_print);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Not supported on CE
//socket.NoDelay = true;
int printerPort = 9100;
IPAddress ip = IPAddress.Parse(printer_ip);
IPEndPoint ipEndPoint = new IPEndPoint(ip, printerPort);
socket.Connect(ipEndPoint);
socket.Send(myEncoded_bytes);
socket.Close();
}

现在对于图像,您有 2 种方法可以遵循:

1) 使用 PCL 命令“手动”绘制图像,see from page 41 of that manual查看一些示例(如果是像我这样的简单示例,那就是简单的条形码是最好的遵循方式)。

2) 用 PCL 宏绘制图像……我只能这么说,因为请阅读 that documentation让我很头疼,迫使我选择方法 1 :D

希望对你有帮助军团

关于c# - 紧凑的框架套接字文档打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50060831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com