C#打印類PrintDocument、PrintDialog、PrintPreviewDialog使用示例
1.使用PrintDocument進(jìn)行打印
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)
{
//實(shí)例化打印對象
PrintDocument printDocument1 = new PrintDocument();
//設(shè)置打印用的紙張,當(dāng)設(shè)置為Custom的時(shí)候,可以自定義紙張的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500);
//注冊PrintPage事件,打印每一頁時(shí)會觸發(fā)該事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
//開始打印
printDocument1.Print();
}
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//設(shè)置打印內(nèi)容及其字體,顏色和位置
e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑體"), 24), System.Drawing.Brushes.Red, 50, 50);
}
}
}
2.使用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)
{
//實(shí)例化打印對象
PrintDocument printDocument1 = new PrintDocument();
//設(shè)置打印用的紙張,當(dāng)設(shè)置為Custom的時(shí)候,可以自定義紙張的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500);
//注冊PrintPage事件,打印每一頁時(shí)會觸發(fā)該事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
//初始化打印對話框?qū)ο?
PrintDialog printDialog1 = new PrintDialog();
//將PrintDialog.UseEXDialog屬性設(shè)置為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)
{
//設(shè)置打印內(nèi)容及其字體,顏色和位置
e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑體"), 24), System.Drawing.Brushes.Red, 50, 50);
}
}
}
打印對話框如下圖所示。

3.使用PrintPreviewDialog增加打印預(yù)覽對話框
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)
{
//實(shí)例化打印對象
PrintDocument printDocument1 = new PrintDocument();
//設(shè)置打印用的紙張,當(dāng)設(shè)置為Custom的時(shí)候,可以自定義紙張的大小
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500);
//注冊PrintPage事件,打印每一頁時(shí)會觸發(fā)該事件
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
//初始化打印預(yù)覽對話框?qū)ο?
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
//將printDocument1對象賦值給打印預(yù)覽對話框的Document屬性
printPreviewDialog1.Document = printDocument1;
//打開打印預(yù)覽對話框
DialogResult result = printPreviewDialog1.ShowDialog();
if (result == DialogResult.OK)
printDocument1.Print();//開始打印
}
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//設(shè)置打印內(nèi)容及其字體,顏色和位置
e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑體"), 24), System.Drawing.Brushes.Red, 50, 50);
}
}
}
打印時(shí),會顯示下圖所示預(yù)覽畫面。

注意:PrintDialog與PrintPreviewDialog位于名稱空間System.Windows.Forms(程序集為System.Windows.Forms.dll)中,而PrintDocument位于名稱空間System.Drawing.Printing(程序集為System.Drawing.dll)中。
相關(guān)文章
C#實(shí)現(xiàn)appSettings節(jié)點(diǎn)讀取與修改的方法
這篇文章主要介紹了C#實(shí)現(xiàn)appSettings節(jié)點(diǎn)讀取與修改的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10
C#使用NPOI設(shè)置Excel下拉選項(xiàng)
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI設(shè)置Excel下拉選項(xiàng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
WindowsForm實(shí)現(xiàn)警告消息框的實(shí)例代碼
這篇文章主要介紹了WindowsForm如何實(shí)現(xiàn)警告消息框,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
小白2分鐘學(xué)會Visual Studio如何將引用包打包到NuGet上
這篇文章主要介紹了小白2分鐘學(xué)會Visual Studio如何將引用包打包到NuGet上,只需兩步完成打包上傳操作,需要的朋友可以參考下2021-09-09
動態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果
webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,在WSDL文件里面,包含這個(gè)webservice暴露在外面可供使用的接口。今天我們來詳細(xì)討論下如何動態(tài)調(diào)用以及讀取解析返回結(jié)果2015-06-06
解析從源碼分析常見的基于Array的數(shù)據(jù)結(jié)構(gòu)動態(tài)擴(kuò)容機(jī)制的詳解
本篇文章是對從源碼分析常見的基于Array的數(shù)據(jù)結(jié)構(gòu)動態(tài)擴(kuò)容機(jī)制進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

