2008-09-12 177 views
4

什麼是頁眉和頁腳添加到一個.net的PrintDocument對象的最簡單的方法,無論是務實還是在設計時?最簡單的方法將一個頁眉和頁腳添加到Printing.PrintDocument(.Net 2.0)?

具體我想打印一個第三方網格控制(Infragistics的當Gridex V4.3),這需要一個PrintDocument的對象和它本身吸引進去。

生成的頁面只包含網格和它的內容 - 但是我想添加一個標題或標題來標識打印的報告,並可能顯示誰打印它的頁腳,何時和理想情況下的頁碼和總數頁面。

我使用VB.Net 2.0。

感謝您的幫助!

+0

打印上自己的PrintDocument頁眉和頁腳? – kokos 2008-09-14 13:59:49

+0

正確。通過創建在設計時的佔位符,或者通過在運行時的提示,直接繪製到的PrintDocument – Andrew 2008-09-14 20:15:33

回答

4

booji-boy的回答,這裏就是我想出了(我已經簡化,例如用途):

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click 

     Dim oDoc As New Printing.PrintDocument 
     oDoc.DefaultPageSettings.Landscape = True 
     AddHandler oDoc.PrintPage, AddressOf PrintPage 

     oDoc.DocumentName = "Printout" 

     InfragisticsWinGrid.PrintPreview(InfragisticsWinGrid.DisplayLayout, oDoc) 

    End If 
End Sub 


Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) 

    ' Draw title 
    e.Graphics.DrawString("Report Title"), New Font("Arial", 16), Brushes.Black, 95, 70) 

    ' Draw footer 
    e.Graphics.DrawImage(DirectCast(mResources.GetObject("footer_logo"), Drawing.Bitmap), 95, e.PageBounds.Height - 87) 
    Dim drawFont As New Font("Arial", 8.75) 

    e.Graphics.DrawString("Report Title", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 90) 
    e.Graphics.DrawString("Printed", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 76) 
    e.Graphics.DrawString("Printed By", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 62) 

    ' Draw some grid lines to add structure to the footer information 
    e.Graphics.DrawLine(Pens.Gray, 246, e.PageBounds.Height - 90, 246, e.PageBounds.Height - 48) 
    e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 75, 550, e.PageBounds.Height - 75) 
    e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 61, 550, e.PageBounds.Height - 61) 

    e.Graphics.DrawString("Report", drawFont, Brushes.Black, 250, e.PageBounds.Height - 90) 
    e.Graphics.DrawString(Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString, drawFont, Brushes.Black, 250, e.PageBounds.Height - 76) 
    e.Graphics.DrawString("Andrew", drawFont, Brushes.Black, 250, e.PageBounds.Height - 62) 

End Sub 

我曾與e.PageBounds.Height - x價值發揮到拿到繪製的項目排隊向上。

再次感謝Booji Boy的指針 - 讓在ReportPage.Graphics()正是我後:O)