split PrintPage for the number of parts

AMER SAID 331 Reputation points
2021-09-27T16:24:09.12+00:00

HI

I have a database with the name of the employee, his data, and a picture of the employee.
I want to split the print page into 3 parts in length and 3 parts in width.
In each part, a picture of the employee and the name of the employee is placed in the case. If the number of employees in the database is greater than 9, it will move to the next page where each page takes 9 employees.
Note: The database contains an unlimited number of employees

A picture of what I want to appear on the print page

135626-untitled-2.png

I used two DataTable, one for calculating the employees for whom I want to print the ID.
and other spreadsheets to fetch data such as the name and image from the table.
I used the following code, but only one employee is printed on each page and goes to the next page.

Private printLine As Integer = 0  
    Private Sub PRINT_DOC_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PRINT_DOC.PrintPage  
  
        Dim recHeight As Integer = e.PageSettings.PrintableArea.Height / 3  
        Dim recWidth As Integer = e.PageSettings.PrintableArea.Width / 3  
  
        For B As Integer = 0 To DataTableA.Rows.Count - 1  

'// Function to populate the database
FillDATTABEL(DataTableB, "SELECT * FROM EMPLOYEE_TB WHERE EMPLOYEE_CODE='" & dta.Rows(printLine).Item("EMPLOYEE_CODE").ToString.Trim & "'")
Dim format As StringFormat = New StringFormat(StringFormatFlags.DirectionRightToLeft)

            e.Graphics.DrawString(DTLOGDETAILS.Rows(0).Item("EMPLOYEE_NAME").ToString, New Font("Arial", 12, FontStyle.Regular), Brushes.Black, New Point(recWidth, recHeight), format)  
  
            Try  
                Dim mybyte As Byte() = New Byte(-1) {}  
                mybyte = CType((DTLOGDETAILS.Rows(0).Item("EMPLOYEE_IMG")), Byte())  
                Dim ms As MemoryStream = New MemoryStream(mybyte)  
                e.Graphics.DrawImage(Image.FromStream(ms), recWidth, recHeight, 200, 300)  
            Catch ex As Exception  
                Return  
            End Try  
  
        Next  
  
        printLine += 1  
        e.HasMorePages = (printLine <= dta.Rows.Count - 1)  
  
  
    End Sub  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,843 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,341 Reputation points
    2021-09-28T06:44:55.84+00:00

    Hi @AMER SAID ,
    I store some pictures in a folder and make a test on my side.
    Here's the code you can refer to.

        Private picCount As Integer = ...' count of pcitures  
        Private picIndex As Integer = 1  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
      
            Dim printDialog1 As PrintDialog = New PrintDialog()  
            printDialog1.Document = PrintDocument1  
            Dim result As DialogResult = printDialog1.ShowDialog()  
      
            If result = DialogResult.OK Then  
                PrintDocument1.Print()  
            End If  
        End Sub  
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage  
            Dim nameFont As Font = New Font("Arial", 20)  
            Dim recWidth As Integer = e.PageSettings.PrintableArea.Width / 3  
            Dim recHeight As Integer = e.PageSettings.PrintableArea.Height / 3  
            Dim x As Single = e.PageSettings.PrintableArea.Left  
            Dim y As Single = e.PageSettings.PrintableArea.Top  
      
            Dim stringFormat As New StringFormat()  
            stringFormat.Alignment = StringAlignment.Center  
            stringFormat.LineAlignment = StringAlignment.Center  
      
            While picIndex <= picCount  
                Dim rectText As New Rectangle(x, y, recWidth - 50, 30)  
                Dim image As Image = image.FromFile("my picture path\image" & picIndex & ".jpg")  
                If x < e.PageSettings.PrintableArea.Width And y < e.PageSettings.PrintableArea.Height Then  
                    e.Graphics.DrawString("name" & picIndex, nameFont, New SolidBrush(Color.Black), rectText, stringFormat)  
                    e.Graphics.DrawImage(image, New Rectangle(x, y + 40, recWidth - 50, recHeight - 150))  
                    x += recWidth  
                ElseIf x > e.PageSettings.PrintableArea.Width Then  
                    x = e.PageSettings.PrintableArea.Left  
                    y += recHeight  
                    If y < e.PageSettings.PrintableArea.Height Then  
                        rectText = New Rectangle(x, y, recWidth - 50, 30)  
                        e.Graphics.DrawString("name" & picIndex, nameFont, New SolidBrush(Color.Black), rectText, stringFormat)  
                        e.Graphics.DrawImage(image, New Rectangle(x, y + 40, recWidth - 50, recHeight - 150))  
                        x += recWidth  
                    Else  
                        e.HasMorePages = True  
                        Return  
                    End If  
                End If  
                picIndex += 1  
            End While  
        End Sub  
    

    Result of my test.
    135796-1.png

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. AMER SAID 331 Reputation points
    2021-10-14T15:11:23.69+00:00

    How much time did you take to show print pictures?
    Did you take a lot of time to fetch data from the database? Yes, very much, because two pages appear. I just stopped the images part. I said maybe this is the reason, but the code stops the whole program until execution and takes an exaggerated amount of time.
    ·