Jump to content

[Resolvido] Ajuda - Contar número de páginas num relatório


sardin3s
 Share

Recommended Posts

Boa tarde.

Preciso imenso da vossa ajuda.

Estou a criar um relatório em vb.net no Visual Basic 2010 Express, que basicamente apenas me imprime os dados de uma Listview.

Acontece que não consigo programar a página actual e o total de páginas do relatório.

Alguém me consegue ajudar?

Segue abaixo o código!

#Region "Print related declarations"
Protected WithEvents pd As Printing.PrintDocument
Protected Ratio As Single = 0, CurrRow As Integer = 0
#End Region

#Region "Simple Printing of ListView"
'''
'''
'''

Public Sub Print()
pd = New Printing.PrintDocument
pd.DocumentName = "Encomenda " & Now.Year & Now.Month & Now.Day & Now.Hour & Now.Minute & Now.Second
Ratio = 1
CurrRow = 0

PrintDialog1.Document = pd

If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then

Else
pd.Print()
End If

End Sub

'''
'''
'''

Public Sub PrintPreview()

pd = New Printing.PrintDocument
pd.DocumentName = "Impressão da encomenda: " & Numero_Pedido
Ratio = 1
CurrRow = 0
Dim ppv As New PrintPreviewDialog
ppv.Document = pd
ppv.MdiParent = Index
ppv.Height = 600
ppv.Width = 400

ppv.Show()

End Sub

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

Dim Linha_Colorir As Integer = 0
Dim Pagina_Atual, Pagina_Total As Integer
Dim c As ColumnHeader
Dim g As Graphics = e.Graphics
Dim l As Integer = 9 'Alinhamento Vertical
Dim iCount As Integer
Dim f As New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
Dim D_Font As New Font("Verdana", 14, FontStyle.Bold, GraphicsUnit.Point)
Dim D_Font2 As New Font("Microsoft Sans Serif", 10, FontStyle.Regular)
Dim b As Brush = Brushes.Black
Dim currentY As Integer = 0, maxY As Integer = 0 'Altura do cabeçalho para os valores
Dim gap As Integer = 1 'Espaçamento horizontal das linhas dos dados
Dim lvsi As ListViewItem.ListViewSubItem
Dim colLefts(ListView1.Columns.Count) As Integer, colWidths(ListView1.Columns.Count) As Integer, idx As Integer = 0, ii As Integer
Dim lr As RectangleF

e.HasMorePages = False

currentY = 75 'Vertical de cima

currentY += maxY + gap
g.DrawLine(Pens.SlateGray, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap

For Each c In ListView1.Columns

maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)

g.DrawString(c.Text, f, b, lr)
l += c.Width
idx += 1

Next

currentY += maxY + gap
g.DrawLine(Pens.SlateGray, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap

'Rows
iCount = ListView1.Items.Count - 1
For ii = CurrRow To iCount

If (currentY + maxY + maxY) > e.PageBounds.Height - 60 Then 'Salta para outra linha
CurrRow = ii - 1
e.HasMorePages = True

Exit For 'Próxima Página

End If

l = 0
maxY = 0 ' Distância de linhas
idx = 0

For Each lvsi In ListView1.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)

If Linha_Colorir Mod 2 = 0 Then g.FillRectangle(Brushes.LightGray, colLefts(idx), currentY, colWidths(idx), maxY)

g.DrawString(lvsi.Text, f, b, lr)

idx += 1

Next
Linha_Colorir = Linha_Colorir + 1
currentY += maxY + gap
Next

g.DrawLine(Pens.SlateGray, 0, 1090, e.PageBounds.Width, 1090)
g.DrawString("Data de Impressão: " & DateTime.Now, D_Font2, Brushes.Black, 20, 1100)
g.DrawString("Página " & Pagina_Atual & " de " & Pagina_Total, D_Font2, Brushes.Black, 650, 1100)

End Sub
#End Region

Ficaria muito grato se alguém me poder ajudar.

😞

Edited by sardin3s
Link to comment
Share on other sites

Consegui resolver esta situação como a implementação de um código manhoso.

O tópico fica resolvido, no entanto deixo aqui o código, não vá alguém precisar 😉

#Region "Print related declarations"
Protected WithEvents pd As Printing.PrintDocument
Protected Ratio As Single = 0, CurrRow As Integer = 0
#End Region

#Region "Simple Printing of ListView"
'''
'''
'''

Public Sub Print()

Listview_Width()
Calcula_Total()

pd = New Printing.PrintDocument
pd.DocumentName = "Encomenda " & Now.Year & Now.Month & Now.Day & Now.Hour & Now.Minute & Now.Second
Ratio = 1
CurrRow = 0

PrintDialog1.Document = pd

If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then

Else
pd.Print()
End If

End Sub

'''
'''
'''

