2014-04-08 28 views
0

我想打印數據網格中的數據。該代碼適用於第一頁,但註釋行不能正常工作,不會移動到下一頁。誰能幫助解決這個問題嗎?PrintDocument HasMorePages不工作

private void DrawFactorA4(object sender, PrintPageEventArgs ev) 
    { 
    for (int j = 0; j < GrdRDocument.Rows.Count; j++) 
     { 
      i += 2; 
      //draw data grid 
      s++; 
      if(s == 10) 
      {  
       //ev.HasMorePages = true; //this line doesn't work 
       s = 0; 
       i = 0;     
      } 
      else 
      { 
       ev.HasMorePages = false;      
      } 
     } 
    } 

_

private void BtnPrint_Click(object sender, EventArgs e) 
    { 
     printFont = new Font("Arial", 12); 

     IEnumerable<PaperSize> paperSizes = 

     pd.PrinterSettings.PaperSizes.Cast<PaperSize>(); 

     sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4); 

     pd.DefaultPageSettings.Landscape = true; 

     pd.DefaultPageSettings.PaperSize = sizeA4; 

     pd.PrintPage += new PrintPageEventHandler(this.DrawFactorA4); 

     printPreviewDialog.Document = pd; 

     printPreviewDialog.ShowDialog(); 
    } 
+0

它工作正常,它只是使用錯誤太頻繁。就像它在這裏一樣,你必須離開for()循環。當你的事件處理程序再次被調用時,在下一行繼續,這需要'j'成爲你的類的一個字段。並且需要實施BeginPrint,因此您可以將其設置爲0。 –

回答

2

靜下心來看你有什麼:

printFont = new Font("Arial", 12); 

字體都是非託管資源;在這裏你即刻一個,永不放棄。也許在這種特殊情況下這是無害的,但這是一個不好的習慣。

pd.PrintPage += new PrintPageEventHandler(this.DrawFactorA4); 

DrawFactorA4將在文檔中的每個頁面中調用。內部DrawFactorA4

for (int j = 0; j < GrdRDocument.Rows.Count; j++) 

您通過GrdRDocument每迭代,行無論數量或頁面的大小。那是錯的;你必須在頁面填滿後停止。順便說一下,我希望GrdRDocument是不可變數據的本地副本,並且您不會將UI控件傳遞給打印線程。

s++; 
if(s == 10) 
{  
    //ev.HasMorePages = true; //this line doesn't work 
    s = 0; 

您的評論行將工作正常。問題是你設置了ev.HasMorePages = true,然後忽略它;您設置s = 0並繼續迭代; !下一次迭代S = 10,所以你:

ev.HasMorePages = false; 

閱讀PrintDocument文檔;它有一個打印多個頁面的例子。您應該創建一個類來存儲所有非託管資源和頁面狀態。使它IDisposable,使他們得到處置。僅遍歷行或任何想要在每個頁面上打印的內容。例如:

class PrintStuff : IDisposable 
{ 
    readonly IEnumerable<Whatever> data; 
    readonly PrintDocument pd; 
    Font font; 
    private int currentIndex; 

    public PrintStuff(IEnumerable<Whatever> data) 
    { 
     this.data = data; 

     pd = new PrintDocument(); 
     pd.BeginPrint += OnBeginPrint; 
     pd.PrintPage += OnPrintPage; 
     pd.EndPrint += OnEndPrint; 
    } 

    public void Print() 
    { 
     pd.Print(); 
    } 

    public void Dispose() 
    { 
     pd.Dispose(); 
    } 

    private void OnBeginPrint(object sender, PrintEventArgs args) 
    { 
     font = new Font(FontFamily.GenericSansSerif, 12F); 
     currentIndex = 0; 
    } 

    private void OnEndPrint(object sender, PrintEventArgs args) 
    { 
     font.Dispose(); 
    } 

    private void OnPrintPage(object sender, PrintPageEventArgs args) 
    { 
     var x = Convert.ToSingle(args.MarginBounds.Left); 
     var y = Convert.ToSingle(args.MarginBounds.Top); 
     var lineHeight = font.GetHeight(args.Graphics); 
     while ((currentIndex < data.Count()) 
       && (y <= args.MarginBounds.Bottom)) 
     { 
      args.Graphics.DrawWhatever(data.ElementAt(currentIndex), font, Brushes.Black, x, y); 
      y += lineHeight; 
      currentIndex++; 
     } 

     args.HasMorePages = currentIndex < data.Count(); 
    } 
}