C#使用PrintDocument实现打印预览

  1. 添加PrintPreviewControl控件,并做如下设置,绑定printDocument控件:

在这里插入图片描述

  1. 初始化printdocument并绑定打印触发事件
this.printDocument1.PrintController = null;
//设置打印用的纸张,当设置为Custom的时候,可以自定义纸张的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom", 800, 1150);
//注册PrintPage事件,打印每一页时会触发该事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
  1. 在画布上写内容:
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
	//添加字符串
	e.Graphics.DrawString("Hello World!", new Font(new FontFamily("宋体"), 10.5f), System.Drawing.Brushes.Black, x, y);
	//添加图片
	e.Graphics.DrawImage(Image.FromFile(imgPath), x, y, imgwid,imgheg);
	//画线
	Pen pen = new Pen(Color.Red, 2);
	e.Graphics.DrawLine(pen, x1, y1, x2, y2);
	//画矩形
	e.Graphics.DrawRectangle(pen, x, y, wid, heg);
}
  1. 放大倍数设置,调整Zoom属性,值为1时放大倍数为100%,可设置成带小数的参数(0.75、1.2等)
    在这里插入图片描述
  2. 分页:
e.HasMorePages = true;
printControl.Rows += 1;

分页具体实现,如下:

int page=0;
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
	if(page=0)
	{
	 page++;
	 //添加字符串
	 e.Graphics.DrawString("第一页", new Font(new FontFamily("宋体"), 10.5f), System.Drawing.Brushes.Black, x, y);
	 e.HasMorePages = true;
     printControl.Rows += 1;
	}
	else
	{
	 e.Graphics.DrawString("第二页", new Font(new FontFamily("宋体"), 10.5f), System.Drawing.Brushes.Black, x, y);
	}
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值