C#打印類(lèi)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í)例化打印對(duì)象 PrintDocument printDocument1 = new PrintDocument(); //設(shè)置打印用的紙張,當(dāng)設(shè)置為Custom的時(shí)候,可以自定義紙張的大小 printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500); //注冊(cè)PrintPage事件,打印每一頁(yè)時(shí)會(huì)觸發(fā)該事件 printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //開(kāi)始打印 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增加打印對(duì)話框
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í)例化打印對(duì)象 PrintDocument printDocument1 = new PrintDocument(); //設(shè)置打印用的紙張,當(dāng)設(shè)置為Custom的時(shí)候,可以自定義紙張的大小 printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500); //注冊(cè)PrintPage事件,打印每一頁(yè)時(shí)會(huì)觸發(fā)該事件 printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //初始化打印對(duì)話框?qū)ο? PrintDialog printDialog1 = new PrintDialog(); //將PrintDialog.UseEXDialog屬性設(shè)置為T(mén)rue,才可顯示出打印對(duì)話框 printDialog1.UseEXDialog = true; //將printDocument1對(duì)象賦值給打印對(duì)話框的Document屬性 printDialog1.Document = printDocument1; //打開(kāi)打印對(duì)話框 DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) printDocument1.Print();//開(kāi)始打印 } 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); } } }
打印對(duì)話框如下圖所示。
3.使用PrintPreviewDialog增加打印預(yù)覽對(duì)話框
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í)例化打印對(duì)象 PrintDocument printDocument1 = new PrintDocument(); //設(shè)置打印用的紙張,當(dāng)設(shè)置為Custom的時(shí)候,可以自定義紙張的大小 printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500); //注冊(cè)PrintPage事件,打印每一頁(yè)時(shí)會(huì)觸發(fā)該事件 printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //初始化打印預(yù)覽對(duì)話框?qū)ο? PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); //將printDocument1對(duì)象賦值給打印預(yù)覽對(duì)話框的Document屬性 printPreviewDialog1.Document = printDocument1; //打開(kāi)打印預(yù)覽對(duì)話框 DialogResult result = printPreviewDialog1.ShowDialog(); if (result == DialogResult.OK) printDocument1.Print();//開(kāi)始打印 } 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í),會(huì)顯示下圖所示預(yù)覽畫(huà)面。
注意:PrintDialog與PrintPreviewDialog位于名稱(chēng)空間System.Windows.Forms(程序集為System.Windows.Forms.dll)中,而PrintDocument位于名稱(chēng)空間System.Drawing.Printing(程序集為System.Drawing.dll)中。
相關(guān)文章
C#數(shù)據(jù)類(lèi)型轉(zhuǎn)換(顯式轉(zhuǎn)型、隱式轉(zhuǎn)型、強(qiáng)制轉(zhuǎn)型)
本文詳細(xì)講解了C#數(shù)據(jù)類(lèi)型轉(zhuǎn)換(顯式轉(zhuǎn)型、隱式轉(zhuǎn)型、強(qiáng)制轉(zhuǎn)型),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Unity中 ShaderGraph 實(shí)現(xiàn)超級(jí)炫酷的溶解效果入門(mén)級(jí)教程
這篇文章主要介紹了Unity中的 ShaderGraph 實(shí)現(xiàn)超級(jí)炫酷的溶解效果入門(mén)級(jí)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07C#實(shí)現(xiàn)把科學(xué)計(jì)數(shù)法(E)轉(zhuǎn)化為正常數(shù)字值
這篇文章主要介紹了C#實(shí)現(xiàn)把科學(xué)計(jì)數(shù)法(E)轉(zhuǎn)化為正常數(shù)字值,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-06-06簡(jiǎn)單了解C#設(shè)計(jì)模式編程中的橋接模式
這篇文章主要介紹了C#設(shè)計(jì)模式編程中的橋接模式,橋接模式經(jīng)常應(yīng)用于解耦邏輯層與數(shù)據(jù)操作層,需要的朋友可以參考下2016-02-02C#使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器
這篇文章主要為大家詳細(xì)介紹了C#如何使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)
這篇文章主要介紹了如何使用C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)的方法,并附上全部源碼,分享給大家,有需要的小伙伴可以參考下。2015-12-12