在windows應用程式中文檔的列印是一項非常重要的功能,在以前一直是一個非常複雜的工作,Microsoft .net Framework的列印功能都以組件的方式提供,為程式員提供了很大的方便,但是這幾個組件的使用還是很複雜的,有必要解釋一下.
列印操作通常包括以下四個功能
1 列印設定 設定印表機的一些參數比如更改印表機驅動程式等
2 版面設定 設定頁面大小紙張類型等
3 預覽列印 類似於word中的預覽列印
4 列印
實現列印功能的核心是PrintDocument類這個類屬於System.Drawing.Printing名字空間這個類封裝了當前的列印設定版面設定以及所有的與列印有關的事件和方法
這個類包括以下幾個屬性 事件 和方法
1、PrinterSettings 屬性
存放印表機的設定資訊這個屬性不需要程式員設定因為它是由列印對話方塊擷取的
2、PrintCountroller 屬性
控制列印過程
3、DefaultPageSettings 屬性
存放版面設定資訊 列印紙大小方向等也不需要程式員設定因為它是由版面設定對話方塊擷取的
4、DocumentName 屬性
指定文檔名稱,出現在印表機狀態視窗中
1. BeginPrint事件
在列印之前發出
2. PrintPage事件
每列印一頁時發出,事件接受一個PrintPageEventArgs參數該參數封裝了列印相關的資訊
PrintPageEventArgs參數有很多重要的屬性
1 Cancel 取消列印 2 Graphics 頁面的繪圖對象 3 HasMorePages 是否還有要列印的頁面
Print 方法 該方法沒有參數 調用它將按照當前設定開始列印 若實現列印功能首先構造PrintDocument對象添加列印事件PrintDocument printDocument;
private void InitializeComponent( )
{
printDocument=new PrintDocument( );
printDocument.PrintPage += new PrintPageEventHandler ( this.printDocument_PrintPage );
}
實現列印事件功能
列印和繪圖類似都是調用Graphics 類的方法進行畫圖 不同的是一個在顯示器上一個在列印紙上並且列印要進行一些複雜的計算如換行 分頁等. private void printDocument_PrintPage( object sender,PrintPageEventArgs e )
{
Graphics g = e.Graphics;//獲得繪圖對象
float linesPerPage = 0;//頁面的行號
float yPosition = 0;//繪製字串的縱向位置
int count = 0; //行計數器
float leftMargin = e.MarginBounds.Left;//左邊距
float topMargin = e.MarginBounds.Top; //上邊距
string line = null;//行字串
Font printFont = this.textBox.Font;/*當前的列印字型*/
SolidBrush myBrush = new SolidBrush( Color.Black ); //刷子
linesPerPage = e.MarginBounds.Height /printFont.GetHeight( g ); //每頁可列印的行數
//逐行的迴圈列印一頁
while( count < linesPerPage && ( ( line=lineReader.ReadLine( ) ) != null ) )
{
yPosition = topMargin + ( count * printFont.GetHeight( g ) );
g.DrawString( line, printFont, myBrush, leftMargin, yPosition, new StringFormat( ) );
count++;
}
/*
如果本頁列印完成而line不為空白說明還有沒完成的頁面這將觸發下一次的列印事件在下一次的列印中lineReader會自動讀取上次沒有列印完的內容因為lineReader是這個列印方法外的類的成員它可以記錄當前讀取的位置
*/
if( line != null )
e.HasMorePages = true;
else
e.HasMorePages = false;
}
列印設定,構造列印對話方塊 將對話方塊中設定的Document屬性賦給printDocument這樣會將使用者的設定自動儲存到printDocument的PrinterSettings屬性中protected void FileMenuItem_PrintSet_Click( object sender,EventArgs e )
{
PrintDialog printDialog = new PrintDialog( );
printDialog.Document = printDocument;
printDialog.ShowDialog( );
}
版面設定和預覽列印與列印設定原理相同都是構造對話方塊將使用者在對話方塊中的設定儲存到相應的類的屬性中protected void FileMenuItem_PageSet_Click( object sender,EventArgs e )
{
PageSetupDialog pageSetupDialog = new PageSetupDialog( );
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog( );
}
預覽列印protected void FileMenuItem_PrintView_Click( object sender,EventArgs e )
{
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog( );
printPreviewDialog.Document = printDocument;
lineReader = new StringReader( textBox.Text );
try
{
printPreviewDialog.ShowDialog( );
}
catch( Exception excep )
{
MessageBox.Show( excep.Message, "列印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
列印就可以直接調用printDocument的Print( )方法因為使用者可能在列印之前還要再更改列印設定所以在這裡再次顯示列印設定對話方塊protected void FileMenuItem_Print_Click( object sender,EventArgs e )
{
PrintDialog printDialog = new PrintDialog( );
printDialog.Document = printDocument;
lineReader = new StringReader( textBox.Text );
if ( printDialog.ShowDialog( ) == DialogResult.OK )
{
try
{
printDocument.Print( );
}
catch( Exception excep )
{
MessageBox.Show( excep.Message, "列印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error );
printDocument.PrintController.OnEndPrint( printDocument,new PrintEventArgs( ) );
}
}
}