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