首页 > 代码库 > C#winfrom打印与打印预览
C#winfrom打印与打印预览
引入printDocument控件
/// <summary>
/// 打印方法
/// </summary>
public void Print()
{
//实例化打印对象
PrintDocument printDocument1 = new PrintDocument();
//设置打印用的纸张,当设置为Custom的时候,可以自定义纸张的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 420, (PrintData.Count * 155) + (PrintData.Count * 10) + 10);
//注册PrintPage事件,打印每一页时会触发该事件
printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
//初始化打印预览对话框对象
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
//将printDocument1对象赋值给打印预览对话框的Document属性
printPreviewDialog1.Document = printDocument1;
//打开打印预览对话框
DialogResult result = printPreviewDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();//开始打印--printDocument控件的PrintPage事件
}
}
//printDocument控件的PrintPage事件
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//画出打印内容
e.Graphics.DrawString("吴世杰", new System.Drawing.Font("宋体", 6, FontStyle.Bold), new SolidBrush(Color.Black), new PointF(5, 53));
}
C#winfrom打印与打印预览