VB.NET打印中的页边距

时间:2018-09-20 07:12:26

标签: vb.net printing drawstring printdocument

我正在尝试使用vb.net和vb.net的Graphics.DrawString在58毫米热敏打印上打印收据,但开始时会留一些空间。 我还尝试将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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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