代码之家  ›  专栏  ›  技术社区  ›  jitbit

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

  •  4
  • jitbit  · 技术社区  · 16 年前

    从实用角度或在设计时,向.Net PrintDocument对象添加页眉和页脚的最简单方法是什么?

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

    我正在使用VB.NET2.0。

    谢谢你的帮助!

    2 回复  |  直到 16 年前
        1
  •  5
  •   Community c0D3l0g1c    7 年前

    下列的 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
  •  4
  •   Booji Boy    16 年前

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

    将其传递到网格时,使用事件将其暗显,以便处理事件。