Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Printing by VB.NET

Printing by VB.NET

Scheduled Pinned Locked Moved Visual Basic
csharpgraphicsquestion
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rain1178
    wrote on last edited by
    #1

    Hi, I'm want to print the text file from my application. I use the code below and i use dot-matrix printer to print. However, i find that the printing process is very slow because the printer will print twice per line. What other method that can be used other than the method below? What other object can be used to print instead of ev.Graphics.DrawString? Or what other setting can used to make the printing faster/print once per row. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim prtdoc As PrintDocument = New PrintDocument() Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName Dim strPrinter As String For Each strPrinter In PrinterSettings.InstalledPrinters ComboBox1.Items.Add(strPrinter) If (strPrinter = strDefaultPrinter) Then ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf(strPrinter) End If Next End Sub Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) Dim linesPerPage As Single = 0 Dim yPos As Single = 0 Dim count As Integer = 0 Dim leftMargin As Single = 0 Dim topMargin As Single = 0 Dim line As String = Nothing ' Calculate the number of lines per page. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ' Iterate over the file, printing each line. While count < linesPerPage line = streamToPrint.ReadLine() If line Is Nothing Then Exit While End If yPos = topMargin + count * printFont.GetHeight(ev.Graphics) ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _ yPos, New StringFormat()) count += 1 End While 'If more lines exist, print another page. If Not (line Is Nothing) Then ev.HasMorePages = True Else ev.HasMorePages = False End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try streamToPrint = New StreamReader("D:\abc.txt") Try printFont = New Font("Verdana", 10) Dim pd As New PrintDocument() pd.PrinterSettings.PrinterName = ComboBox1.SelectedItem If (pd.PrinterSettings.IsValid) Then AddHandler pd.PrintPage, AddressOf pd_PrintPage ' Print the document. pd.Print() Else MessageBox.Show("Invalid Printer") End If Finally streamToPrint.Close() End Try Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups