一、使用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", 500, 500); //註冊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("黑體"), 24), System.Drawing.Brushes.Red, 50, 50); } } }
二、使用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", 500, 500); //註冊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("黑體"), 24), System.Drawing.Brushes.Red, 50, 50); } } }
列印對話方塊如下圖所示:
三、使用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", 500, 500); //註冊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("黑體"), 24), System.Drawing.Brushes.Red, 50, 50); } } }
列印時,會顯示下圖所示預覽畫面:
注意:PrintDialog與PrintPreviewDialog位於名稱空間System.Windows.Forms(程式集為System.Windows.Forms.dll)中,而PrintDocument位於名稱空間System.Drawing.Printing(程式集為System.Drawing.dll)中。