Le moyen le plus simple d’ajouter un en-tête et un pied de page à un Printing.PrintDocument (.Net 2.0) ?

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

Question

Quel est le moyen le plus simple d'ajouter un en-tête et un pied de page à un objet .Net PrintDocument, de manière pragmatique ou au moment de la conception ?

Plus précisément, j'essaie d'imprimer un contrôle de grille tiers (Infragistics GridEx v4.3), qui prend un objet PrintDocument et s'y dessine.

La page résultante contient uniquement la grille et son contenu. Cependant, j'aimerais ajouter un en-tête ou un titre pour identifier le rapport imprimé, et éventuellement un pied de page pour indiquer qui l'a imprimé, quand et idéalement un numéro de page et le nombre total de pages.

J'utilise VB.Net 2.0.

Merci pour votre aide!

Était-ce utile?

La solution

L'objet printdocument déclenche l'événement printpage pour chaque page à imprimer.Vous pouvez dessiner du texte/des lignes/etc dans la file d'attente d'impression à l'aide du paramètre d'événement printpageeventargs :

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

Atténuez-le WithEvents lorsque vous le transmettez à la grille, afin de pouvoir gérer l'événement.

Autres conseils

Suivant booji-garçonRéponse, voici ce que j'ai trouvé (que j'ai simplifié à titre d'exemple) :

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

J'ai dû jouer avec les valeurs de e.PageBounds.Height - x pour aligner les éléments dessinés.

Merci encore Garçon Booji pour le pointeur - accéder à ReportPage.Graphics() était exactement ce que je recherchais :o)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top