如何在固定宽度和动态高度的纸张上使用vb.net进行打印

时间:2011-11-10 19:00:36

标签: .net vb.net printing

我正在VB.NET和.NET Framework 3.5中开发一个销售点(POS)应用程序,其中一次可以购买多个项目。我需要以行列方式打印所有项目:代码,名称数量,价格。

SHOP NAME            date
==========           =====
SL   CODE      NAME     QTY      PRICE
==   =====     =====    ===      =====
1    ANC-059   Pencil   1        $2.00
2    ASNC-009  Pencil   1        $2.00
3    ASNC-09   Pencil   1        $2.00
4    ASNC-009  Pencil   1        $2.00

页面宽度是固定的,但高度是动态的。

打印输出将打印在POS系统中通常使用的卷纸上。

如何做到这一点?

1 个答案:

答案 0 :(得分:4)

标准winforms印刷:

Try
    'Set up the document for printing and create a new PrintDocument object.
    Dim pd As New Printing.PrintDocument
    'Set the event handler for the printpage event of the PrintDocument.
    AddHandler pd.PrintPage, AddressOf pd_PrintPage
    'Set the printer name.
    pd.PrinterSettings.PrinterName = PrintDialog1.PrinterSettings.PrinterName
    'Print the document by using the print method for the PrintDocument, which triggers the PrintPage event
    pd.Print()  
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try


'The PrintPage event is raised for each page to be printed.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
    'Set up the default graphics object that will be used to do the actual printing.
    Dim g As Graphics
    g = ev.Graphics

    Dim tHeight as Double
    Dim txt as String = "My text goes here"
    g.DrawString(txt, myFont, myBrush, xPosition, yPosition, StringAlignment.Near)
    'Measure the height (on the page) of the item that you have just drawn, so that
    'you can place the next item below it.
    tHeight = g.MeasureString("Customer", fntBlue).Height()

    txt = "My new line of text"
    g.DrawString(txt, myFont, myBrush, xPosition, yPosition + tHeight, StringAlignment.Near)

    '.....continue printing other items
End Sub