使用打印預覽界面可以使用PrintPreviewDialog類
.Net中實現打印可使用PrintPreviewControl類,該類可以用來在窗體中預覽文檔。但這個類沒的提供相關菜單。
PrintPreviewDialog在PrintPreviewControl有基礎上封裝了一些控件的對話框。它派生於System.Windows.Forms.Form。
這里我們用PrintPreviewDialog界面進行打印預覽。
PrintPreviewDialog的用法與PageSetupDiaog及PrintDialog用法類似。即:
1、在實例化一個PrintPreviewDialog
2、設置document屬性設置為需要打印的文檔
3、呼叫ShowDialog()方法,打開“打印”界面。
注:打印DataGridView例子:https://files.cnblogs.com/scottckt/Print_DataGridView.rar
我們對前次的代碼作修改,增加預覽功能。代碼如下:
using System.IO;
using System.Drawing.Printing;
namespace SimpleEditor
{
public partial class SimpleEditorForm : Form
{
private string filename = "Untitled";
//打印文檔
PrintDocument pdDocument = new PrintDocument();
//打印格式設置頁面
PageSetupDialog dlgPageSetup = new PageSetupDialog();
//打印頁面
PrintDialog dlgPrint = new PrintDialog();
//1、實例化打印預覽
PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();
private string[] lines;
private int linesPrinted;
public SimpleEditorForm()
{
InitializeComponent();
pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);
//頁面設置的打印文檔設置為需要打印的文檔
dlgPageSetup.Document = pdDocument;
//打印界面的打印文檔設置為被打印文檔
dlgPrint.Document = pdDocument;
//2、打印預覽的打印文檔設置為被打印文檔
dlgPrintPreview.Document = pdDocument;
}
/// <summary>
/// 打印預覽按鈕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFilePrintPreview(object sender, EventArgs e)
{
//3、顯示打印預覽界面
dlgPrintPreview.ShowDialog();
}
/// <summary>
/// 頁面設置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFilePageSetup(object sender, EventArgs e)
{
dlgPageSetup.ShowDialog();
}
private void OnExit(object sender, EventArgs e)
{
}
/// <summary>
/// 當按下打印時,此為界面中的打印按鈕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFilePrint(object sender, EventArgs e)
{
try
{
//判斷是否有選擇文本
if (textBoxEdit.SelectedText != "")
{
//如果有選擇文本,則可以選擇"打印選擇的範圍"
dlgPrint.AllowSelection = true;
}
else
{
dlgPrint.AllowSelection = false;
}
//呼叫打印界面
if (dlgPrint.ShowDialog()==DialogResult.OK)
{
/*
* PrintDocument對象的Print()方法在PrintController類的幫助下,執行PrintPage事件。
*/
pdDocument.Print();
}
}
catch (InvalidPrinterException ex )
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
/// <summary>
/// 每個打印任務衹調用OnBeginPrint()一次。
/// 所有要打印的內容都在此設置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pdDocument_BeginPrint(object sender, PrintEventArgs e)
{
char[] param ={ '\n' };
//lines = textBoxEdit.Text.Split(param);
//判斷是否選取 列印被選擇的範圍
if (dlgPrint.PrinterSettings.PrintRange==PrintRange.Selection)
{
lines = textBoxEdit.SelectedText.Split(param);
}
else
{
lines = textBoxEdit.Text.Split(param);
}
int i = 0;
char[] trimParam ={ '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
/// <summary>
/// printDocument的PrintPage事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
/*
* 得到TextBox中每行的字符串數組
* \n換行
* \r回車
*/
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while (linesPrinted<lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
y += 55;
//判斷超過一頁時,列印其它頁面
if (y >= e.PageBounds.Height - 80)
{
//多頁打印
e.HasMorePages = true;
/*
* PrintPageEventArgs類的HaeMorePages屬性為True時,通知控件器,必須再次調用OnPrintPage()方法,打印一個頁面。
* PrintLoopI()有一個用於每個要打印的頁面的序例。如果HasMorePages是False,PrintLoop()就會停止。
*/
return;
}
}
linesPrinted = 0;
e.HasMorePages = false;
}
/// <summary>
/// EndPrint事件釋放BeginPrint方法中佔用的資源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pdDocument_EndPrint(object sender, PrintEventArgs e)
{
//變量Lines占用和引用的字符串數組,現在釋放
lines = null;
}
}
}
using System.Drawing.Printing;
namespace SimpleEditor
{
public partial class SimpleEditorForm : Form
{
private string filename = "Untitled";
//打印文檔
PrintDocument pdDocument = new PrintDocument();
//打印格式設置頁面
PageSetupDialog dlgPageSetup = new PageSetupDialog();
//打印頁面
PrintDialog dlgPrint = new PrintDialog();
//1、實例化打印預覽
PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();
private string[] lines;
private int linesPrinted;
public SimpleEditorForm()
{
InitializeComponent();
pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);
//頁面設置的打印文檔設置為需要打印的文檔
dlgPageSetup.Document = pdDocument;
//打印界面的打印文檔設置為被打印文檔
dlgPrint.Document = pdDocument;
//2、打印預覽的打印文檔設置為被打印文檔
dlgPrintPreview.Document = pdDocument;
}
/// <summary>
/// 打印預覽按鈕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFilePrintPreview(object sender, EventArgs e)
{
//3、顯示打印預覽界面
dlgPrintPreview.ShowDialog();
}
/// <summary>
/// 頁面設置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFilePageSetup(object sender, EventArgs e)
{
dlgPageSetup.ShowDialog();
}
private void OnExit(object sender, EventArgs e)
{
}
/// <summary>
/// 當按下打印時,此為界面中的打印按鈕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFilePrint(object sender, EventArgs e)
{
try
{
//判斷是否有選擇文本
if (textBoxEdit.SelectedText != "")
{
//如果有選擇文本,則可以選擇"打印選擇的範圍"
dlgPrint.AllowSelection = true;
}
else
{
dlgPrint.AllowSelection = false;
}
//呼叫打印界面
if (dlgPrint.ShowDialog()==DialogResult.OK)
{
/*
* PrintDocument對象的Print()方法在PrintController類的幫助下,執行PrintPage事件。
*/
pdDocument.Print();
}
}
catch (InvalidPrinterException ex )
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
/// <summary>
/// 每個打印任務衹調用OnBeginPrint()一次。
/// 所有要打印的內容都在此設置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pdDocument_BeginPrint(object sender, PrintEventArgs e)
{
char[] param ={ '\n' };
//lines = textBoxEdit.Text.Split(param);
//判斷是否選取 列印被選擇的範圍
if (dlgPrint.PrinterSettings.PrintRange==PrintRange.Selection)
{
lines = textBoxEdit.SelectedText.Split(param);
}
else
{
lines = textBoxEdit.Text.Split(param);
}
int i = 0;
char[] trimParam ={ '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
/// <summary>
/// printDocument的PrintPage事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
/*
* 得到TextBox中每行的字符串數組
* \n換行
* \r回車
*/
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while (linesPrinted<lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
y += 55;
//判斷超過一頁時,列印其它頁面
if (y >= e.PageBounds.Height - 80)
{
//多頁打印
e.HasMorePages = true;
/*
* PrintPageEventArgs類的HaeMorePages屬性為True時,通知控件器,必須再次調用OnPrintPage()方法,打印一個頁面。
* PrintLoopI()有一個用於每個要打印的頁面的序例。如果HasMorePages是False,PrintLoop()就會停止。
*/
return;
}
}
linesPrinted = 0;
e.HasMorePages = false;
}
/// <summary>
/// EndPrint事件釋放BeginPrint方法中佔用的資源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pdDocument_EndPrint(object sender, PrintEventArgs e)
{
//變量Lines占用和引用的字符串數組,現在釋放
lines = null;
}
}
}
這樣我們就完成了打印的全部功能。
打印DataGridView例子:https://files.cnblogs.com/scottckt/Print_DataGridView.rar