打印系统开发(14)——WinForm开发(42)——C#启动打印机打印文件

mac2024-05-16  33

C#启动打印机我总结了两种方法:

新建如图winForm窗体:

一、第一种方法:Process类方法

1、添加引用:using System.Diagnostics;

2、在按钮打印测试button4单击下写入如下代码:

private void button4_Click(object sender, EventArgs e) {       Process pro = new Process();       pro .StartInfo.FileName = "C:\\数据格式.docx";//文件路径       pro .StartInfo.CreateNoWindow = true;       pro .StartInfo.WindowStyle = ProcessWindowStyle.Hidden;       pro .StartInfo.Verb = "Print";       pro .Start(); }

这部分代码打印具体文档。

二、第二种方法:printDocument控件方法

1、添加printDocument控件

2、在控件printDocument的PrintPage事件下添加

 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {        //打印内容 为 自定义文本内容         Font font = new Font("宋体", 12);        Brush bru = Brushes.Blue;        for (int i = 1; i <= 5; i++)        {             e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);        }  }

这部分代码讲打印出5个"Hello world "字符串。

最新回复(0)