2012-10-21 97 views
0

我很想弄清楚如何讓MSChart在打印時填充整個頁面。有人能告訴我爲什麼下面的代碼不起作用和/或提出另一種方法。我爲自己的無知道歉,但迄今爲止所做的所有打印都是通過Crystal和其他報告框架完成的,所以我通常不必直接使用PrintDialog和PrintPreviewDialog。提前致謝。將MSChart調整爲適合頁面大小打印前

// Printing Code 
Size pageSize = new Size(this.Chart.Printing.PrintDocument.DefaultPageSettings.PaperSize.Width, this.Chart.Printing.PrintDocument.DefaultPageSettings.PaperSize.Height); 
this.Chart.Size = pageSize; 

// This properly previews my chart but, it does not fill up the page      
this.Chart.Printing.PrintPreview(); 

回答

3

好吧,所以我終於想出瞭如何完成我正在嘗試做的事情。它歸結爲我不理解如何使用Graphics對象的PrintDocument。下面是我用來完成任務的代碼。我真的希望這有助於某人。另外,我強烈推薦MSFT Chart示例代碼。當我創建這些圖表時,它已被證明對我非常重要。鏈接是:http://archive.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=4418

public void PrintPreviewIncompleteJobsByStatus() 
    { 
     // Set new print document with custom page printing event handler 
     chart.Printing.PrintDocument = new PrintDocument(); 
     chart.Printing.PrintDocument.PrintPage += new PrintPageEventHandler(ChartGenericFormat_PrintPage); 

     chart.Printing.PrintDocument.DefaultPageSettings.Landscape = true; 

     // Print preview chart 
     chart.Printing.PrintPreview(); 
    } 

    private void ChartGenericFormat_PrintPage(object sender, PrintPageEventArgs ev) 
    { 
     // Calculate first chart position rectangle 
     Rectangle chartPosition = new Rectangle(ev.MarginBounds.X, ev.MarginBounds.Y, ev.MarginBounds.Width, ev.MarginBounds.Height); 

     // Draw chart on the printer graphics 
     chart.Printing.PrintPaint(ev.Graphics, chartPosition); 

    }