0

I am developing a small desktop application that should print tickets on thermal papers.

The problem is that I don't have a physical thermal printer device so I am trying to simulate the layout of the receipt on a PDF document. So, I written some code that creates a sample document and print it in pdf but I am unable to set the correct dimensions of the paper.

The test code for printing (Visual Basic)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim printDlg As New PrintDialog()

    prntDocTicket.DocumentName = "Test Document"

    printDlg.Document = prntDocTicket
    printDlg.Document.DefaultPageSettings.PaperSize = New PaperSize("Custom", 225, 225)

    prntDocTicket.DefaultPageSettings.PaperSize.Height = 225
    prntDocTicket.DefaultPageSettings.PaperSize.Width = 225

    prntDocTicket.PrinterSettings.PrintToFile = True
    prntDocTicket.PrinterSettings.PrintFileName = "Test.pdf"

    prntDocTicket.Print()

    printDlg = Nothing
    prntDocTicket = Nothing
End Sub

Private Sub prntDocTicket_PrintPage(sender As Object, e As PrintPageEventArgs) Handles prntDocTicket.PrintPage
    Dim textToPrint As String = "Test"
    Dim typeFont As Font = New Font("calibri", 10)
    Dim textArea As SizeF = e.Graphics.MeasureString(textToPrint, typeFont)
    Dim drawX As Integer = (e.PageBounds.Width - textArea.Width) / 2
    Dim drawY As Integer = (e.PageBounds.Height - textArea.Height) / 2

    With e.Graphics
        Call .DrawRectangle(New Pen(Color.Red), e.PageBounds)
        Call .DrawString(textToPrint, typeFont, New SolidBrush(Color.Black), drawX, drawY)
        Call .Dispose()
    End With

End Sub

And this is how the output PDF looks like, I can't control the size of the document and I don't have a thermal printer to test the output.

Print Output PDF

I am supposed to print using Star micronics TSP650 thermal printer.

Can someone help me please as this is my first time to print such documents ?

Note: Although the code is in VB but I don't mind codes in C#, actually I would prefer C#.

Taher
  • 565
  • 1
  • 14
  • 32

0 Answers0