![]() |
|
Tipp 0170
|
Mehrseitige Textdateien drucken
|
 |
|
Autor/Einsender: Datum: |
|
Michael Werner 14.10.2007 |
|
| Entwicklungsumgebung: |
|
VB.Net 2005 |
| Framework: |
|
2.0 |
|
|
|
Um eine Datei zu drucken, wird normalerweise eine Instanz der PrintDocument-Klasse erzeugt,
die Druckeigenschaften festgelegt und mit der Methode Print der Druckvorgang gestartet.
Mit Print wird das den seitenweisen Ausdruck bestimmende PrintPage-Ereignis ausgelöst.
|
Im PrintPage-Event wird die Anzahl der Zeilen je Seite berechnet. Die schon gedruckten Zeilen
werden hochgezählt und wenn es am Ende einer Seite noch weitere zu druckende Zeilen gibt,
wird mit e.HasMorePages = True das PrintPage-Ereignis erneut angestoßen und eine weitere Seite gedruckt.
|
|
| |
Imports System.IO
Imports System.Drawing.Printing
'...
Private printFont As Font
Private streamToPrint As StreamReader
Private fileToPrint As String
' Das PrintPage-Event wird für jede zu druckende Seite angestoßen
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As _
PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
' Die Anzahl der zeilen einer Seite berechnen.
linesPerPage = ev.MarginBounds.Height / _
printFont.GetHeight(ev.Graphics)
' Zeilenweise drucken.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black, _
leftMargin, yPos, New StringFormat())
count += 1
End While
' Wenn es noch Zeilen gibt, eine neue Seite drucken.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
Private Sub printButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles printButton.Click
fileToPrint = ChooseFile()
If fileToPrint = "" Then
Exit Sub
End If
Try
'Mit Encoding.Default werden auch Sonderzeichen gedruckt
streamToPrint = New StreamReader(fileToPrint, _
System.Text.Encoding.Default)
Try
printFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
|
|
|
|
|
|
|
| Windows-Version |
| 98/SE |
 |
|
| ME |
 |
|
| NT |
 |
|
| 2000 |
 |
|
| XP |
 |
|
| Vista |
 |
|
| Win
7 |
 |
|
|
|
Download (17,2
kB)
|
Downloads bisher: [ 336 ]
|
|
|