domingo, 10 de junho de 2007

Codigo para criação de relatórios com PrintDocument

Public Class Form2

Private _PaginaAtual As Integer

Private _X As Single

Private _Y As Single

Private _MargemSuperior As Single

Private _Registro As Integer

Private _Fonte As System.Drawing.Font

Private tbDados As DataTable

Private _AlturaDoCabecalho As Single

Property AlturaDoCabecalho() As Single

Get

Return _AlturaDoCabecalho

End Get

Set(ByVal value As Single)

_AlturaDoCabecalho = value

End Set

End Property

Private Property Registro() As Integer

Get

Return _Registro

End Get

Set(ByVal value As Integer)

_Registro = value

End Set

End Property

Property MargemSuperior() As Single

Get

Return _MargemSuperior

End Get

Set(ByVal value As Single)

_MargemSuperior = value

End Set

End Property

Public Property X() As Single

Get

Return _X

End Get

Set(ByVal value As Single)

_X = value

End Set

End Property

Public Property Y() As Single

Get

Return _Y + Me.MargemSuperior

End Get

Set(ByVal value As Single)

_Y = value - Me.MargemSuperior

End Set

End Property

Public Property PaginaAtual() As Integer

Get

Return _PaginaAtual

End Get

Set(ByVal value As Integer)

_PaginaAtual = value

End Set

End Property

Public Property Fonte() As System.Drawing.Font

Get

If IsNothing(_Fonte) Then

_Fonte = New System.Drawing.Font("Verdana", 10)

End If

Return _Fonte

End Get

Set(ByVal value As System.Drawing.Font)

_Fonte = value

End Set

End Property

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Me.MargemSuperior = e.MarginBounds.Top

Me.X = e.MarginBounds.Left

Me.PaginaAtual += 1

Me.Cabecalho(e)

While Me.Registro <= Me.tbDados.Rows.Count - 1

e.Graphics.DrawString(Me.tbDados.Rows(Me.Registro)("ID") & " - " & Me.tbDados.Rows(Me.Registro)("NOME"), Me.Fonte, New System.Drawing.SolidBrush(Color.Black), Me.X, Me.Y)

Me.Y += Me.Fonte.Height

Me.Registro += 1

If Me.Y > (e.MarginBounds.Bottom - (Me.Fonte.Height + 3)) AndAlso (Me.Registro <= Me.tbDados.Rows.Count - 1) Then

Me.Rodape(e)

Me.Y = Me.MargemSuperior

e.HasMorePages = True

Exit Sub

End If

End While

Me.Rodape(e)

e.HasMorePages = False

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim Nomes As String() = {"Maria", "João", "José", "Joaquim", "Joana", "Valdete", "Joca", "Rosa"}

Me.tbDados = New DataTable

Me.tbDados.Columns.Add("id")

Me.tbDados.Columns.Add("Nome")

While tbDados.Rows.Count <>

Dim dr As DataRow

dr = tbDados.NewRow

dr("id") = tbDados.Rows.Count + 1

Randomize()

dr("Nome") = Nomes(CInt(Int((8 * Rnd())))) & " " & Nomes(CInt(Int((8 * Rnd()))))

tbDados.Rows.Add(dr)

End While

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Registro = 0

Me.PaginaAtual = 0

Me.PrintPreviewDialog1.Document = Me.PrintDocument1

Me.PrintPreviewDialog1.ShowDialog()

End Sub

Private Sub Cabecalho(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

e.Graphics.DrawRectangle(Pens.Red, Me.X, Me.Y, e.MarginBounds.Width, 60)

Me.Y += 0.5

e.Graphics.DrawImage(Image.FromFile("c:\divtopfundo.jpg"), Me.X, Me.Y, e.MarginBounds.Width, 60)

e.Graphics.DrawString(Now.ToString, New Font("Verdana", 10, FontStyle.Bold), New System.Drawing.SolidBrush(Color.White), e.MarginBounds.Width - 80, Me.Y)

Me.Y += 70

Dim pontos As Point() = {New Point(Me.X, Me.Y), New Point(e.MarginBounds.Width, Me.Y)}

e.Graphics.DrawLines(Pens.Black, pontos)

Me.Y += 0.5

e.Graphics.DrawString("Id: Nome:", New Font("Verdana", 10, FontStyle.Bold), New System.Drawing.SolidBrush(Color.Blue), Me.X, Me.Y)

Me.Y += 20

pontos = New Point() {New Point(Me.X, Me.Y), New Point(e.MarginBounds.Width, Me.Y)}

e.Graphics.DrawLines(Pens.Black, pontos)

Me.Y += 10

Me.AlturaDoCabecalho = Me.Y 'Acrecente este codigo

End Sub

Private Sub Rodape(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

Me.Y = e.MarginBounds.Bottom - (Me.Fonte.Height + 1)

Dim pontos As Point() = {New Point(Me.X, Me.Y), New Point(e.MarginBounds.Width, Me.Y)}

e.Graphics.DrawLines(Pens.Black, pontos)

Me.Y += 0.5

Dim TotPag As Integer

TotPag = Math.Ceiling(((Me.Fonte.Height * tbDados.Rows.Count) / (e.MarginBounds.Bottom - Me.AlturaDoCabecalho)))

e.Graphics.DrawString("Pagina " & Me.PaginaAtual & " de " & TotPag.ToString, New Font("Verdana", 10, FontStyle.Bold), New System.Drawing.SolidBrush(Color.Blue), Me.X, Me.Y)

Me.Y += Me.Fonte.Height + 0.5

pontos = New Point() {New Point(Me.X, Me.Y), New Point(e.MarginBounds.Width, Me.Y)}

e.Graphics.DrawLines(Pens.Black, pontos)

End Sub

End Class

Nenhum comentário: