Question PrintDocument class multiple pages

shaju.a.antony

New member
Joined
Sep 26, 2013
Messages
2
Programming Experience
1-3
Hi,

I have a image array, it consists 100 images (50 x 50 image size)

i need to print 10 images in 1st page and another 10 images in 2nd page like it going on print up to the ubound(array)

i checked like the following


For n As Integer = initcount To img.Count - 1
ev.Graphics.DrawImage(img(n), xcol, topMargin, 150, 100)
If cnt > 10 then
cnt=1
ev.HasMorePages = True
Endif
Next

it is not working...

please help me to do

Thanks
Shaju
 
Hi and welcome to the Forum,

Just for future reference, the statement "it is not working....", does not help us to help you at all since only you know why something is not working because only you can see what is going on with your code. So, you need to explain what is not working and not leave it to use to try and figure it out for ourselves.

That said, the way to use the PrintDocument object is to work out how many lines or objects that you can fit on a page and then print that information. Once printed, the last thing to check is whether their are more lines or objects that need to be printed and only then do you set HasMorePages = True just before the current routine ends.

Here is an example demonstrating this knowing that we only want 10 pictures on each page:-

Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  Const totalPicturesPerPage As Integer = 10
  Static totaPicturesPrinted As Integer
 
  Dim currentPictureOnThePage As Integer = 1
  Dim currentTopMargin As Integer = 60
 
  For n As Integer = totaPicturesPrinted To totaPicturesPrinted + totalPicturesPerPage - 1
    If totaPicturesPrinted > ImageList1.Images.Count - 1 Then
      Exit For
    Else
      e.Graphics.DrawImage(ImageList1.Images(n), 100, currentTopMargin, 90, 90)
      totaPicturesPrinted += 1
      currentTopMargin = (currentPictureOnThePage * 100) + 60
      currentPictureOnThePage += 1
    End If
  Next
  If totaPicturesPrinted < ImageList1.Images.Count - 1 Then
    e.HasMorePages = True
  End If
End Sub


Hope that helps.

Cheers,

Ian
 
You just need one variable to keep track of how many images has already been processed. For example:
Private processed As Integer

then in PrintPage handler
        Dim selection = img.Skip(processed).Take(10)
        For Each picture In selection
            'draw it
        Next
        processed += selection.Count
        e.HasMorePages = (img.Count - processed) > 0

Remember to set the 'processed' counter to 0 before each print job.
 
Back
Top