• 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 }

    完成。

  • 相关阅读:
    tensorflow之tf.meshgrid()
    Python中数据的保存和读取
    透视投影推导
    tensorflow之tf.slice()
    tensorflow的tf.train.Saver()模型保存与恢复
    偶数分割求平均值
    母牛的故事
    统计一行的不重复的单词字符个数
    N个顶点构成多边形的面积
    贪心法基本入门
  • 原文地址:https://www.cnblogs.com/KTblog/p/4482342.html
Copyright © 2020-2023  润新知