本文的代碼可以從這里獲?。簑informDemo.rar · 張祥裕/分享的資源名稱 - Gitee.com
作者的水平有限,如有錯誤,望指正。
為了簡單起見,紙張大小,打印機等信息按照默認的來,本文的實現(xiàn)方案是:打印Panel中的控件信息,循環(huán)進行打印,打印完一張,把信息重新填充到對應的控件上再繼續(xù)打印。
這個地址winforms - Print the entire area of the control with C# - Stack Overflow鏈接提到了這么一句:
?說是要把控件里的內(nèi)容重新賦值給另外一個控件myControlToDisplay,大概看了一下提供的代碼,感覺代碼不全,就沒有照著來試了
還有這里的鏈接:
Printing Multiple Pages of a Text File in Windows Forms | DotNetCurry
讀取txt記事本里面的內(nèi)容,并分頁打印,提供的代碼
提到了一個HasMorePages屬性,設置它應該可以實現(xiàn)連續(xù)打印,結合這兩個鏈接提供的方案,這讓我想到了另外一種方案:把要打印的內(nèi)容全部渲染到一個容器控件上,根據(jù)紙張的大小,計算并截取要打印的內(nèi)容,類似效果如下:
?從上往下,就像疊羅漢一樣,直到截取并打印完畢,這只是個人的想法,還沒驗證。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-620385.html
好了,本文的內(nèi)容正式開始
步驟如下:
1? 新建Winform程序,名為winfromDemo
UI布局及控件的名稱如下:
2? 新增類,名為:Student,代碼如下:
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? 打印邏輯代碼如下:
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;
//遍歷循環(huán)打印
foreach (var item in studentPrintList)
{
currentPageNumber++;
//設置頁碼信息
SetPageInfo(currentPageNumber, studentPrintList.Count);
this.txtName.Text = item.Name;
this.txtSex.Text = item.Age;
printDialog.ShowDialog();
}
}
/// <summary>
/// 調(diào)用printDialog.ShowDialog()方法就會自動調(diào)該方法進行打印
/// </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? 點擊打印按鈕,運行效果如下:
?關掉預覽框后,會接著彈出打印第二頁的預覽框,如下圖:
?
好了,本水文到此結束。如有疑問,可以通過郵箱聯(lián)系:2847225301@qq.com文章來源:http://www.zghlxwxcb.cn/news/detail-620385.html
?
到了這里,關于打印Winform控件實現(xiàn)簡陋版的分頁打印(C#)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!