學習.Net(c#)打印--頁面設置

要進行版面設置需要使用 PageSetupDialog 類。

PageSetupDialog
    可以配置頁面大小和紙張來源、方向、頁邊距,因為這些選項都依賴於打印機,所以在這個對話框中也可以選擇打印機。

它有一些屬性如下圖:

    學習.Net(c#)打印--頁面設置_第1张图片

說明:

1、頁面
    AllowPaper
:是否可選擇頁面大小和紙張來源。
    PageSetupDialog.PageSetting.PaperSize
屬性返回一個PaperSize實例,用它的屬性HeightWidthPaperName可以讀取高度、寬度和紙張名稱。
    PaperName
指定諸如LetterA4等名稱。
    Kind
屬性返回一個枚舉,從中可獲取PaperKind枚舉的一個值,PaperKind枚舉包含許多不同的紙張大小,例如A3A4A5LetterLetterPlusLetterRotated
    PageSetupDiaog.PageSettings.PaperSource
屬性返回一個PaperSource實例,其中可以讀取打印機紙張來源和相應的紙張類型(只要打印機用該打印機設置進行了正確的配置)。

2、頁邊距
    AllowMargins
屬性允許用戶設置打印輸出的頁邊距值。
    MinMargins
可以為用戶定義輸入的最小頁邊距值.
    PageSetupDialog.PageSettings.Margins
讀取頁邊距,值有Bottom,Left,RightTop屬性。

3、方向
    AllowOrientation
屬性定義用戶是否可以選擇縱向和橫向打印方式。
    PageSetupDialog.PageSettings.Landscape
值可以讀取選定的值。橫向為True,縱向為False

4、打印機
AllowPrinter屬性指定用戶是否可選擇打印機。



PageSetupDialog的用法

   使用PageSetupDialog的方法比較簡單,只需
    1、在實例化一個
PageSetupDialog
    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();   
        
        
// 1、打印格式設置頁面
        PageSetupDialog dlgPageSetup  =   new  PageSetupDialog();

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

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


        
///   <summary>
        
///  3、在頁面設置按鈕事件中呼叫頁面設置界面,這樣單擊“頁面設置”按鈕就會彈出“頁面設置”界面
        
///   </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
            {
                
// 調用打印
                pdDocument.Print();

                
/*
                 * PrintDocument對象的Print()方法在PrintController類的幫助下,執行PrintPage事件。
                 
*/
            }
            
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 ;
        }
    }
}

    版面是可以設置了,可我們想設定打印的相關選項(如,打印當前頁、選擇的部分、打印到文件等)如何設呢?

    請看下頁:學習.Net(c#)打印--調用打印界面



你可能感兴趣的:(.net)