[转载]PrintDocument,PrintDialog与PrintPreviewDialog用法总结
一、使用PrintDocument进行打印
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms; namespace PrintTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//实例化打印对象
PrintDocument printDocument1 = new PrintDocument();
//设置打印用的纸张,当设置为Custom的时候,可以自定义纸张的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", , );
//注册PrintPage事件,打印每一页时会触发该事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
//开始打印
printDocument1.Print();
}
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//设置打印内容及其字体,颜色和位置
e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑体"), ), System.Drawing.Brushes.Red, , );
}
}
}
二、使用PrintDialog增加打印对话框
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms; namespace PrintTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//实例化打印对象
PrintDocument printDocument1 = new PrintDocument();
//设置打印用的纸张,当设置为Custom的时候,可以自定义纸张的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", , );
//注册PrintPage事件,打印每一页时会触发该事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //初始化打印对话框对象
PrintDialog printDialog1 = new PrintDialog();
//将PrintDialog.UseEXDialog属性设置为True,才可显示出打印对话框
printDialog1.UseEXDialog = true;
//将printDocument1对象赋值给打印对话框的Document属性
printDialog1.Document = printDocument1;
//打开打印对话框
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
printDocument1.Print();//开始打印
}
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//设置打印内容及其字体,颜色和位置
e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑体"), ), System.Drawing.Brushes.Red, , );
}
}
}
打印对话框如下图所示:

三、使用PrintPreviewDialog增加打印预览对话框
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms; namespace PrintTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//实例化打印对象
PrintDocument printDocument1 = new PrintDocument();
//设置打印用的纸张,当设置为Custom的时候,可以自定义纸张的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", , );
//注册PrintPage事件,打印每一页时会触发该事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //初始化打印预览对话框对象
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
//将printDocument1对象赋值给打印预览对话框的Document属性
printPreviewDialog1.Document = printDocument1;
//打开打印预览对话框
DialogResult result = printPreviewDialog1.ShowDialog();
if (result == DialogResult.OK)
printDocument1.Print();//开始打印
}
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//设置打印内容及其字体,颜色和位置
e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑体"), ), System.Drawing.Brushes.Red, , );
}
}
}
打印时,会显示下图所示预览画面:

注意:PrintDialog与PrintPreviewDialog位于名称空间System.Windows.Forms(程序集为System.Windows.Forms.dll)中,而PrintDocument位于名称空间System.Drawing.Printing(程序集为System.Drawing.dll)中。
[转载]PrintDocument,PrintDialog与PrintPreviewDialog用法总结的更多相关文章
- [转载]PyTorch中permute的用法
[转载]PyTorch中permute的用法 来源:https://blog.csdn.net/york1996/article/details/81876886 permute(dims) 将ten ...
- 转载:Hadoop排序工具用法小结
本文转载自Silhouette的文章,原文地址:http://www.dreamingfish123.info/?p=1102 Hadoop排序工具用法小结 发表于 2014 年 8 月 25 日 由 ...
- 【转载】python super的用法
转载地址: http://blog.csdn.net/cxm19830125/article/details/20610533 super的用法是调用继承类的初始化方法,如下面的代码: class A ...
- [转载] 跟着实例学习zookeeper 的用法
原文: http://ifeve.com/zookeeper-curato-framework/ zookeeper 的原生客户端库过于底层, 用户为了使用 zookeeper需要编写大量的代码, 为 ...
- 【转载】extern "C"的用法解析(原博主就是抄百度百科的,不如另外一篇好)
[说明]文章转载自Rollen Holt 的文章 http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html --------- ...
- (转载)mysql group by 用法解析(详细)
(转载)http://blog.tianya.cn/blogger/post_read.asp?BlogID=4221189&PostID=47881614 mysql distinct 去重 ...
- (转载)mysql中limit用法
(转载)http://hi.baidu.com/sppeivan/item/e45179375d6778c62f8ec221 mysql中limit用法 使用查询语句的时候,经常要返回前几条或者中 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- [转载]js中return的用法
一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return; 在大多数情况下,为事件处理函 ...
随机推荐
- datePicker.js 应用
var calendar2 = new datePicker();calendar.init({ 'trigger': '#datetime-picker-start', /*选择器,触发弹出插件*/ ...
- MySQL复制进阶
Ⅰ.背景 搭建MySQL复制环境非常简单 你的系统是否也是像我之前那么搭建的呢? 那么,你的复制系统是否出现过以下的情况呢? 复制报错,例如:1062,1032 主从数据不一致 Ⅱ.真正高可靠复制环境 ...
- JVM堆内存监测的一种方式,性能调优依旧任重道远
上月,由极客邦.InfoQ和听云联合主办2016 APMCon中国应用性能管理大会圆满落下帷幕.会上,Java冠军Martijn Verburg进行了一场Java and the Machine的分享 ...
- SVN使用教程2017.10.6
http://www.cnblogs.com/mq0036/p/5250198.html
- 无service.bat的tomcat服务怎么设置自启动
在正式环境中,经常需要设置tomcat自启动,这样在重启系统服务器后就不需要再手动去开启tomcat服务器了.通过设置tomcat下的service.bat可以实现自启动的目的,但有时候会发现自己的t ...
- MyBatis 中一对一和一对多的映射关系
1 一对一映射 比如每位学生有一个地址. public class Address { private Integer addrId; private String street; private S ...
- go语言nsq源码解读七 lookup_protocol_v1.go
本篇将解读nsqlookup处理tcp请求的核心代码文件lookup_protocol_v1.go. 1234567891011121314151617181920212223242526272829 ...
- grpc.go
package,,), etcd.WithPrefix(), etcd.WithPrevKV()} gw.wch = gw.c.Watch(gw.ctx, gw.target, opts... ...
- java的Integer与int的比较
- ISCC 2018 Writeup
题解部分:Misc(除misc500).Web(除Only Admin.Only admin can see flag.有种你来绕.试试看).Reverse.Pwn.Mobile Misc( Auth ...