打印Winform控件实现简陋版的分页打印(C#)

本文的代码可以从这里获取:winformDemo.rar · 张祥裕/分享的资源名称 - Gitee.com

作者的水平有限,如有错误,望指正。

为了简单起见,纸张大小,打印机等信息按照默认的来,本文的实现方案是:打印Panel中的控件信息,循环进行打印,打印完一张,把信息重新填充到对应的控件上再继续打印。

这个地址winforms - Print the entire area of the control with C# - Stack Overflow链接提到了这么一句:

说是要把控件里的内容重新赋值给另外一个控件myControlToDisplay,大概看了一下提供的代码,感觉代码不全,就没有照着来试了

还有这里的链接:

Printing Multiple Pages of a Text File in Windows Forms | DotNetCurry

读取txt记事本里面的内容,并分页打印,提供的代码

提到了一个HasMorePages属性,设置它应该可以实现连续打印,结合这两个链接提供的方案,这让我想到了另外一种方案:把要打印的内容全部渲染到一个容器控件上,根据纸张的大小,计算并截取要打印的内容,类似效果如下:

从上往下,就像叠罗汉一样,直到截取并打印完毕,这只是个人的想法,还没验证。

好了,本文的内容正式开始

步骤如下:

1 新建Winform程序,名为winfromDemo

UI布局及控件的名称如下:

2 新增类,名为:Student,代码如下:

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace winformDemo
{
    public class Student
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { set; get; }
        /// <summary>
        /// 年龄
        /// </summary>
        public string Age { set; get; }
    }
}

3 打印逻辑代码如下:

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winformDemo
{
    public partial class Form1 : Form
    {
        PrintPreviewDialog printDialog = new PrintPreviewDialog();
        PrintDocument printDocument = new PrintDocument();
        public Form1()
        {
            InitializeComponent();
            InitPrintInfo();
            this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
        }
        /// <summary>
        /// 初始化打印信息
        /// </summary>
        private void InitPrintInfo()
        {
            printDialog.Document = printDocument;
            printDocument.PrintPage += PrintDocument_PrintPage;
        }
        

        /// <summary>
        /// 打印逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, EventArgs e)
        {
            List<Student> studentPrintList = new List<Student>();
            studentPrintList.Add(new Student() {  Name="张三", Age="29"});
            studentPrintList.Add(new Student() { Name = "李四", Age = "28" });
            int currentPageNumber = 0;
            //遍历循环打印
            foreach (var item in studentPrintList)
            {
                currentPageNumber++;
                //设置页码信息
                SetPageInfo(currentPageNumber, studentPrintList.Count);
                this.txtName.Text = item.Name;
                this.txtSex.Text = item.Age;
                printDialog.ShowDialog();
            }
        }
        /// <summary>
        /// 调用printDialog.ShowDialog()方法就会自动调该方法进行打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            PrintStudentInfo(e.Graphics, this.printArea);
        }
        /// <summary>
        /// 打印控件
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="printControl"></param>
        public void PrintStudentInfo(Graphics graphics,Control printControl)
        {
            Bitmap bitmap = new Bitmap(printControl.Width, printControl.Height);
            printControl.DrawToBitmap(bitmap, new Rectangle(0,0, bitmap.Width, bitmap.Height));
            Rectangle target = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            graphics.PageUnit = GraphicsUnit.Display;
            graphics.DrawImage(bitmap, target);
        }
        /// <summary>
        /// 设置页码信息
        /// </summary>
        /// <param name="currentPageNumber"></param>
        /// <param name="totalPageNumber"></param>
        private void SetPageInfo(int currentPageNumber,int totalPageNumber)
        {
            this.lblPageInfo.Text =string.Format("第{0}页/总{1}页",currentPageNumber,totalPageNumber);
        }
    }
}

代码太简单了,就不解释了

4 点击打印按钮,运行效果如下:

关掉预览框后,会接着弹出打印第二页的预览框,如下图:

好了,本水文到此结束。如有疑问,可以通过邮箱联系:[email protected]

相关推荐
o0向阳而生0o2 小时前
28、.NET 中元数据是什么?
microsoft·c#·.net
niuTaylor2 小时前
Linux驱动开发快速上手指南:从理论到实战
linux·运维·开发语言·驱动开发·c#
军训猫猫头3 小时前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
冰茶_5 小时前
.NET MAUI 发展历程:从 Xamarin 到现代跨平台应用开发框架
学习·microsoft·微软·c#·.net·xamarin
The Future is mine6 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
Iotfsd13 小时前
.NET写的开源工业物联网网关(IoTGateway)
物联网·c#·.net·dotnet·边缘网关·雾计算·工业物联网智能网关
先生沉默先13 小时前
c#接口_抽象类_多态学习
开发语言·学习·c#
江沉晚呤时13 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
iReachers14 小时前
使用命令行加密混淆C#程序
开发语言·c#
[太阳]8814 小时前
Kafka命令行的使用/Spark-Streaming核心编程(二)
c#·linq