¿La forma más sencilla de agregar un encabezado y pie de página a un Printing.PrintDocument (.Net 2.0)?

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

Pregunta

¿Cuál es la forma más sencilla de agregar un encabezado y pie de página a un objeto .Net PrintDocument, ya sea de forma pragmática o en tiempo de diseño?

Específicamente, estoy intentando imprimir un control de cuadrícula de terceros (Infragistics GridEx v4.3), que toma un objeto PrintDocument y se dibuja en él.

La página resultante solo contiene la cuadrícula y su contenido; sin embargo, me gustaría agregar un encabezado o título para identificar el informe impreso y posiblemente un pie de página para mostrar quién lo imprimió, cuándo e idealmente un número de página y el total de páginas.

Estoy usando VB.Net 2.0.

¡Gracias por tu ayuda!

¿Fue útil?

Solución

El objeto printdocument activa el evento printpage para cada página que se va a imprimir.Puede dibujar texto/líneas/etc. en la cola de impresión utilizando el parámetro de evento printpageeventargs:

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

Atenúelo WithEvents cuando lo pase a la cuadrícula, para que pueda manejar el evento.

Otros consejos

Siguiente chico boojiEn respuesta, esto es lo que se me ocurrió (que he simplificado a modo de ejemplo):

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

Tuve que jugar con los valores de e.PageBounds.Height - x para alinear los elementos dibujados.

gracias de nuevo chico booji para el puntero: llegar a ReportPage.Graphics() era exactamente lo que buscaba :o)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top