Public Sub PrintPreview()

Listview_Width()
Calcula_Total()

pd = New Printing.PrintDocument
pd.DocumentName = "Impressão da encomenda: " & Numero_Pedido
Ratio = 1
CurrRow = 0
Dim ppv As New PrintPreviewDialog

ppv.Document = pd
ppv.MdiParent = Index
ppv.Height = 600
ppv.Width = 400

ppv.Show()

End Sub

Private Sub Calcula_Total()

Dim total As Decimal = 0D

For Each item As ListViewItem In ListView1.Items
total += IIf(IsNumeric(item.SubItems(8).Text), item.SubItems(8).Text, 0)
Next

Total_Encomenda = total
End Sub

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

Dim Linha_Colorir As Integer = 0
Dim Pagina_Total As Integer = 1
Dim c As ColumnHeader
Dim g As Graphics = e.Graphics
Dim l As Integer = 9 'Alinhamento Vertical
Dim iCount As Integer
Dim f As New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
Dim D_Font As New Font("Verdana", 18, FontStyle.Bold, GraphicsUnit.Point)
Dim D_Font2 As New Font("Microsoft Sans Serif", 10, FontStyle.Regular)
Dim b As Brush = Brushes.Black
Dim currentY As Integer = 0, maxY As Integer = 0 'Altura do cabeçalho para os valores
Dim gap As Integer = 5 'Espaçamento horizontal das linhas dos dados
Dim lvsi As ListViewItem.ListViewSubItem
Dim colLefts(ListView1.Columns.Count) As Integer, colWidths(ListView1.Columns.Count) As Integer, idx As Integer = 0, ii As Integer
Dim lr As RectangleF

paginaAtual = 1

e.HasMorePages = False

currentY = 55 'Vertical de cima

currentY += maxY + gap
g.DrawLine(Pens.SlateGray, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap

'g.DrawImage(Image.FromFile(My.Application.Info.DirectoryPath & "\Images\logo.png"), 9, 10)

g.DrawString("Nome", D_Font, Brushes.Black, 9, 10)
'g.DrawString("Nome", D_Font, Brushes.Black, 30, 30)


For Each c In ListView1.Columns

maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)

g.DrawString(c.Text, f, b, lr)
l += c.Width
idx += 1

Next

currentY += maxY + gap
g.DrawLine(Pens.SlateGray, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap

'Rows
iCount = ListView1.Items.Count - 1
For ii = CurrRow To iCount

If (currentY + maxY + maxY) > e.PageBounds.Height - 100 Then 'Salta para outra linha
CurrRow = ii - 1

e.HasMorePages = True


Exit For 'Próxima Página

End If

l = 0
maxY = 0 ' Distância de linhas
idx = 0

For Each lvsi In ListView1.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)

If Linha_Colorir Mod 2 = 0 Then
g.FillRectangle(Brushes.LightGray, colLefts(idx), currentY, colWidths(idx), maxY)
g.FillRectangle(Brushes.LightGray, 9, currentY + 15, 155, maxY - 15)
End If


g.DrawString(lvsi.Text, f, b, lr)



idx += 1

Next
Linha_Colorir = Linha_Colorir + 1
currentY += maxY + gap

Next

If ListView1.Items.Count - 1 < 47 Then Pagina_Total = 1
If ListView1.Items.Count - 1 > 45 And ListView1.Items.Count - 1 < 91 Then Pagina_Total = 2
If ListView1.Items.Count - 1 > 90 Then Pagina_Total = ((ListView1.Items.Count - 1) / 46) + 1


If Pagina_Total = paginaAtual Then

g.FillRectangle(Brushes.SlateGray, 510, currentY, 287, 17)
g.DrawString("Total aproximado da encomenda: " & Total_Encomenda & " €", D_Font2, Brushes.White, 510, currentY)

End If

g.DrawLine(Pens.SlateGray, 0, 1090, e.PageBounds.Width, 1090)

g.DrawString("Data de Impressão: " & DateTime.Now, D_Font2, Brushes.Black, 20, 1100)

g.DrawString("Artigos Encomendados: " & ListView1.Items.Count, D_Font2, Brushes.Black, 330, 1100)

g.DrawString("Página " & paginaAtual & " de " & Pagina_Total, D_Font2, Brushes.Black, 650, 1100)

paginaAtual += 1

End Sub
#End Region
Edited by Caça
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.