VB.NET 打印边距

Margin in VB.NET printing

我正在尝试使用 vb.net 和 vb.net 的 Graphics.DrawString 在 58mm 热敏打印机上打印收据,但它在开头留下了一些 space . 我也尝试过将 x 坐标值设置为 0 和负数,在这种情况下,第一种情况有时会打印一半或被丢弃。

代码如下

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

    PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.

    If PrintDialog1.ShowDialog() = DialogResult.OK Then
        PrintDocument1.Print()
    End If
End Sub



    Private Sub PrintDocument1_PrintPage(sender As Object, e As                         Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim printFont = New Font("Calligraphr", 24.8, FontStyle.Regular)

    Dim printBold = New Font("Calligraphr", 26, FontStyle.Bold)
    e.Graphics.DrawString("C", printFont, Brushes.Black, New Point(0, 50))
    e.Graphics.DrawString("I", printFont, Brushes.Black, New Point(-6, 60))
    e.Graphics.DrawString("T", printFont, Brushes.Black, New Point(6, 60))
    e.Graphics.DrawString("Y", printFont, Brushes.Black, New Point(12, 60))

   End Sub

尝试在打印前输入您要处理的纸张尺寸,例如

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

    PrintDoc.PrinterSettings = PrintDialog.PrinterSettings

    Dim psz As New Printing.PaperSize

    PrintDoc.PrinterSettings.PrinterName = printerName

    psz.RawKind = Printing.PaperKind.Custom

    Dim printSize as Size = New Size(MMInPixel(58), MMInPixel(58))
    psz.Width = printSize.Width
    psz.Height = printSize.Height

    '---- Set the paper size
    PrintDoc.DefaultPageSettings.PaperSize = psz

    PrintDoc.Print()

End Sub

Private Sub PrintDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage

    Dim f As New Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point)

    With e.Graphics

        .SmoothingMode = SmoothingMode.HighQuality
        .TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

        .DrawString("C", printFont, Brushes.Black, MMInPixel(0), MMInPixel(50))

    End With

End Sub

Private inchInMM As Double = 25.4          'mm = 1 inch
Private dpi As Integer = 96

Private Function MMInPixel(value As Integer) As Integer

    Return Math.Round((value / inchInMM) * dpi, MidpointRounding.AwayFromZero)

End Function