在打印预览VB中使所有内容对齐



我试图让一切都对齐,但我不完全确定如何。。。到目前为止,这是我做的,但商品名称、价格和其他都不一致。。。有什么办法让它们对齐吗?这是我的密码。。。

  Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim printFont As New Font("Courier New", 10)
    'Dim headingFont As New Font("Courier New", 10)
    Dim sngLineHeight As Single = printFont.GetHeight + 2
    Dim sngXLocation As Single = e.MarginBounds.Left
    Dim sngYLocation As Single = e.MarginBounds.Top
    Dim intItemCount As Integer
    e.Graphics.DrawString("                   XXXX  Restaurant                   ", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("                       RECEIPT                         ", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("========================================================", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString(DateTime.Now.ToString("hh:mm dddd, dd MMMM yyyy"), printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    sngYLocation += sngLineHeight
    Dim ptOne As Point
    Dim ptTwo As Point
    ptOne.X = 10
    ptOne.Y = 10
    ptTwo.X = 10
    ptTwo.Y = 200
    e.Graphics.DrawLine(Pens.Black, ptOne, ptTwo)
    intItemCount = frmSalesManagement.dgvSaleManagement.Rows.Count
    Dim strItemDesc As String
    Dim strItemDesc2 As String
    Dim strItemDesc3 As String
    Dim strItemDesc4 As String
    Dim strTableNo As String
    Dim strNoOfPerson As String

    strTableNo = frmSalesManagement.txtTableNo.Text
    strNoOfPerson = frmSalesManagement.txtNoOfPerson.Text
    If frmSalesManagement.lblMemberName.Text <> "" Then
        e.Graphics.DrawString("Member's Name     : " & frmSalesManagement.lblMemberName.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Member's Discount : 10%", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
    End If
    e.Graphics.DrawString("Table No.: " & strTableNo, printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("No of Person : " & strNoOfPerson, printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("ItemName" & vbTab & "" & vbTab & "Price" & vbTab & "" & vbTab & "Quantity" & vbTab & "" & vbTab & "Subtotal", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    sngYLocation += sngLineHeight
    For i As Integer = 0 To intItemCount - 1

        strItemDesc = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(0).FormattedValue.ToString() + "" & vbTab & "" & vbTab
        strItemDesc2 = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(1).FormattedValue.ToString() + "" & vbTab & "" & vbTab
        strItemDesc3 = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(2).FormattedValue.ToString() + "" & vbTab & "" & vbTab
        strItemDesc4 = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(3).FormattedValue.ToString() + "" & vbTab & "" & vbTab

        e.Graphics.DrawString(strItemDesc & strItemDesc2 & strItemDesc3 & strItemDesc4, printFont, Brushes.Black, sngXLocation, sngYLocation)

        sngYLocation += sngLineHeight
    Next
    If lblMethodOfPayment.Text = "Cash" Then
        e.Graphics.DrawString("Payment Method : " & lblMethodOfPayment.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Discount    :  " & frmSalesManagement.txtDiscount.Text.ToString() & "%", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Due Amount  :  RM" & lblDueAmount.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Amount Paid :  RM" & txtAmountPaid.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Change      :  RM" & lblChange.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("======================= Thank You ======================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("========================== End =========================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
    ElseIf lblMethodOfPayment.Text = "Visa Card" Or lblMethodOfPayment.Text = "Master Card" Then
        e.Graphics.DrawString("Payment Method : " & lblMethodOfPayment.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Discount    :  " & frmSalesManagement.txtDiscount.Text.ToString() & "%", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Due Amount  :  RM" & lblDueAmount.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Amount Paid :  RM" & txtAmountPaid.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("======================= Thank You ======================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("========================== End =========================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
    End If
    sngYLocation += sngLineHeight

End Sub

它可能会在文本编辑器中正确对齐,因为它使用了类似Consolas或New Century的字体。每个字母都有相同宽度的字体。并有意选择使代码中的语句对齐。

但这在打印机上不起作用,你将使用可变间距的字体打印。就像这个答案中的文本一样,字母i比字母W:iiiiii vs WWWWWW占用的空间要小得多。

所以不要在DrawString()文本中放空格,这样不会对齐。相反,请使用DrawString的重载,该重载采用RectangleF和StringFormat。所以你使用StringAlignment.Center,让字符串在矩形中完全居中。在下一行使用相同的矩形宽度,文本将与上一行完全居中。在打印机上使用固定间距的字体也可以解决你的问题,但没有人喜欢它的外观。

基本上是这样的。。

Dim x as Single = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("XXXX  Restaurant", PrintFont).Width / 2)
e.Graphics.DrawString("XXXX  Restaurant",PrintFont, Brushes.Black, x, 100)

相关内容

  • 没有找到相关文章

最新更新