473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PrintDocument with streamreader gives blank pages

I use this code to print a textfile, but it gives me a lot of blank pages.
The file contains 9 lines of text.

Private strFileName As String = "C:\Sendepost\LoggFil.txt"
Private objStreamToPrint As StreamReader
Private objPrintFont As Font

Private Sub cmdSkrivUt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSkrivUt.Click
Dim objPrintDocument As PrintDocument = New PrintDocument
objPrintDocument.DocumentName = "Utskrift av logg"
PrintDialog1.AllowPrintToFile = False
PrintDialog1.AllowSelection = True
PrintDialog1.AllowSomePages = False
PrintDialog1.Document = objPrintDocument

If PrintDialog1.ShowDialog() = DialogResult.OK Then
objStreamToPrint = New StreamReader(strFileName)
objPrintFont = New Font("Arial", 10, FontStyle.Regular)
AddHandler objPrintDocument.PrintPage, AddressOf
objPrintDocument_PrintPage
objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings
objPrintDocument.Print()
objStreamToPrint.Close()
objStreamToPrint = Nothing
End If

End Sub

Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim sngLinesPerpage As Single = 0
Dim sngVerticalPosition As Single = 0
Dim intLineCount As Integer = 0
Dim sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim strLine As String

sngLinesPerpage = _
e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)
strLine = objStreamToPrint.ReadLine()
While (intLineCount < sngVerticalPosition And strLine <> Nothing)
sngVerticalPosition = sngTopMargin + _
(intLineCount * objPrintFont.GetHeight(e.Graphics))

e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
sngLeftMargin, sngVerticalPosition, New StringFormat)
intLineCount = intLineCount + 1

If (intLineCount < sngLinesPerpage) Then
strLine = objStreamToPrint.ReadLine()
End If
End While

If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

End Sub

regards
reidarT
Nov 21 '05 #1
2 3331
One thing I see that is probably wrong is this line

While (intLineCount < sngVerticalPosition And strLine <> Nothing)

I think it should read While (intLineCount < sngLinesPerpage And strLine <>
Nothing).

Try that change and see if it helps.
"ReidarT" <re****@eivon.no> wrote in message
news:u3**************@TK2MSFTNGP15.phx.gbl...
I use this code to print a textfile, but it gives me a lot of blank pages.
The file contains 9 lines of text.

Private strFileName As String = "C:\Sendepost\LoggFil.txt"
Private objStreamToPrint As StreamReader
Private objPrintFont As Font

Private Sub cmdSkrivUt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSkrivUt.Click
Dim objPrintDocument As PrintDocument = New PrintDocument
objPrintDocument.DocumentName = "Utskrift av logg"
PrintDialog1.AllowPrintToFile = False
PrintDialog1.AllowSelection = True
PrintDialog1.AllowSomePages = False
PrintDialog1.Document = objPrintDocument

If PrintDialog1.ShowDialog() = DialogResult.OK Then
objStreamToPrint = New StreamReader(strFileName)
objPrintFont = New Font("Arial", 10, FontStyle.Regular)
AddHandler objPrintDocument.PrintPage, AddressOf
objPrintDocument_PrintPage
objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings
objPrintDocument.Print()
objStreamToPrint.Close()
objStreamToPrint = Nothing
End If

End Sub

Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim sngLinesPerpage As Single = 0
Dim sngVerticalPosition As Single = 0
Dim intLineCount As Integer = 0
Dim sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim strLine As String

sngLinesPerpage = _
e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)
strLine = objStreamToPrint.ReadLine()
While (intLineCount < sngVerticalPosition And strLine <> Nothing)
sngVerticalPosition = sngTopMargin + _
(intLineCount * objPrintFont.GetHeight(e.Graphics))

e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
sngLeftMargin, sngVerticalPosition, New StringFormat)
intLineCount = intLineCount + 1

