R
ReidarT
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
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