question

AMERSAID-7084 avatar image
0 Votes"
AMERSAID-7084 asked XingyuZhao-MSFT edited

split PrintPage for the number of parts

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


dotnet-visual-basic
untitled-2.png (646.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered XingyuZhao-MSFT commented

Hi @AMERSAID-7084 ,
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.



1.png (177.4 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi
Xingyu Zhao


Actually the code works fine, but the problem is that it takes a lot of time to show the print page, even though the data was only 9 and one page

0 Votes 0 ·

Hi @AMERSAID-7084 ,

it takes a lot of time to show the print page

How much time did you take to show print pictures?
Did you take a lot of time to fetch data from the database?

·

0 Votes 0 ·
AMERSAID-7084 avatar image
0 Votes"
AMERSAID-7084 answered XingyuZhao-MSFT edited

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.
·

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AMERSAID-7084 ,

the code stops the whole program until execution and takes an exaggerated amount of time.

You can consider using BackgroundWorker to execute an operation on a separate thread.


0 Votes 0 ·
AMERSAID-7084 avatar image AMERSAID-7084 XingyuZhao-MSFT ·

The problem is not that the program stops, the problem is that the exaggerated code time works.

0 Votes 0 ·
XingyuZhao-MSFT avatar image XingyuZhao-MSFT AMERSAID-7084 ·

Hi @AMERSAID-7084 ,
Thanks for your feedback.
I tested the code from my side, my delay is about 2 seconds, and the code will not stop, so could you provide the exact time of your delay? It will help us analyze if this is caused by database or code.

0 Votes 0 ·