Easiest way to add headers and footers in Print.PrintDocument (.Net 2.0)?

What is the easiest way to add headers and footers to a .Net PrintDocument object, both pragmatically and at design time?

In particular, I am trying to print a third-party network control (Infragistics GridEx v4.3) that accepts and draws into a PrintDocument.

The resulting page contains only the grid and its contents. However, I would like to add a heading or heading to define the printed report, and possibly a footer, to show who printed it, when and ideally the page number and shared pages.

I am using VB.Net 2.0.

Thank you for your help!

+4
source share
2 answers

The printdocument object fires a print event for each page to print. You can draw text / lines / etc in the print queue using the printpageeventargs event parameter:

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

Drop WithEvents when you pass it to the grid so you can handle the event.

+4
source

Following booji-boy , here is what I came up with (which I simplified for example):

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 

I needed to play with the values โ€‹โ€‹of e.PageBounds.Height - x to draw the drawn elements into a line.

Thanks again to Booji Boy for the pointer - getting in ReportPage.Graphics () was exactly what I was after: o)

+4
source

Source: https://habr.com/ru/post/1276666/


All Articles