zoukankan      html  css  js  c++  java
  • PrintDocument组件打印

    运行效果:

    代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Drawing.Printing;
     7 using System.Linq;
     8 using System.Text;
     9 using System.Threading.Tasks;
    10 using System.Windows.Forms;
    11 
    12 namespace Print
    13 {
    14     public partial class Form1 : Form
    15     {
    16         public Form1()
    17         {
    18             InitializeComponent();
    19         }
    20 
    21         private int intCurrentRowIndex = 0;
    22 
    23         public void InitPrintDocument(PrintDocument printDocument)
    24         {
    25             printDocument.DocumentName = "PrintDocument事例!";
    26 
    27             foreach (PaperSize ps in printDocument.PrinterSettings.PaperSizes)
    28             {
    29                 if (ps.PaperName == "A4")
    30                 {
    31                     printDocument.DefaultPageSettings.PaperSize = ps;
    32                     break;
    33                 }
    34             }
    35 
    36             printDocument.BeginPrint += printDocument_BeginPrint;
    37 
    38             printDocument.EndPrint += printDocument_EndPrint;
    39 
    40             printDocument.PrintPage += printDocument_PrintPage;
    41         }
    42 
    43         void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    44         {
    45             int x = 10;
    46             int y = 10;
    47 
    48             for (int i = intCurrentRowIndex; i < this.textBox1.Lines.Length; i++)
    49             {
    50                 e.Graphics.DrawString(this.textBox1.Lines[i], this.textBox1.Font, Brushes.Black, x, y);
    51                 intCurrentRowIndex++;
    52 
    53                 y = this.textBox1.Font.Height + 5;
    54 
    55                 if (y > e.PageBounds.Height)
    56                 {
    57                     e.HasMorePages = true;
    58                     break;
    59                 }
    60             }
    61         }
    62 
    63         void printDocument_EndPrint(object sender, PrintEventArgs e)
    64         {
    65             this.label2.Text = "打印完成!";
    66         }
    67 
    68         void printDocument_BeginPrint(object sender, PrintEventArgs e)
    69         {
    70             intCurrentRowIndex = 0;
    71             this.label2.Text = "开始打印!";
    72             Application.DoEvents();
    73         }
    74 
    75 
    76         private void Form1_Load(object sender, EventArgs e)
    77         {
    78             InitPrintDocument(this.printDocument1);
    79         }
    80 
    81         private void btn_Print_Click(object sender, EventArgs e)
    82         {
    83             this.printDocument1.Print();
    84         }
    85 
    86         private void btn_Cancle_Click(object sender, EventArgs e)
    87         {
    88             Application.Exit();
    89         }
    90     }
    91 }

    完成。

  • 相关阅读:
    2020最新JavaScript开发必须知道的41个技巧,你会几个?
    Vue项目上线要做哪些优化?面试必学
    javascript 关于dom节点操作的增删改查
    uniapp 上拉加载下拉刷新
    移动WEB适配布局
    微信小程序中封装网络请求方法
    react兄弟组件传值
    【python】Mutilindex多层索引的取值
    【python】通过Mutilindex生成二维表数据
    【python】python vs Excel ( 如何通过loc与iloc函数处理Excel数据)
  • 原文地址:https://www.cnblogs.com/KTblog/p/4482342.html
Copyright © 2011-2022 走看看