圖表列印

您可以在適用於 Windows Form 的 Chart 控制項中列印圖表圖片。若要這麼做,請使用 Chart 控制項的 Printing 屬性。這個物件會列印目前資料檢視中除了捲軸之外的所有 Chart 控制項項目。

您可以叫用 [列印] 對話方塊或在背景中列印。在 Printing 物件中,PrintDocument 屬性可讓您設定列印屬性,例如頁面邊界。

自訂列印

若要在包含其他文件項目的文件中列印圖表圖片,請在 PrintPageEventHandler 內部叫用 PrintPaint 方法。您必須將 PrintPageEventArgs 物件的 Graphics 屬性,以及定義文件中圖表圖片位置的 Rectangle 物件傳遞給 PrintPaint 方法。

下列程式碼示範如何在文件中依序列印一行文字、圖表圖片以及另一行文字。

' Create new PrintDocument 
Dim pd As New System.Drawing.Printing.PrintDocument() 
' Add the event handler, and then print 
AddHandler pd.PrintPage, AddressOf pd_PrintPage 
' Print the document 
pd.Print() 
... 
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) 
   ' Create and initialize print font 
   Dim printFont As New System.Drawing.Font("Arial", 10) 
   ' Create Rectangle structure, used to set the position of the chart 
   Dim myRec As New System.Drawing.Rectangle(10, 30, 150, 150) 
   ' Draw a line of text, followed by the chart, and then another line of text 
   ev.Graphics.DrawString("Line before chart", printFont, Brushes.Black, 10, 10) 
   chart1.Printing.PrintPaint (ev.Graphics, myRec) 
   ev.Graphics.DrawString("Line after chart", printFont, Brushes.Black, 10, 200) 
End Sub
// Create new PrintDocument 
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); 
// Add a PrintPageEventHandler for the document 
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); 
// Print the document 
pd.Print(); 
...
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
   // Create and initialize print font 
   System.Drawing.Font printFont = new System.Drawing.Font("Arial", 10); 
   // Create Rectangle structure, used to set the position of the chart Rectangle 
   myRec = new System.Drawing.Rectangle(10, 30, 150, 150); 
   // Draw a line of text, followed by the chart, and then another line of text 
   ev.Graphics.DrawString("Line before chart", printFont, Brushes.Black, 10, 10); 
   chart1.Printing.PrintPaint (ev.Graphics, myRec); 
   ev.Graphics.DrawString("Line after chart", printFont, Brushes.Black, 10, 200); 
}

請參閱

其他資源

使用 Chart 控制項