3

I have barcode images in a folder and I want to print them like in the image below. But the problem is I can't make the rest of the image go to the next page.

In this situation, I have 22 images, but the paper fits only 20 to 21 (the last image is cut by the margin). My question is how do I make the rest 20s and 20s if I got many images to the next pages?

barcode images

Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim extensions As New List(Of String)
    extensions.Add("*.jpg")
    ' And so on, until all are in...

    Dim fileCount As Integer
    For i As Integer = 0 To extensions.Count - 1
        fileCount += Directory.GetFiles(Application.StartupPath & "\temp\", extensions(i), SearchOption.AllDirectories).Length
    Next

    Dim imgPictures(fileCount) As Image

    For i As Integer = 1 To fileCount
        imgPictures(i) = Bitmap.FromFile(Application.StartupPath & "\temp\" & i & ".jpg")
        e.Graphics.DrawImage(imgPictures(i), 50, 50 * i)
        If i = 20 Then
            e.HasMorePages = True
        ElseIf i = fileCount Then
            e.HasMorePages = False
        End If
    Next
End Sub
Jimi
  • 29,621
  • 8
  • 43
  • 61
bryanjez
  • 439
  • 4
  • 8
  • 2
    The principle is very simple. Declare a variable as a record counter and initialise it to zero when you start printing. In the event handler use a loop that starts at the counter and ends when there are no more records or you reach the end of the page. Set HasMorePages based on whether there are more records to print. Now the first event will print one page worth of records and the second tool print the rest. – jmcilhinney Jul 04 '21 at 14:57
  • 2
    Thank you @jmcilhinney I will try to understand and apply this. – bryanjez Jul 04 '21 at 15:11
  • 2
    @jmcilhinney I solved it, thank you very much. – bryanjez Jul 04 '21 at 16:36

1 Answers1

3

After hours of trying, finally it worked, shout out to jmcilhinney, big help

Dim recordCount As Integer

Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim lineCount As Integer
    Dim extensions As New List(Of String)
    extensions.Add("*.jpg")
    ' And so on, until all are in...

    Dim fileCount As Integer
    For i As Integer = 0 To extensions.Count - 1
        fileCount += Directory.GetFiles(Application.StartupPath & "\temp\", extensions(i), SearchOption.AllDirectories).Length
    Next

    Dim imgPictures(fileCount) As Image
    For i As Integer = 1 To fileCount
        recordCount += 1
        lineCount += 1
        imgPictures(recordCount) = Bitmap.FromFile(Application.StartupPath & "\temp\" & recordCount & ".jpg")
        e.Graphics.DrawImage(imgPictures(recordCount), 50, 50 * i)
        If lineCount >= 20 Then
            Exit For
        ElseIf recordCount = fileCount Then
            Exit For
        End If
    Next
    If recordCount = fileCount Then
        e.HasMorePages = False
    ElseIf lineCount = 20 Then
        e.HasMorePages = True
    End If
End Sub

Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    recordCount = 0
End Sub
Jimi
  • 29,621
  • 8
  • 43
  • 61
bryanjez
  • 439
  • 4
  • 8