Introduction
Here is a simple way to ensure your text wraps when you display a PrintDialog or a PrintPreviewDialog.
Using the code
The code is simple...here is the code for the PrintDialog:
private printFont As Font = New Font("Tahoma", 12, FontStyle.Regular)
private aBunchOfLongLines As String
Private Sub New()
aBunchOfLongLines = "This software is provided 'as-is', with no warrenties " & _
"of any kind stating operability of a specific " & vbNewLine
aBunchOfLongLines &= "or error free operation. Use of " & _
"the application is entirely the user's responsibility. " & vbNewLine
aBunchOfLongLines &= "The software vendor assumes no responsibility " & _
"for use of this application. This includes damage " & vbNewLine
aBunchOfLongLines &= "which may occur during operation of the " & _
"application to the users files or computer system. " & vbNewLine
aBunchOfLongLines &= "By use of this application, you agree that you " & _
"will take full responsibility for any damage that may " & vbNewLine
aBunchOfLongLines &= "occur. If you do not wish to use this " & _
"application any further, please uninstall it from your computer."
End Sub
Private Sub tsbPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tsbPrint.Click
Dim pd As New Printing.PrintDocument()
Me.PrintDialog1.AllowPrintToFile = False
Me.PrintDialog1.AllowSelection = False
Me.PrintDialog1.AllowSomePages = False
Me.PrintDialog1.UseEXDialog = True
Me.PrintDialog1.Document = pd
If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Dim aBytes() As Byte = System.Text.Encoding.ASCII.GetBytes( _
aBunchOfLongLines)
Dim strmMem As New MemoryStream(aBytes)
streamToPrint = New IO.StreamReader(strmMem)
pd.PrinterSettings = Me.PrintDialog1.PrinterSettings
AddHandler pd.PrintPage, AddressOf pd_PrintPage
pd.Print()
Catch ex As Exception
Finally
streamToPrint.Close()
End Try
End If
End Sub
Here is the code for the PrintPreviewDialog:
Private Sub tsbPreview_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tsbPreview.Click
Dim aBytes() As Byte = System.Text.Encoding.ASCII.GetBytes( _
aBunchOfLongLines)
Dim strmMem As New MemoryStream(aBytes)
streamToPrint = New IO.StreamReader(strmMem)
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
Me.PrintPreviewDialog1.AllowTransparency = False
Dim dlg As Form = DirectCast(Me.PrintPreviewDialog1, Form)
dlg.Width = 600
dlg.Height = 400
dlg.WindowState = FormWindowState.Maximized
Me.PrintPreviewDialog1.Document = pd
Me.PrintPreviewDialog1.ShowDialog()
streamToPrint.Close()
End Sub
Here is the actual output to the PrintDocument. Notice the StringFormat object. This is what causes the wrapping, and also can be used to measure the text area for the line.
Private Sub pd_PrintPage(ByVal sender As Object, _
ByVal ev As Printing.PrintPageEventArgs)
Dim yPos As Single = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
Dim actual As SizeF = Nothing
yPos = topMargin
While yPos < ev.MarginBounds.Top + ev.MarginBounds.Size.Height
line = streamToPrint.ReadLine()
Dim sf As StringFormat = StringFormat.GenericTypographic
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Near
sf.FormatFlags = StringFormatFlags.LineLimit
sf.Trimming = StringTrimming.Word
If line Is Nothing Then
Exit While
ElseIf line.Equals("") Then
line = " "
End If
actual = ev.Graphics.MeasureString(line, printFont, _
New SizeF(ev.MarginBounds.Size.Width, _
ev.MarginBounds.Size.Height), sf)
ev.Graphics.DrawString(line, printFont, Brushes.Black, _
New RectangleF(leftMargin, yPos, ev.MarginBounds.Size.Width, _
ev.MarginBounds.Size.Height), sf)
yPos = yPos + actual.Height
End While
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
Hope this helps everyone out.