- A+
节
1、从官网上下载Visual Studio开发环境:https://visualstudio.microsoft.com/zh-hans/,下载完成后运行安装程序,进行安装。
2、从官网下载光打印机驱动:https://www.epson.com/Support/Printers/All-In-Ones/WorkForce-Series/Epson-WorkForce-210/s/SPT_C11CA48201,下载到本地。
3、解压缩下载的压缩文件,将文件夹中的文件复制到Visual Studio的文件夹中。
4、将Visual Studio打开,选择文件->新建->项目,然后可以选择要使用的语言,在本次实验中选择C#语言,然后在“空项目”下进行配置,添加以下代码:
using System;
using System.Drawing;
using System.Drawing.Printing;
//设置页面边距
Margins margins = new Margins(200,200,200,200);
//要打印的文本
String printtext = "This is a test print";
//保存打印结果
Boolean result = true;
//设置打印机
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.Margins = margins;
pd.PrintPage += delegate(object sender, PrintPageEventArgs e)
{
//将文本绘制到打印页面上
e.Graphics.DrawString(printtext, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, pd.DefaultPageSettings.PrintableArea.Width, pd.DefaultPageSettings.PrintableArea.Height));
result = true;
};
//将打印机设置指定的光打印机
pd.PrinterSettings.PrinterName = "Epson WorkForce 210";
//执行打印
try
{
pd.Print();
}catch(Exception ex)
{
result = false;
Console.WriteLine(ex.Message);
}
if (result)
{
Console.WriteLine("Print success");
}
5、完成代码后,CTRL+F5编译运行程序,程序将连接到指定的光打印机,并完成打印任务。
- 微信在线客服
- 这是我的微信扫一扫
-
- 我的微信公众号
- 扫一扫码上服务
-






