Printing.PrintDocument (.Net 2.0) にヘッダーとフッターを追加する最も簡単な方法は?

StackOverflow https://stackoverflow.com/questions/59213

質問

プラグインまたは設計時にヘッダーとフッターを .Net PrintDocument オブジェクトに追加する最も簡単な方法は何ですか?

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

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

VB.Net2.0を使用しています。

ご協力いただきありがとうございます!

役に立ちましたか?

解決

printdocument オブジェクトは、印刷するページごとに printpage イベントを起動します。printpageeventargs イベント パラメータを使用して、テキスト/行などを印刷キューに描画できます。

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

グリッドに渡すときに WithEvents で薄暗くして、イベントを処理できるようにします。

他のヒント

続く ブージーボーイの答え、これが私が思いついたものです(例として簡略化しました)。

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 描画されたアイテムを整列させます。

再度、感謝します ブージーボーイ ポインタについては、ReportPage.Graphics() に到達することがまさに私が求めていたものでした:o)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top