一、使用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. Debian虚拟机安装VirtualBox增强功能

    作者:荒原之梦 原文链接:http://zhaokaifeng.com/?p=573 本文中使用的Debian是安装在VirtualBox中的虚拟机,具体参数如下: Debian版本:Linux de ...

  2. Spring-Security-OAuth2调用微信API

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.an ...

  3. PuTTY/终端使用复制、粘贴

    Putty鼠标按钮选项 通过鼠标按钮选项可以控制鼠标来进行复制.粘贴操作,选项包括: 1.Windows选项: 2.混合模式(系统默认选项): 3.Xterm模式. 以上是三种模式选项的简单介绍,下面 ...

  4. 浏览器中缓存Cache

        在请求服务器资源时,服务器会将图片.网页文件等资源保存在客户端的临时文件夹中,称为缓存,当浏览器向服务器请求相同的资源时,如果与服务器版本一致,则从缓存读取     Cookie:服务器存放在 ...

  5. tkinter中checkbutton多选框控件和variable用法(六)

    checkbutton控件 简单的实现多选: import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry ...

  6. 玩转Web之html+CSS(一)---论坛首页表格的实现

    转载请说明出处,小编博客地址:http://blog.csdn.net/u012116457 最近本来想去写一个类似论坛的页面,论坛首页一般都需要一个表格去显示数据,自己简单的写了一下,先上一张图 c ...

  7. Jmeter----【Mac电脑】环境配置与打开Jmeter界面

    前提条件:打开Jmeter界面,首先需要安装java并配置环境变量. 第一步:下载并安装jdk和环境配置 java jdk下载:http://www.oracle.com/technetwork/ja ...

  8. 计算机17-3,4作业E

    E.complete number Description 完数是指一个整数的因子和等于这个数本身,例如6=1+2+3,所以6是一个完数. 按照给定数据范围,找出期中所有完数并输出. Input 数据 ...

  9. python书籍推荐:python编码推荐(高清完整pdf)

    目录INF-qa Python 编码规范................................................................................ ...

  10. TCP报文解析

    概述 在<网络基础总结(一)>总结了TCP建立连接和断开连接的流程,然而TCP协议远比我所了解的复杂得多,我所知的可以说就冰山一角,所总结的也只是纸上谈兵,仅仅只能对TCP有个肤浅的认识, ...