Print width on thermal printer, WPF and C#

Asked

Viewed 1,288 times

1

Hello, I am making a program to print list of activities in thermal printers, in it I put this information within a table (created in print-only code, full width around 250, and font size like 12)and the table inside a Flowdocument and then print with the method PrintDocument of a PrintDialog, the problem is that depending on the printer the content is being printed very large, and I would like it to be the same size in any thermal printer, whose sheet width follows the standard of 80mm. I have already tried to turn the table into an image and try to catch the indicated width in PrintDialog.PrintableAreaWidth and print as an image with this width, but I was not successful...

In short: I need to get a way that the font size and print width are equal regardless of the printer thinking of 80mm papers.

  • Hello Rune. Your question is good - I leave here only the hint that if you edit it to include the code you have already tried to use as an example, you will have more chances of getting a good answer in addition to getting a faster answer.

1 answer

1

The likely solution will be to adopt the standard ESC/POS we believe that all thermal printers adopt this standard.

By experience, EPSON, ELGIN, BEMATECH, I have already tested, obviously some commands of this standard can apply to one model and another not, for example, cutting paper only has effect on printer with guillotine.

Here’s a Example of use

Documentation: Here

Code snippet (Full example on link above):

Public Class Form1
    Public Const eClear As String = Chr(27) + "@"
    Public Const eCentre As String = Chr(27) + Chr(97) + "1"
    Public Const eLeft As String = Chr(27) + Chr(97) + "0"
    Public Const eRight As String = Chr(27) + Chr(97) + "2"
    Public Const eDrawer As String = eClear + Chr(27) + "p" + Chr(0) + ".}"
    Public Const eCut As String = Chr(27) + "i" + vbCrLf
    Public Const eSmlText As String = Chr(27) + "!" + Chr(1)
    Public Const eNmlText As String = Chr(27) + "!" + Chr(0)
    Public Const eInit As String = eNmlText + Chr(13) + Chr(27) + _
    "c6" + Chr(1) + Chr(27) + "R3" + vbCrLf
    Public Const eBigCharOn As String = Chr(27) + "!" + Chr(56)
    Public Const eBigCharOff As String = Chr(27) + "!" + Chr(0)

    Private prn As New RawPrinterHelper

    Private PrinterName As String = "EPSON TM-T20 Receipt"

    Public Sub StartPrint()
        prn.OpenPrint(PrinterName)
    End Sub

    Public Sub PrintHeader()
        Print(eInit + eCentre + "My Shop")
        Print("Tel:0123 456 7890")
        Print("Web: www.????.com")
        Print("sales@????.com")
        Print("VAT Reg No:123 4567 89" + eLeft)
        PrintDashes()
    End Sub

    Public Sub PrintBody()
        Print(eSmlText + "Tea                                          T1   1.30")

        PrintDashes()

        Print(eSmlText + "                                         Total:   1.30")

        Print("                                     Paid Card:   1.30")
    End Sub

    Public Sub PrintFooter()
        Print(eCentre + "Thank You For Your Support!" + eLeft)
        Print(vbLf + vbLf + vbLf + vbLf + vbLf + eCut + eDrawer)
    End Sub

    Public Sub Print(Line As String)
        prn.SendStringToPrinter(PrinterName, Line + vbLf)
    End Sub

    Public Sub PrintDashes()
        Print(eLeft + eNmlText + "-".PadRight(42, "-"))
    End Sub

    Public Sub EndPrint()
        prn.ClosePrint()
    End Sub

    Private Sub bnExit_Click(sender As System.Object, e As System.EventArgs) _
            Handles bnExit.Click
        prn.ClosePrint()

        Me.Close()
    End Sub

    Private Sub bnPrint_Click(sender As System.Object, e As System.EventArgs) _
            Handles bnPrint.Click
        StartPrint()

        If prn.PrinterIsOpen = True Then
            PrintHeader()

            PrintBody()

            PrintFooter()

            EndPrint()
        End If
    End Sub
End Class

Source: http://www.codeproject.com/Tips/704989/Print-Direct-To-Windows-Printer-EPOS-Receipt

  • Although in Vb, great example, but the problem is that in this system there are two modes, one only text and the other where I need to make use of some Graphic elements, so I use the printdocument function, which is where I’m having problems. Still, thank you.

  • It did not make this clear in the question, however, this answer method also works with graphical elements, images, bar code, etc. Post image and model code that is having problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.