2008-09-12 28 views
4

実際にまたはデザイン時に、.Net PrintDocumentオブジェクトにヘッダーとフッターを追加する最も簡単な方法は何ですか?Printing.PrintDocument(.Net 2.0)にヘッダーとフッターを追加する最も簡単な方法は?

具体的には、PrintDocumentオブジェクトを受け取り、それ自体を描画するサードパーティのグリッドコントロール(Infragistics GridEx v4.3)を印刷しようとしています。

結果のページにはグリッドとその内容しか含まれていませんが、印刷されたレポートを識別するためのヘッダーやタイトルを追加したい場合は、誰が印刷したか、いつ、そして理想的にはページ番号と合計を表示するページ。

私はVB.Net 2.0を使用しています。

ありがとうございました!

+0

印刷のPrintDocument自分のヘッダー&フッター? – kokos

+0

正しい。デザイン時にプレースホルダを作成するか、実行時にPrintDocumentに直接描画することで – Andrew

答えて

4

printdocumentオブジェクトは、印刷するページごとにprintpageイベントを発生させます。あなたはprintpageeventargsイベント・パラメータを使用して印刷キューにテキスト/ラインの/ etc描くことができます:あなたがイベントを処理できるよう

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

は、あなたがグリッドにそれを渡すとき、それはWITHEVENTS薄暗いです。

+0

チップをありがとう、私はそれを試してみましょう; o) – Andrew

+1

甘い、問題ありません! –

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)

関連する問題