【C#】Winform之C/S架构html页面文件实现直接点击批量顺序打印
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
打印HTML页面的话,我使用WebBrowser控件。 1、设置默认打印机 /// <summary>
/// 页面加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
#region 绑定打印机信息
PrintDocument fPrintDocument = new PrintDocument();
//获取默认打印机
string defaultPrinter = fPrintDocument.PrinterSettings.PrinterName;
//打印机绑定
List<string> printerList = new List<string>();
var printList = PrinterSettings.InstalledPrinters;
foreach (String fPrinterName in printList)
{
printerList.Add(fPrinterName);
}
this.cbx_devs.DataSource = printerList;
this.cbx_devs.selectedItem = defaultPrinter;
#endregion } /// <summary> /// 值改变事件-设置默认打印机 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbx_devs_selectedIndexChanged(object sender, EventArgs e) { SetDefaultPrinter(this.cbx_devs.selectedItem.ToString()); } /// <summary> /// 默认打印机设置 /// </summary> private void SetDefaultPrinter(string printerName) { Externs.SetDefaultPrinter(printerName); //设置默认打印机 } public class Externs { //调用win api将指定名称的打印机设置为默认打印机 [DllImport("winspool.drv")] public static extern bool SetDefaultPrinter(String Name); } 2、单个文件打印 //导出的测试文件
string path = @"C:\Users\Administrator\Desktop\测试.html";
/// <summary>
/// webBrowser打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_print_Click(object sender, EventArgs e)
{
//不显示脚本错误
webBrowser1.scriptErrorsSuppressed = true;
//此处也可以直接在页面进行事件绑定
webBrowser1.DocumentCompleted += WebBrowser_DocumentCompleted;
webBrowser1.Url = new Uri(path);
}
/// <summary>
/// 加载完成后事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//html加载完成后,触发打印方法
((WebBrowser)sender).Print();
} 3、打印多个文件 //为了与单个文件打印区分,我重新实例化了一个WebBrowser控件 WebBrowser webBrowser2 = new WebBrowser(); /// <summary> /// 页面加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { //打印设置 webBrowser2.scriptErrorsSuppressed = true; webBrowser2.DocumentCompleted += WebBrowser2_DocumentCompleted; //绑定打印机信息 } //表示网页是否正在加载 bool loading = true; /// <summary> /// 打印多个文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_printFiles_Click(object sender, EventArgs e) { List<string> list = new List<string>() { path1, path2, path3 }; foreach (var filePath in list) { loading = true;//网页正在加载 webBrowser2.Url = new Uri(filePath); while (loading) { //等待本次加载完毕后才执行下次循环 Application.DoEvents(); } } } /// <summary> /// 加载完成后事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //html加载完成后,触发打印方法 ((WebBrowser)sender).Print(); loading = false;//表示加载完成后,状态变为false,下一次循环开始执行 } 3、顺序打印该文章在 2023/8/11 18:05:28 编辑过
|
关键字查询
相关文章
正在查询... |