打印

下面,来进行简单的winform打印功能的使用:

页面布局如下:

代码实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms代码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Print_Click(object sender, EventArgs e)
        {
            // 定义打印模板(paper纸张)的大小;Custum表示自定义纸张大小
            this.PD_document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custum", 1000, 520);
            this.PD_document.PrintPage += PD_document_PrintPage; // 添加打印事件(快捷键:Tab)
            //this.PPD_Dialog.Document = this.PD_document;  // 预览
            //if(this.PPD_Dialog.ShowDialog()==DialogResult.OK)
            //{
            //    this.PD_document.Print();  // 打印
            //}
            // 使用printDialog(也可以同上PPD_Dialog在页面进行添加)
            PrintDialog printDialog = new PrintDialog();
            printDialog.UseEXDialog = true;  // 弹出打印对话框界面
            if(printDialog.ShowDialog()==DialogResult.OK)
            {
                this.PD_document.Print();
            }
        }

        // 进行画图
        private void PD_document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // 绘制字符串
            e.Graphics.DrawString("PrintString", new Font(new FontFamily("宋体"), 24), System.Drawing.Brushes.Yellow, 20, 20);
        }
    }
}

待续。。。

原文地址:https://www.cnblogs.com/namejr/p/11509091.html