[转载]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; 在大多数情况下,为事件处理函 ...
随机推荐
- [ Java面试题 ]WEB篇
1.AJAX有哪些有点和缺点? 优点: 1.最大的一点是页面无刷新,用户的体验非常好. 2.使用异步方式与服务器通信,具有更加迅速的响应能力. 3.可以把以前一些服务器负担的工作转嫁到客户端,利用客户 ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- JVM学习②
JVM运行机制 1.JVM启动流程 Java启动命令->装载配置寻找jvm.cfg->根据配置寻找JVM.dll(JVM主要实现)->初始化JVM,获得JNIEnv接口 2.JVM基 ...
- error LNK2001: 无法解析的外部符号 解决方法
错误提示:LNK2001 无法解析的外部符号 "public: class el::base::Writer & __cdecl el::base::Writer::construc ...
- netData.go 阅读源码
) // 定义数据传输结构 type NetData struct { // 消息体 Body interface{} // 操作代号 Operation string ...
- Dubbo中暴露服务的过程解析
dubbo暴露服务有两种情况,一种是设置了延迟暴露(比如delay="5000"),另外一种是没有设置延迟暴露或者延迟设置为-1(delay="-1"): 设置 ...
- layer的删除询问框的使用
删除是个很需要谨慎的操作 我们需要进行确认 对了删除一般使用ajax操作 因为如果同url请求 处理 再返回 会有空白页 1.js自带的样式 <button type="button& ...
- java quartz 计算近20次执行时间
/** * * @desc 计算表达式近20次时间 * @auth josnow * @date 2017年5月31日 下午12:16:25 * @param cron * @return */ pu ...
- python+appium 查找某个元素find_element()并click()点击,正向判断与反判断的方法封装
使用场景: 在自动化测试过程中,难免会用到反判断,包括异常情况的处理,比如:find_element_by_name('测试') 判断"测试"是否存在,存在则点击,不存在则执行其他 ...
- 集群IPtables转发与防火墙
子网集群通过接入公网的服务器Iptables转发上网 1. 对iptables进行初始化工作 清空filter表 iptables -F 清空nat表 iptables -t nat -F 默认禁止所 ...