一、使用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用法总结的更多相关文章

  1. [转载]PyTorch中permute的用法

    [转载]PyTorch中permute的用法 来源:https://blog.csdn.net/york1996/article/details/81876886 permute(dims) 将ten ...

  2. 转载:Hadoop排序工具用法小结

    本文转载自Silhouette的文章,原文地址:http://www.dreamingfish123.info/?p=1102 Hadoop排序工具用法小结 发表于 2014 年 8 月 25 日 由 ...

  3. 【转载】python super的用法

    转载地址: http://blog.csdn.net/cxm19830125/article/details/20610533 super的用法是调用继承类的初始化方法,如下面的代码: class A ...

  4. [转载] 跟着实例学习zookeeper 的用法

    原文: http://ifeve.com/zookeeper-curato-framework/ zookeeper 的原生客户端库过于底层, 用户为了使用 zookeeper需要编写大量的代码, 为 ...

  5. 【转载】extern "C"的用法解析(原博主就是抄百度百科的,不如另外一篇好)

    [说明]文章转载自Rollen Holt 的文章 http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html --------- ...

  6. (转载)mysql group by 用法解析(详细)

    (转载)http://blog.tianya.cn/blogger/post_read.asp?BlogID=4221189&PostID=47881614 mysql distinct 去重 ...

  7. (转载)mysql中limit用法

    (转载)http://hi.baidu.com/sppeivan/item/e45179375d6778c62f8ec221   mysql中limit用法 使用查询语句的时候,经常要返回前几条或者中 ...

  8. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  9. [转载]js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

随机推荐

  1. JFrame图形界面 ----鼠标消息

    #开始 不管是什么GUI 按钮的存在都是必不可少的而且还会有很多奇怪的按钮 #代码 package window; import java.awt.Container; import java.awt ...

  2. BootStrap 专题

    验证码的输入框和验证码图片在一行,用bootstrap原生的怎么写呢? 看了教程,没有完全一样的可以让右侧的按钮“输入验证码”固定大小.左侧的输入框动态大小吗?   <div class=&qu ...

  3. composer安装以及更新问题,配置中国镜像源。

    配置国内镜像源 中国镜像源 https://pkg.phpcomposer.com/ composer 中文官网地址 http://www.phpcomposer.com/ 下载 Composer 安 ...

  4. jmeter 监听器聚合报告说明:

    Label:表示定义HTTP请求名称 Samples:表示这次测试中一共发出了多少个请求. Average:平均响应时长---默认情况下是单个Request的平均响应时长,当使用了Transactio ...

  5. [Python] uniform() 函数

    描述uniform() 方法将随机生成下一个实数,它在[x, y) 范围内. 语法以下是 uniform() 方法的语法: import random random.uniform(x, y) 注意: ...

  6. Python反序列化 pickle

    # 若需要处理更复杂的数据, 用pickle. pickle只有在Python里能用, 其它语言不行. # 序列化. import pickle def sayhi(name): print('hel ...

  7. 开机出现loading (hd0)/ntldr。。。

    电脑一开机就出现ntldr is missing的原因:1.操作系统文件损坏.2.MBR表损坏.3.硬盘数据线松了.4.硬盘坏了.解决方法:1.重新安装操作系统.2.用U盘或光盘引导,进入PE系统,用 ...

  8. bzoj 3167 SAO

    树dp 定义f[i][j]为i在其已合并子树内排名为j的方案数 O(n2)进行子树合并 转移时枚举他在已合并子树中的排名j和新合并子树中的排名k+1 当他比他儿子大的时候$f[x][j+k]=f[x] ...

  9. python九九

    学了有一段时间了,才发现自己连九九乘法表都写不出,好好笑,哈哈. 代码实现: for i in range(1,10): for j in range(1,i+1): print('%dx%d=%-2 ...

  10. 全文检索选择-------- Elasticsearch与Solr

    Elasticsearch简介* Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文搜索,结构化搜索以及分析,当然你也可以将这三 ...