简体   繁体   中英

VB.net how to print text directly client side

I am trying to print a variable (some text) to a printer (as a generic text for creating a barcode label Zebra printer 3488) , it works fine when using Printing.PrintDocument on server side , all what I need to print the variable "TextToBePrinted" or text box directly to printer without popup , I know that I have to use JavaScript ,Activex or vbscript so if anyone can help I appreciate that

thank you

Hamada

------ the following code works fine on server side -----

Imports System.Drawing.Printing
Imports System.Drawing

Dim printernameD As String
TextToBePrinted = TextBox3.Text

Dim prn As New Printing.PrintDocument
Using (prn)

    TextToBePrinted = "Line 1111111111111" & Environment.NewLine
    TextToBePrinted = TextToBePrinted & "Line 2222222222222222" & Environment.NewLine
    TextToBePrinted = TextToBePrinted & "Line 3333333333333333" & Environment.NewLine
    TextToBePrinted = TextToBePrinted & "Line 4444444444444444" & Environment.NewLine

    AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
    prn.Print()
    RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
End Using

Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)

    Dim myFont As New Font("Times New Roman", 12) ' font

    Dim drawFont As New Font("EAN 13", 12)
    Dim drawBrush As New SolidBrush(Color.Black)
    ' Create rectangle for drawing. 
    Dim x As Single = 50.0F
    Dim y As Single = 50.0F
    Dim width As Single = 200.0F
    Dim height As Single = 50.0F
    Dim drawRect As New RectangleF(x, y, width, height)
    ' Set format of string. 
    Dim drawFormat As New StringFormat
    drawFormat.Alignment = StringAlignment.Center

    args.Graphics.DrawString(TextToBePrinted, New Font(myFont, FontStyle.Regular), Brushes.Black, 5, 5)

End Sub

Printers cannot be commanded by a webserver. The closest thing is for the server to generate a PostScript (in reality, PDF) document which the user then downloads and prints.

Fortunately you can re-use your existing code for that: just install Adobe Acrobat Pro (or some other PDF printer driver) and change your code to print to that virtual printer, identify where the PDF was saved, then return it to the client.

POS: get a website to print directly to a defined local printer/s

The first answer on this page explains how to print with Chrome Kiosk mode which I never heard of before reading that post but sounds pretty promising. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM