Imports System.Drawing
Imports System.Drawing.Printing
Public Class FormInvoice
Private WithEvents PrintDocument1 As New PrintDocument
Private Invoice_Font As New Font("Arial", 10)
Private Bold_Font As New Font("Arial", 10, FontStyle.Bold)
Private Logo As Image = Image.FromFile("path_to_your_logo.png") ' تأكد من تغيير المسار إلى الشعار الخاص بك
Private Sub ButtonPrint_Click(sender As Object, e As EventArgs) Handles ButtonPrint.Click
Try
PrintDocument1.Print()
Catch ex As Exception
MessageBox.Show("خطأ في الطباعة: " & ex.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim startX As Integer = 10
Dim startY As Integer = 10
Dim Offset As Integer = 40
' طباعة رقم الهاتف
Dim phoneNumber As String = "0000 00 00 00"
Dim phoneSize As SizeF = e.Graphics.MeasureString(phoneNumber, Bold_Font)
e.Graphics.DrawString(phoneNumber, Bold_Font, Brushes.Black, (e.PageBounds.Width - phoneSize.Width) / 2, startY)
startY += Offset
' طباعة الشعار واسم المحل
e.Graphics.DrawImage(Logo, startX, startY, 50, 50)
e.Graphics.DrawString("سامي للحواسيب", Bold_Font, Brushes.Black, startX + 60, startY + 15)
startY += 60
' رسم جدول للبيانات
Dim cellHeight As Integer = 30
Dim tableWidth As Integer = e.PageBounds.Width - 2 * startX
' رسم رأس الجدول
For i As Integer = 0 To DataGridView1.Columns.Count - 1
Dim cellWidth As Integer = tableWidth / DataGridView1.Columns.Count
e.Graphics.DrawRectangle(Pens.Black, startX + i * cellWidth, startY, cellWidth, cellHeight)
e.Graphics.DrawString(DataGridView1.Columns(i).HeaderText, Bold_Font, Brushes.Black, startX + i * cellWidth + 5, startY + 5)
Next
startY += cellHeight
' رسم صفوف البيانات
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
For i As Integer = 0 To DataGridView1.Columns.Count - 1
Dim cellWidth As Integer = tableWidth / DataGridView1.Columns.Count
e.Graphics.DrawRectangle(Pens.Black, startX + i * cellWidth, startY, cellWidth, cellHeight)
e.Graphics.DrawString(row.Cells(i).Value.ToString(), Invoice_Font, Brushes.Black, startX + i * cellWidth + 5, startY + 5)
Next
startY += cellHeight
' التحقق مما إذا كانت هناك حاجة لصفحة جديدة
If startY + cellHeight > e.PageBounds.Height - 50 Then
e.HasMorePages = True
Exit Sub
End If
End If
Next
e.HasMorePages = False
End Sub
End Class
- في بداية الفاتورة، يتم طباعة رقم الهاتف في وسط الصفحة.
- بعد ذلك، يتم رسم الشعار (اللوغو) واسم المحل بجانبه.
- ثم يتم إنشاء جدول يحتوي على بيانات الـ DataGridView.
لاستخدام هذا الكود:
- قم بإنشاء زر في نموذجك وسمِّه
ButtonPrint
- .
- تأكد من أن لديك DataGridView في النموذج وأنه يحتوي على البيانات التي تريد طباعتها.
- استبدل
"path_to_your_logo.png"
- بالمسار الفعلي لملف الشعار الخاص بك.
هذا الكود مرن ويمكن أن يعمل مع معظم أنواع الطابعات، بما في ذلك الطابعات الحرارية. ومع ذلك، قد تحتاج إلى
ضبط بعض الإعدادات (مثل حجم الخط أو أبعاد الصورة) اعتمادًا على الطابعة المحددة التي تستخدمها.