2014年4月25日 星期五

vbnet 直接列印內容 (運用在簡易列印,如出單機)

MSDN:PrintDialog 類別
MSDN:PrintDocument 類別
Basic Text and Image Printing
--
範例 : MSDN:VB.Net Printing Example


底下為:印圖+文字範例,參考引用來源
--

Please check this code sample.

Clicking the Button1 will print a .bmp file and a .txt file on the same page.

Code Block
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog
        Dim PrintDocument1 As Printing.PrintDocument = New Printing.PrintDocument
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()
        AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage
        PrintDocument1.Print()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'RichTextBox1.LoadFile("D:\Text.txt", RichTextBoxStreamType.PlainText)
        'PictureBox1.Image = Image.FromFile("E:\VBproject\Gap.bmp")
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
     
      Dim newImage As Image = Image.FromFile("E:\VBproject\Gap.bmp")
      e.Graphics.DrawImage(newImage, 100, 100)
      ' You also can reference an image to PictureBox1.Image.

        Dim txtReader As System.IO.StreamReader = New System.IO.StreamReader("D:\Text.txt")
        Dim textOfFile As String = txtReader.ReadToEnd
        txtReader.Close()
        e.Graphics.DrawString(textOfFile, New Font("Arial", 16), Brushes.Black, 100, 500)
        ' You also can reference some text to RichTextBox1.Text, etc.

    End Sub
End Class

---
運用列印條碼字型:參考引用來源

'printing barcode coupon...

'1. Print the company logo and % for coupon...
Dim logo As Image = Image.FromFile(@"C:\temp\AWCoupon.jpg")
e.Graphics.DrawImage(logo, New Point(0, 0))
logo.Dispose()

'2. Print barcode...
Dim barcode As New Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional()

'set barcode symbology... eg Code 128
barcode.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128
'set barcode bars dimensions...
barcode.BarcodeUnit = Neodynamic.WinControls.BarcodeProfessional.BarcodeUnit.Inch
barcode.BarWidth = 0.015
barcode.BarHeight = 0.75
'set value to encode... this time a random value...
barcode.Code = Guid.NewGuid().ToString().ToUpper().Substring(0, 16)
'justify text...
barcode.CodeAlignment = Neodynamic.WinControls.BarcodeProfessional.Alignment.BelowJustify
'set font for barcode human readable text...
Dim fnt As New Font("Courier New", 10F)
barcode.Font = fnt
fnt.Dispose()
'print barcode below logo...
'IMPORTANT: barcode location unit depends on BarcodeUnit setting which in this case is INCH
barcode.DrawOnCanvas(e.Graphics, New PointF(0.1F, 1.5F))

'3. Print some texts...
Dim fnt1 As New Font("Arial", 12F, FontStyle.Bold)
Dim fnt2 As New Font("Arial", 8F, FontStyle.Regular)
 
e.Graphics.DrawString("RESELLER INTRUCTIONS", fnt1, Brushes.Black, New PointF(10F, 270F))
e.Graphics.DrawString("Use Item -% Off for the 15%. Scan coupon barcode or enter coupon code. Collect coupon with purchase as coupon may only be redeemed once per customer.", fnt2, Brushes.Black, New RectangleF(New PointF(10F, 290F), New SizeF(570F, 50F)))

e.Graphics.DrawString("COUPON OFFER DETAILS", fnt1, Brushes.Black, New PointF(10F, 350F))
e.Graphics.DrawString("This coupon can be redeemed once at any Adventure Works Cycles retail store." + Environment.NewLine + "This coupon is valid from May 21, 2009 to Jun 21, 2009.", fnt2, Brushes.Black, New RectangleF(New PointF(10F, 370F), New SizeF(570F, 50F)))

fnt1.Dispose()
fnt2.Dispose()

沒有留言:

張貼留言