تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.NET] احتاج مساعدة في طابعة فواتير حرارية
#3
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




  1. في بداية الفاتورة، يتم طباعة رقم الهاتف في وسط الصفحة.
  2. بعد ذلك، يتم رسم الشعار (اللوغو) واسم المحل بجانبه.
  3. ثم يتم إنشاء جدول يحتوي على بيانات الـ DataGridView.
لاستخدام هذا الكود:
  1. قم بإنشاء زر في نموذجك وسمِّه
    ButtonPrint
  2. .
  3. تأكد من أن لديك DataGridView في النموذج وأنه يحتوي على البيانات التي تريد طباعتها.
  4. استبدل
    "path_to_your_logo.png"
  5. بالمسار الفعلي لملف الشعار الخاص بك.
هذا الكود مرن ويمكن أن يعمل مع معظم أنواع الطابعات، بما في ذلك الطابعات الحرارية. ومع ذلك، قد تحتاج إلى 
ضبط بعض الإعدادات (مثل حجم الخط أو أبعاد الصورة) اعتمادًا على الطابعة المحددة التي تستخدمها.
الرد }}}
تم الشكر بواسطة: PABLO , علاء الكبابي


الردود في هذا الموضوع
RE: احتاج مساعدة في طابعة فواتير حرارية - بواسطة الـ ـجارح - 15-08-24, 11:08 AM


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم