MÉTODO PRINTDOCUMENT.PRINT - RELATÓRIO
Pessoal estou com esse código para usar no PrintDocument, até o 18 registro no banco ele funciona ok, após os próximos lançamentos que deveria mudar de folha ele não o faz, e começa a gerar varias paginas em loop conforme a imagem.
Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
Dim ProcCliente As String
ProcCliente = [Ô]select * from cadastroclientes ORDER BY id_codigo ASC[Ô]
Dim strservcli01 As String = My.Settings.locacaoConnectionString
Dim dr_Cliente As MySqlDataReader
Dim Con_Cliente As New MySqlConnection(strservcli01)
Dim Cmd_Cliente As New MySqlCommand(ProcCliente, Con_Cliente)
Con_Cliente.Open()
dr_Cliente = Cmd_Cliente.ExecuteReader
[ô]
[ô] Variaveis das linhas
[ô]
Dim LinhasPorPagina As Single = 0
Dim PosicaoDaLinha As Single = 0
Dim LinhaAtual As Integer = 0
PrintDocument2.DefaultPageSettings.Landscape = True
[ô]
[ô]Variaveis das margens
[ô]
Dim MargemEsquerda As Single = e.MarginBounds.Left
Dim MargemSuperior As Single = e.MarginBounds.Top + 100
Dim MargemDireita As Single = e.MarginBounds.Right
Dim MargemInferior As Single = e.MarginBounds.Bottom
Dim CanetaDaImpressora As Pen = New Pen(Color.Black, 1)
[ô]
[ô]Variaveis das fontes
[ô]
Dim FonteNegrito As Font
Dim FonteTitulo As Font
Dim FonteSubTitulo As Font
Dim FonteRodape As Font
Dim FonteNormal As Font
[ô]
[ô]Define efeitos em fontes
[ô]
FonteNegrito = New Font([Ô]Arial[Ô], 9, FontStyle.Bold)
FonteTitulo = New Font([Ô]Arial[Ô], 15, FontStyle.Bold)
FonteSubTitulo = New Font([Ô]Arial[Ô], 11, FontStyle.Bold)
FonteRodape = New Font([Ô]Arial[Ô], 8)
FonteNormal = New Font([Ô]Arial[Ô], 9)
LinhaAtual = 0
PaginaAtual = 1
Dim RelatorioTitulo As String = Mid(Me.Text, 1, 25)
LinhasPorPagina = e.MarginBounds.Height / FonteNormal.GetHeight(e.Graphics) - 10
PosicaoDaLinha = MargemSuperior + (LinhaAtual * FonteNormal.GetHeight(e.Graphics))
If PaginaAtual = 1 Then
e.Graphics.DrawLine(CanetaDaImpressora, 18, 60, MargemDireita, 60)
e.Graphics.DrawImage(Image.FromFile(([Ô]C: este\[Ô] & [Ô]teste_logo.gif[Ô])), 15, 68)
e.Graphics.DrawString(RelatorioTitulo, FonteTitulo, Brushes.Black, MargemEsquerda + 150, 120, New StringFormat())
e.Graphics.DrawLine(CanetaDaImpressora, 18, 60, MargemDireita, 60)
e.Graphics.DrawString([Ô]Código[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda - 80, 160, New StringFormat())
e.Graphics.DrawString([Ô]Sigla[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda - 10, 160, New StringFormat())
e.Graphics.DrawString([Ô]Nome[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda + 120, 160, New StringFormat())
e.Graphics.DrawString([Ô]Endereço[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda + 370, 160, New StringFormat())
e.Graphics.DrawString([Ô]Número[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda + 640, 160, New StringFormat())
e.Graphics.DrawLine(CanetaDaImpressora, 20, 190, MargemDireita, 190)
Else
e.Graphics.DrawLine(CanetaDaImpressora, 18, 60, MargemDireita, 60)
e.Graphics.DrawString([Ô]MatrÃcula[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Red, MargemEsquerda - 80, 140, New StringFormat())
e.Graphics.DrawString([Ô]Nome[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Red, MargemEsquerda - 10, 140, New StringFormat())
e.Graphics.DrawLine(CanetaDaImpressora, 18, 160, MargemDireita, 160)
End If
While (LinhaAtual < LinhasPorPagina AndAlso dr_Cliente.Read())
e.Graphics.DrawString(dr_Cliente.Item([Ô]id_codigo[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 30, PosicaoDaLinha, New StringFormat())
e.Graphics.DrawString(dr_Cliente.Item([Ô]nfant[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 80, PosicaoDaLinha, New StringFormat())
e.Graphics.DrawString(dr_Cliente.Item([Ô]nome[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 220, PosicaoDaLinha, New StringFormat())
e.Graphics.DrawString(dr_Cliente.Item([Ô]ende[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 470, PosicaoDaLinha, New StringFormat()) [ô]280-200)
e.Graphics.DrawString(dr_Cliente.Item([Ô]nr[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 770, PosicaoDaLinha, New StringFormat()) [ô]280-200)
LinhaAtual += 1
PosicaoDaLinha += 18
End While
e.Graphics.DrawLine(CanetaDaImpressora, MargemEsquerda, MargemInferior, MargemDireita, MargemInferior)
e.Graphics.DrawString(System.DateTime.Now.ToString(), FonteRodape, Brushes.Black, MargemEsquerda, MargemInferior, New StringFormat())
LinhaAtual += CInt(FonteNormal.GetHeight(e.Graphics))
LinhaAtual += 1
e.Graphics.DrawString([Ô]Pagina : [Ô] & paginaAtual, FonteRodape, Brushes.Black, MargemDireita - 50, MargemInferior, New StringFormat())
paginaAtual += 1
[ô]Incrementa o número da pagina
paginaAtual += 1
[ô]verifica se continua imprimindo
If (LinhaAtual > LinhasPorPagina) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Con_Cliente.Close()
dr_Cliente.Close()
End Sub
Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
Dim ProcCliente As String
ProcCliente = [Ô]select * from cadastroclientes ORDER BY id_codigo ASC[Ô]
Dim strservcli01 As String = My.Settings.locacaoConnectionString
Dim dr_Cliente As MySqlDataReader
Dim Con_Cliente As New MySqlConnection(strservcli01)
Dim Cmd_Cliente As New MySqlCommand(ProcCliente, Con_Cliente)
Con_Cliente.Open()
dr_Cliente = Cmd_Cliente.ExecuteReader
[ô]
[ô] Variaveis das linhas
[ô]
Dim LinhasPorPagina As Single = 0
Dim PosicaoDaLinha As Single = 0
Dim LinhaAtual As Integer = 0
PrintDocument2.DefaultPageSettings.Landscape = True
[ô]
[ô]Variaveis das margens
[ô]
Dim MargemEsquerda As Single = e.MarginBounds.Left
Dim MargemSuperior As Single = e.MarginBounds.Top + 100
Dim MargemDireita As Single = e.MarginBounds.Right
Dim MargemInferior As Single = e.MarginBounds.Bottom
Dim CanetaDaImpressora As Pen = New Pen(Color.Black, 1)
[ô]
[ô]Variaveis das fontes
[ô]
Dim FonteNegrito As Font
Dim FonteTitulo As Font
Dim FonteSubTitulo As Font
Dim FonteRodape As Font
Dim FonteNormal As Font
[ô]
[ô]Define efeitos em fontes
[ô]
FonteNegrito = New Font([Ô]Arial[Ô], 9, FontStyle.Bold)
FonteTitulo = New Font([Ô]Arial[Ô], 15, FontStyle.Bold)
FonteSubTitulo = New Font([Ô]Arial[Ô], 11, FontStyle.Bold)
FonteRodape = New Font([Ô]Arial[Ô], 8)
FonteNormal = New Font([Ô]Arial[Ô], 9)
LinhaAtual = 0
PaginaAtual = 1
Dim RelatorioTitulo As String = Mid(Me.Text, 1, 25)
LinhasPorPagina = e.MarginBounds.Height / FonteNormal.GetHeight(e.Graphics) - 10
PosicaoDaLinha = MargemSuperior + (LinhaAtual * FonteNormal.GetHeight(e.Graphics))
If PaginaAtual = 1 Then
e.Graphics.DrawLine(CanetaDaImpressora, 18, 60, MargemDireita, 60)
e.Graphics.DrawImage(Image.FromFile(([Ô]C: este\[Ô] & [Ô]teste_logo.gif[Ô])), 15, 68)
e.Graphics.DrawString(RelatorioTitulo, FonteTitulo, Brushes.Black, MargemEsquerda + 150, 120, New StringFormat())
e.Graphics.DrawLine(CanetaDaImpressora, 18, 60, MargemDireita, 60)
e.Graphics.DrawString([Ô]Código[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda - 80, 160, New StringFormat())
e.Graphics.DrawString([Ô]Sigla[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda - 10, 160, New StringFormat())
e.Graphics.DrawString([Ô]Nome[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda + 120, 160, New StringFormat())
e.Graphics.DrawString([Ô]Endereço[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda + 370, 160, New StringFormat())
e.Graphics.DrawString([Ô]Número[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Black, MargemEsquerda + 640, 160, New StringFormat())
e.Graphics.DrawLine(CanetaDaImpressora, 20, 190, MargemDireita, 190)
Else
e.Graphics.DrawLine(CanetaDaImpressora, 18, 60, MargemDireita, 60)
e.Graphics.DrawString([Ô]MatrÃcula[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Red, MargemEsquerda - 80, 140, New StringFormat())
e.Graphics.DrawString([Ô]Nome[Ô], New Font([Ô]arial[Ô], 11, FontStyle.Regular), Brushes.Red, MargemEsquerda - 10, 140, New StringFormat())
e.Graphics.DrawLine(CanetaDaImpressora, 18, 160, MargemDireita, 160)
End If
While (LinhaAtual < LinhasPorPagina AndAlso dr_Cliente.Read())
e.Graphics.DrawString(dr_Cliente.Item([Ô]id_codigo[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 30, PosicaoDaLinha, New StringFormat())
e.Graphics.DrawString(dr_Cliente.Item([Ô]nfant[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 80, PosicaoDaLinha, New StringFormat())
e.Graphics.DrawString(dr_Cliente.Item([Ô]nome[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 220, PosicaoDaLinha, New StringFormat())
e.Graphics.DrawString(dr_Cliente.Item([Ô]ende[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 470, PosicaoDaLinha, New StringFormat()) [ô]280-200)
e.Graphics.DrawString(dr_Cliente.Item([Ô]nr[Ô]).ToString, New Font([Ô]arial[Ô], 9, FontStyle.Regular), Brushes.Black, 770, PosicaoDaLinha, New StringFormat()) [ô]280-200)
LinhaAtual += 1
PosicaoDaLinha += 18
End While
e.Graphics.DrawLine(CanetaDaImpressora, MargemEsquerda, MargemInferior, MargemDireita, MargemInferior)
e.Graphics.DrawString(System.DateTime.Now.ToString(), FonteRodape, Brushes.Black, MargemEsquerda, MargemInferior, New StringFormat())
LinhaAtual += CInt(FonteNormal.GetHeight(e.Graphics))
LinhaAtual += 1
e.Graphics.DrawString([Ô]Pagina : [Ô] & paginaAtual, FonteRodape, Brushes.Black, MargemDireita - 50, MargemInferior, New StringFormat())
paginaAtual += 1
[ô]Incrementa o número da pagina
paginaAtual += 1
[ô]verifica se continua imprimindo
If (LinhaAtual > LinhasPorPagina) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Con_Cliente.Close()
dr_Cliente.Close()
End Sub
dá uma procurada no site que tem vários exemplos, inclusive deve ter um tópico com o esmo problema, se não me engano
Faça seu login para responder