How to print all pages of the PrintDocument?

  • Thread starter Marcos Beccar Varela
  • Start date
M

Marcos Beccar Varela

Hello to all, I have this form with a PrintDocument named prt_doc,
theproblem is that when I invoke the prt_doc.print() it only prints the las
page, and not multiple pages.
I Also Have a Preview with a PrintPreviewControl and it shows me all the
pages, but I still don´t know how to make them print.
Thank you all
Marcos





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
prt_doc.PrinterSettings.PrintRange = PrintRange.AllPages
prt_doc.Print()
End Sub



Private Sub prt_doc_PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles prt_doc.PrintPage
Dim TipoTexto As Font = New Font("Verdana", 8, FontStyle.Regular)
Dim TipoTexto2 As Font = New Font("Verdana", 8, FontStyle.Bold)
Dim TipoTitulo As Font = New Font("Verdana", 18, FontStyle.Bold)
Dim TipoTitulo2 As Font = New Font("Verdana", 14, FontStyle.Bold)
Dim TipoTitulosDatos As Font = New Font("Verdana", 7,
FontStyle.Underline Or FontStyle.Bold)
Dim TipoDatos As Font = New Font("Verdana", 10, FontStyle.Italic)
Dim Brocha As Brush
Dim Lapiz As Pen = New Pen(Color.Black, 1)
Dim PosY As Integer = e.MarginBounds.Top
If LineaTexto = 0 Then
PosY = e.MarginBounds.Y
Dim Tamano As SizeF = e.Graphics.MeasureString(Titulo, TipoTitulo)
e.Graphics.MeasureString(Titulo, TipoTitulo)
e.Graphics.DrawString(Titulo, TipoTitulo, Brushes.Gray, 0, PosY)
PosY += Tamano.Height
e.Graphics.DrawString(Titulo2, TipoTitulo2, Brushes.Black, 0, PosY)
PosY += Tamano.Height + 10
End If
Dim Indice As Integer
Dim AltoLinea As Integer = TipoTexto.GetHeight(e.Graphics) + 2
For Indice = LineaTexto To st_camp0.GetUpperBound(0)
Dim ta_camp1 As SizeF = e.Graphics.MeasureString(st_camp1(Indice),
TipoTexto)
Dim ta_camp4 As SizeF = e.Graphics.MeasureString(st_camp4(Indice),
TipoTexto)
e.Graphics.DrawString(st_camp1(Indice), TipoTexto, Brushes.Black, 2,
PosY + 1)
e.Graphics.DrawLine(Lapiz, 0, PosY, 0, PosY + AltoLinea)
e.Graphics.DrawLine(Lapiz, 0, PosY, 750, PosY)
PosY += AltoLinea
If PosY + AltoLinea >= e.MarginBounds.Bottom Then
LineaTexto = Indice
PosY = e.MarginBounds.Top
e.HasMorePages = True
Exit Sub
End If
If Len(st_camp0(Indice)) = 0 Then Exit For
Next
End Sub
 
K

Ken Tucker [MVP]

Hi,

Each time the printdocument_printpage event is fired it prints one
page. If you set e.hasmorepages to true it will recall the print document
print event to print a new page. Here is a simple example.

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal
e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim g As Graphics = e.Graphics
Static intItem As Integer = 0
Dim y As Integer = 0
Dim bDraw As Boolean = True

Do Until Not bDraw
y += 15
g.DrawString(intItem.ToString, Me.Font, Brushes.Black, 10, y)
intItem += 1
bDraw = (intItem Mod 3 <> 0)
Loop

If intItem = 18 Then
intItem = 0
Else
e.HasMorePages = True
End If
End Sub

Ken
-------------------

Hello to all, I have this form with a PrintDocument named prt_doc,
theproblem is that when I invoke the prt_doc.print() it only prints the las
page, and not multiple pages.
I Also Have a Preview with a PrintPreviewControl and it shows me all the
pages, but I still don´t know how to make them print.
Thank you all
Marcos





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
prt_doc.PrinterSettings.PrintRange = PrintRange.AllPages
prt_doc.Print()
End Sub



Private Sub prt_doc_PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles prt_doc.PrintPage
Dim TipoTexto As Font = New Font("Verdana", 8, FontStyle.Regular)
Dim TipoTexto2 As Font = New Font("Verdana", 8, FontStyle.Bold)
Dim TipoTitulo As Font = New Font("Verdana", 18, FontStyle.Bold)
Dim TipoTitulo2 As Font = New Font("Verdana", 14, FontStyle.Bold)
Dim TipoTitulosDatos As Font = New Font("Verdana", 7,
FontStyle.Underline Or FontStyle.Bold)
Dim TipoDatos As Font = New Font("Verdana", 10, FontStyle.Italic)
Dim Brocha As Brush
Dim Lapiz As Pen = New Pen(Color.Black, 1)
Dim PosY As Integer = e.MarginBounds.Top
If LineaTexto = 0 Then
PosY = e.MarginBounds.Y
Dim Tamano As SizeF = e.Graphics.MeasureString(Titulo, TipoTitulo)
e.Graphics.MeasureString(Titulo, TipoTitulo)
e.Graphics.DrawString(Titulo, TipoTitulo, Brushes.Gray, 0, PosY)
PosY += Tamano.Height
e.Graphics.DrawString(Titulo2, TipoTitulo2, Brushes.Black, 0, PosY)
PosY += Tamano.Height + 10
End If
Dim Indice As Integer
Dim AltoLinea As Integer = TipoTexto.GetHeight(e.Graphics) + 2
For Indice = LineaTexto To st_camp0.GetUpperBound(0)
Dim ta_camp1 As SizeF = e.Graphics.MeasureString(st_camp1(Indice),
TipoTexto)
Dim ta_camp4 As SizeF = e.Graphics.MeasureString(st_camp4(Indice),
TipoTexto)
e.Graphics.DrawString(st_camp1(Indice), TipoTexto, Brushes.Black, 2,
PosY + 1)
e.Graphics.DrawLine(Lapiz, 0, PosY, 0, PosY + AltoLinea)
e.Graphics.DrawLine(Lapiz, 0, PosY, 750, PosY)
PosY += AltoLinea
If PosY + AltoLinea >= e.MarginBounds.Bottom Then
LineaTexto = Indice
PosY = e.MarginBounds.Top
e.HasMorePages = True
Exit Sub
End If
If Len(st_camp0(Indice)) = 0 Then Exit For
Next
End Sub
 
M

Marcos Beccar Varela

Thank You Ken, in the code, in almost the bottom there is these lines.
If PosY + AltoLinea >= e.MarginBounds.Bottom Then
LineaTexto = Indice
PosY = e.MarginBounds.Top
e.HasMorePages = True
Exit Sub
End If
But It isn´t working, it says that when the active line is superior to the
MarginBounds.Bottom, print a new page. So It is generated, As I see in the
previewer, but still not printed.
Thank you again
 
J

Jack Russell

Marcos said:
Thank You Ken, in the code, in almost the bottom there is these lines.


But It isn´t working, it says that when the active line is superior to the
MarginBounds.Bottom, print a new page. So It is generated, As I see in the
previewer, but still not printed.
Thank you again
Not sure if I can explain this but after the preview you have to print
it all again.
I think this printdoc stuff stinks!
 
M

Marcos Beccar Varela

Thank you Ken, I found this yesterday, I didn´t know that I had to make de
printpage again, but when i took off the previer it began to work. I´ll try
to leave it and invoke the printpage again, thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top