Results 1 to 3 of 3

Thread: Printing in .Net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Posts
    16

    Printing in .Net

    Does anyone know how to print. Every quickstart tutorial I tried failed. Printing in .Net cannot be as hard as it has been so far. I even found things that were not part of classes.

    All I want to do is print a file. Even the books I have and the websites out there leave the answer out of the explanation. They explain what printing is. I know what I want it to do, I just need to figure out how.

    I'm thinking 1.Load Printer objects - 2.Load document - 3.Select printer - 4.Print document.

    Based on the info out there printing is an obscure task and everyone uses felt tip pens and have monks to make hand copies but I would have to guess that somebody else has tried to print something in VB.Net.

  2. #2
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256
    VB Code:
    1. Private StreamToPrint As IO.StreamReader
    2.  
    3. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    4.         Dim LinesPerPage As Single = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics)
    5.         Dim Line As String = Nothing, count As Integer, yPos As Single = 0
    6.         For count = 0 To LinesPerPage - 1
    7.             Line = StreamToPrint.ReadLine
    8.             If Line Is Nothing Then Exit For
    9.             yPos = e.MarginBounds.Top + count * PrintFont.GetHeight(e.Graphics)
    10.             e.Graphics.DrawString(Line, PrintFont, Brushes.Black, e.MarginBounds.Left, yPos, New StringFormat())
    11.         Next
    12.         If Not (Line Is Nothing) Then e.HasMorePages = True
    13. End Sub
    14.  
    15. Private Sub MenuItemPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemPrint.Click
    16.         StreamToPrint = New IO.StreamReader("temp.xml")
    17.         PrintDialog1.Document = PrintDocument1
    18.         PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
    19.         PrintDialog1.AllowSomePages = True
    20.         If PrintDialog1.ShowDialog = DialogResult.OK Then
    21.             PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
    22.             PrintDocument1.Print()
    23.             If Not (StreamToPrint Is Nothing) Then
    24.                 StreamToPrint.Close()
    25.             End If
    26.         End If
    27. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Posts
    16

    printing

    Thanks jkw119,


    If anyone is interested, this code works. I found some similiar code last night and tried it out. Looks like I will be using it a lot for priniting reports after performing functionality tests so I tossed the code into a class to make life easier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width