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

时间:2013-07-21 06:38:09

标签: vb.net print-preview

我试图让一切都对齐但我不完全确定如何... 到目前为止,这是我所做的,但项目名称,价格和其他不是对齐形式...... 有没有办法让它们对齐? 继承我的代码......

  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

2 个答案:

答案 0 :(得分:2)

它可能会在您的文本编辑器中正确对齐,因为它使用Consolas或New Century等字体。每个字母宽度相同的字体。并故意选择让代码中的语句排成一行。

但是这不适用于打印机,您将使用可变的倾斜字体进行打印。就像这个答案中的文字一样,字母i占用的空间比字母W:iiiiii vs WWWWWW少得多。

所以不要在DrawString()文本中放置空格,这不会对齐。而是使用DrawString的重载,它接受RectangleF和StringFormat。所以你使用StringAlignment.Center并将字符串完美地置于矩形中心。在下一行中使用相同的矩形宽度,文本将与上一行完美居中。使用固定间距字体的打印机也可以解决您的问题,但没有人喜欢看起来的样式。

答案 1 :(得分:0)

基本上就是这样..

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)