将页眉和页脚添加到 Printing.PrintDocument (.Net 2.0) 的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/59213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:49:49  来源:igfitidea点击:

Easiest way to add a Header and Footer to a Printing.PrintDocument (.Net 2.0)?

.netvb.netprintingheaderprintdocument

提问by Andrew

What's the easiest way to add a header and footer to a .Net PrintDocument object, either pragmatically or at design-time?

将页眉和页脚添加到 .Net PrintDocument 对象的最简单方法是实用的还是在设计时?

Specifically I'm trying to print a 3rd party grid control (Infragistics GridEx v4.3), which takes a PrintDocument object and draws itself into it.

具体来说,我正在尝试打印一个 3rd 方网格控件(Infragistics GridEx v4.3),它接受一个 PrintDocument 对象并将其自身绘制到其中。

The resulting page just contains the grid and it's contents - however I would like to add a header or title to identify the printed report, and possibly a footer to show who printed it, when, and ideally a page number and total pages.

结果页面只包含网格及其内容 - 但是我想添加一个标题或标题来标识打印的报告,可能还有一个页脚来显示谁打印了它,何时打印,最好是页码和总页数。

I'm using VB.Net 2.0.

我正在使用 VB.Net 2.0。

Thanks for your help!

谢谢你的帮助!

采纳答案by Booji Boy

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

printdocument 对象为要打印的每一页触发 printpage 事件。您可以使用 printpageeventargs 事件参数将文本/线条/等绘制到打印队列中:

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

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

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

当您将其传递给网格时,将其与事件调暗,以便您可以处理该事件。

回答by Andrew

Following booji-boy's answer, here's what I came up with (which I've simplified for example purposes):

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

I had to play with the values of e.PageBounds.Height - xto get the drawn items to line up.

我必须使用 的值e.PageBounds.Height - x才能使绘制的项目对齐。

Thanks again Booji Boyfor the pointer - getting at the ReportPage.Graphics() was exactly what I was after :o)

再次感谢Booji Boy的指点 - 获得 ReportPage.Graphics() 正是我所追求的 :o)