【问题标题】:Easiest way to add a Header and Footer to a Printing.PrintDocument (.Net 2.0)?向 Printing.PrintDocument (.Net 2.0) 添加页眉和页脚的最简单方法?
【发布时间】:2010-09-08 17:19:54
【问题描述】:

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

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

生成的页面只包含网格及其内容 - 但是我想添加一个页眉或标题来标识打印的报告,可能还有一个页脚来显示谁打印它,何时打印,理想情况下是页码和总页数.

我正在使用 VB.Net 2.0。

感谢您的帮助!

【问题讨论】:

  • 自己在 PrintDocument 上打印页眉和页脚?
  • 正确。通过在设计时创建占位符,或在运行时直接绘制到 PrintDocument

标签: .net vb.net printing header printdocument


【解决方案1】:

根据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

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

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

【讨论】:

    【解决方案2】:

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

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

    当你将它传递给网格时,用事件调暗它,这样你就可以处理事件了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-13
      • 2010-12-03
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 2015-01-07
      相关资源
      最近更新 更多