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. How to show Text File in PrintPreviewDialog in vb.net (WinForm)

How to show Text File in PrintPreviewDialog in vb.net (WinForm)

Scheduled Pinned Locked Moved Visual Basic
csharpgraphicstutorialquestion
1 Posts 1 Posters 0 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.
  • K Offline
    K Offline
    kedarrkulkarni
    wrote on last edited by
    #1

    Hello all, I was trying to print the text file in vb.net (winform) After googling i got this code... Let me explain in detail... I Create a file at runtime based on user request. user can print that file after viewing it. I want to show that file in PrintPreviewDialog and if user chooses to print then it can be. The PrintPage event i m using is as

    Dim pd As New PrintDocument

    AddHandler pd.PrintPage, AddressOf pd_PrintPage

    'Event for printpage
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
    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

        linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
        ' Iterate over the file, printing each line.
        While count < linesPerPage + 9  ' manipulate the pagelength according to your interest
    
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count \* printFont.GetHeight(e.Graphics)
            e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, \_
                yPos, New StringFormat)
            count += 1
        End While
        Pgnum = Pgnum + 1
    
        If Not (line Is Nothing) Then
            e.HasMorePages = True
            count = count + 1
            yPos = topMargin + count \* printFont.GetHeight(e.Graphics)
            e.Graphics.DrawString("Page:" + CStr(Pgnum), printFont, Brushes.Black, leftMargin + 700, \_
                            yPos, New StringFormat)
        Else
            e.HasMorePages = False
            count = count + 1
            yPos = topMargin + count \* printFont.GetHeight(e.Graphics)
            e.Graphics.DrawString("Page:" + CStr(Pgnum), printFont, Brushes.Black, leftMargin + 700, \_
                            yPos, New StringFormat)
        End If
    End Sub
    

    But i m not sure where i should add this code to show document in oreview

    PrintPreviewDialog1.Document = pd
    PrintPreviewDialog1.ShowDialog()

    am i missing something. or completely wrong? Thanks.........! ___________________________________________ .Net Developer

    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