1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
Public Class Form3
Private fileName As String = String.Empty
Private printFont As Font
Private streamToPrint As StreamReader
Private Sub printButton_Click(sender As System.Object, e As System.EventArgs) Handles printButton.Click
fileName = Application.StartupPath + "\TextFile1.txt"
Try
streamToPrint = New StreamReader(fileName)
Try
printFont = New Font("Arial", 16)
Me.PrintPreviewDialog1.Document = Me.PrintDocument1
If (Me.PrintPreviewDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
Me.PrintDocument1.Print()
End If
'TextBox1.Text = streamToPrint.ReadToEnd()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim gr = e.Graphics
Dim linesPerPage As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim yPos As Single = topMargin
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
' Print each line of the file.
While yPos < e.MarginBounds.Bottom
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
Dim sizeLine As SizeF = gr.MeasureString(line, printFont, e.MarginBounds.Width) 'mesure la ligne
Dim layoutRect As RectangleF = New RectangleF(leftMargin, yPos, sizeLine.Width, sizeLine.Height) 'cree un rectangle Layout dont Largeur=Largeur de ligne
yPos += sizeLine.Height 'incremente la hauteur de ligne (hauteur variable)
gr.DrawString(line, printFont, Brushes.Black, layoutRect)
gr.DrawRectangle(Pens.Red, Rectangle.Round(layoutRect))
End While
' If more lines exist, print another page.
If (line IsNot Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
End Class |
Partager