If (intLineCount < sngLinesPerpage) Then
strLine = objStreamToPrint.ReadLine()
End If
End While

If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

End Sub

regards
reidarT

Nov 21 '05 #2
Thanks alot. It works perfectly.
reidarT
"William LaMartin" <la******@tampabay.rr.com> skrev i melding
news:Ou**************@TK2MSFTNGP10.phx.gbl...
One thing I see that is probably wrong is this line

While (intLineCount < sngVerticalPosition And strLine <> Nothing)

I think it should read While (intLineCount < sngLinesPerpage And strLine
<> Nothing).

Try that change and see if it helps.
"ReidarT" <re****@eivon.no> wrote in message
news:u3**************@TK2MSFTNGP15.phx.gbl...
I use this code to print a textfile, but it gives me a lot of blank pages.
The file contains 9 lines of text.

Private strFileName As String = "C:\Sendepost\LoggFil.txt"
Private objStreamToPrint As StreamReader
Private objPrintFont As Font

Private Sub cmdSkrivUt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSkrivUt.Click
Dim objPrintDocument As PrintDocument = New PrintDocument
objPrintDocument.DocumentName = "Utskrift av logg"
PrintDialog1.AllowPrintToFile = False
PrintDialog1.AllowSelection = True
PrintDialog1.AllowSomePages = False
PrintDialog1.Document = objPrintDocument

If PrintDialog1.ShowDialog() = DialogResult.OK Then
objStreamToPrint = New StreamReader(strFileName)
objPrintFont = New Font("Arial", 10, FontStyle.Regular)
AddHandler objPrintDocument.PrintPage, AddressOf
objPrintDocument_PrintPage
objPrintDocument.PrinterSettings =
PrintDialog1.PrinterSettings
objPrintDocument.Print()
objStreamToPrint.Close()
objStreamToPrint = Nothing
End If

End Sub

Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim sngLinesPerpage As Single = 0
Dim sngVerticalPosition As Single = 0
Dim intLineCount As Integer = 0
Dim sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim strLine As String

sngLinesPerpage = _
e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)
strLine = objStreamToPrint.ReadLine()
While (intLineCount < sngVerticalPosition And strLine <> Nothing)
sngVerticalPosition = sngTopMargin + _
(intLineCount * objPrintFont.GetHeight(e.Graphics))

e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
sngLeftMargin, sngVerticalPosition, New StringFormat)
intLineCount = intLineCount + 1

If (intLineCount < sngLinesPerpage) Then
strLine = objStreamToPrint.ReadLine()
End If
End While

If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

End Sub

regards
reidarT


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Igor | last post by:
Hi, I have a problem when I attempt to utilize the PrintDocument component. I basically copy word for word the code provided under the PrintDocument overview help, but I keep getting the same...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
4
by: Hexman | last post by:
Code below ---- I've asked a similar question on this forum earlier. This is a slightly different situation. Previous Question ---- I'm trying to save some specific web pages to disk as...
2
by: bp | last post by:
Hi, I try to use my own PreviewDialog with a PrinPreviewControl, to preview a document of type MyPrintDocument, and I want to implement the PrintRange functionnality (print some pages between 2...
1
by: kig25 | last post by:
Hello, When using the VB.NET PrintDocument class, I seem to be encountering an issue where the sub pd_PrintPage handles PrintDocument.PrintPage (upon continuing if HasMorePages = true) will...
0
by: sradhakr | last post by:
When I try to print using PrintDocument class, I notice that a blank page gets printed (apart from my document). Even if I comment out all the code within the function that prints my document, I...
1
by: tghamm | last post by:
Hey everyone, I'm trying to output a text file to a printer, and for some reason, it prints over itself, something that should be 15 pages can be from 4-8 pages, and it changes each time I try it. ...
7
by: =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post by:
Hi, I am using StreamReader to read an ASCII file that contains blank lines. How can I omit reading blank lines? I tried somting like... FileStream inFile = new...
16
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.