C#打印

        private void btnPreview_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            //创建打印文档
            PrintDocument printDocument1 = new PrintDocument();
            //开始打印的时候
            printDocument1.BeginPrint += new PrintEventHandler(printDocument1_BeginPrint);
            //输送打印数据
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            //打印结束的时候
            printDocument1.EndPrint += new PrintEventHandler(printDocument1_EndPrint);
            //设置纸张的大小,主要用于自定义纸张
            System.Drawing.Printing.PageSettings pageSet = new System.Drawing.Printing.PageSettings();
            pageSet.PaperSize = new PaperSize("First custom size", 1366, 768);
            printDocument1.DefaultPageSettings = pageSet;
            //
            printPreviewDialog.Document = printDocument1;
            printPreviewDialog.ShowDialog();
            printPreviewDialog.Close();
        }

        private void printDocument1_EndPrint(object sender, PrintEventArgs e)
        {
            //throw new NotImplementedException();
            //打印结束之后进行的操作
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            //分页,分页数=选中节点数 HasMorePages 用来设置自动触发打印事件 
            //if (Count == 0)
            //{
            //    e.HasMorePages = false;
            //}
            //else
            //{
            //    e.HasMorePages = true;
            //}

            //当第二次加载的数据的时候,要把第一调用的画布,清除一遍,记得这是很重要的一句话哇!呜呜!
            //e.Graphics.Clear(Color.White);
            //绘制背景图像
            //e.Graphics.DrawImage(panelPrint.BackgroundImage, 0, 0, 1366, 768);
        }

        private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
        {
            //throw new NotImplementedException();
            // 打印开始之前整理好数据源
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            PageSetupDialog pageSetupDialog1 = new PageSetupDialog();
            //创建打印文档
            PrintDocument printDocument1 = new PrintDocument();
            //开始打印的时候
            printDocument1.BeginPrint += new PrintEventHandler(printDocument1_BeginPrint);
            //输送打印数据
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            //打印结束的时候
            printDocument1.EndPrint += new PrintEventHandler(printDocument1_EndPrint);
            //设置纸张的大小,主要用于自定义纸张
            System.Drawing.Printing.PageSettings pageSet = new System.Drawing.Printing.PageSettings();
            pageSet.PaperSize = new PaperSize("First custom size", 1024, 768);//此参数调整纸张大小
            printDocument1.DefaultPageSettings = pageSet;
            //将打印文档赋给打印组件
            pageSetupDialog1.Document = printDocument1;
            //纸张及页边距设置
            pageSetupDialog1.PageSettings.Margins = new Margins(0, 0, 0, 0);
            if (DialogResult.OK == pageSetupDialog1.ShowDialog())
            {
                printDocument1.Print();
            }
            printDocument1.Dispose();
            //打印完毕后记得清空临时数据,释放内存,不管别的地方有没有清
        }    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值