學習.Net(c#)打印--調用打印界面

要想彈出“打印”窗口,可以在程式中呼叫 PrintDialog 界面。
    PrintDialog 類允許用戶從已安裝的打印機中選擇一台打印機,選擇打印機份數和其它一找印設置,例如佈局和打印機紙張來源。

    PrintDialog相關屬性如下圖:


 

 


 

說明:
    AllowPrintToFile
:允許打印到文件選項
    AllowSelection
:允許打印選中的文本
    PrintRange:允許打印範圍
    AllowSomePage
允許打印頁面範圍
    PrinterSettings.FromPage
從哪頁開始
    PrinterSettings.ToPage
到哪頁結束
      注:
打印任務的開始可以調用OnBeginPrint(),訪問PrintDialog.PrinterSettings.PrintRage屬性獲得用戶的Selection選項信息。此值為枚舉類型。

 

PrintDialog的用法以與PageSetupDialog用法類似。

即:

1、在實例化一個PrintDialog
  2
、設置document屬性設置為需要打印的文檔
  3
、呼叫ShowDialog()方法,打開“打印”界面。

代碼如下: ( 我們對前次的代碼作變變更,在“打印”按鈕事件中加入彈出“打印”界面的代碼。 )


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();

        
// 1、 實例化打印頁面
        PrintDialog dlgPrint  =   new  PrintDialog();

        
private   string [] lines;
        
private   int  linesPrinted;
        
public  SimpleEditorForm()
        {
            InitializeComponent();

            
// 訂閱事件

            
// 訂閱PinrtPage事件,用於繪製各個頁內容
            pdDocument.PrintPage  +=   new  PrintPageEventHandler(OnPrintPage);
            
// 訂閱BeginPrint事件,用於得到被打印的內容
            pdDocument.BeginPrint  +=   new  PrintEventHandler(pdDocument_BeginPrint);
            
// 訂閱EndPrint事件,用於釋放資源
            pdDocument.EndPrint  +=   new  PrintEventHandler(pdDocument_EndPrint);

            
// 頁面設置的打印文檔設置為需要打印的文檔
            dlgPageSetup.Document  =  pdDocument;

            
// 2、 打印界面的打印文檔設置為被打印文檔
            dlgPrint.Document  =  pdDocument;
        }


        
///   <summary>
        
///  在頁面設置按鈕事件中呼叫頁面設置界面,這樣單擊“頁面設置”按鈕就會彈出“頁面設置”界面
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         private   void  OnFilePageSetup( object  sender, EventArgs e)
        {
            dlgPageSetup.ShowDialog();
        }

        
private   void  OnFilePrint( object  sender, EventArgs e)
        {   
            
try
            {
                
// 判斷是否有選擇文本
                 if  (textBoxEdit.SelectedText  !=   "" )
                {
                    
// 如果有選擇文本,則可以選擇"打印選擇的範圍"
                    dlgPrint.AllowSelection  =   true ;
                }
                
else
                {
                    dlgPrint.AllowSelection 
=   false ;
                }
                
// 3、呼叫打印界面
                 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);

            
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  =   20 ;
            
int  y  =   20 ;
            
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事件,釋放資源
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         void  pdDocument_EndPrint( object  sender, PrintEventArgs e)
        {
           
// 變量Lines占用和引用的字符串數組,現在釋放
            lines  =   null ;
        }
    }
}

這樣我們就可以打印界面中選擇相關設定了。現在文件可以打印了,版面也可以設置了,在打印界面中也可作相關設定了,可是我們如何預覽列印呢?

转载于:https://www.cnblogs.com/82767136/articles/1254527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值