Wrapped Printing
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Drawing
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.cmdPrintWrapped = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.cmdPrint = New System.Windows.Forms.Button
Me.txtData = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
Me.cmdPrintWrapped.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.cmdPrintWrapped.Location = New System.Drawing.Point(210, 174)
Me.cmdPrintWrapped.Name = "cmdPrintWrapped"
Me.cmdPrintWrapped.Size = New System.Drawing.Size(104, 24)
Me.cmdPrintWrapped.TabIndex = 7
Me.cmdPrintWrapped.Text = "Print Wrapped"
'
Me.Label1.Location = New System.Drawing.Point(14, 10)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(164, 16)
Me.Label1.TabIndex = 6
Me.Label1.Text = "Text To Print:"
'
'cmdPrint
'
Me.cmdPrint.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.cmdPrint.Location = New System.Drawing.Point(14, 174)
Me.cmdPrint.Name = "cmdPrint"
Me.cmdPrint.Size = New System.Drawing.Size(104, 24)
Me.cmdPrint.TabIndex = 5
Me.cmdPrint.Text = "Print Unwrapped"
'
'txtData
'
Me.txtData.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtData.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtData.Location = New System.Drawing.Point(14, 30)
Me.txtData.Multiline = True
Me.txtData.Name = "txtData"
Me.txtData.ReadOnly = True
Me.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtData.Size = New System.Drawing.Size(300, 132)
Me.txtData.TabIndex = 4
Me.txtData.Text = resources.GetString("txtData.Text")
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(329, 212)
Me.Controls.Add(Me.cmdPrintWrapped)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.cmdPrint)
Me.Controls.Add(Me.txtData)
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "Form1"
Me.Text = "Wrapped Printing"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents cmdPrintWrapped As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cmdPrint As System.Windows.Forms.Button
Friend WithEvents txtData As System.Windows.Forms.TextBox
Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
Dim MyDoc As New PrintDocument()
AddHandler MyDoc.PrintPage, AddressOf UnWrappedPrint
Dim dlgSettings As New PrintDialog()
dlgSettings.Document = MyDoc
Dim Result As DialogResult
Result = dlgSettings.ShowDialog()
If Result = DialogResult.OK Then MyDoc.Print()
End Sub
Private Sub cmdPrintWrapped_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintWrapped.Click
Dim MyDoc As New PrintDocument()
AddHandler MyDoc.PrintPage, AddressOf WrappedPrint
Dim dlgSettings As New PrintDialog()
dlgSettings.Document = MyDoc
Dim Result As DialogResult
Result = dlgSettings.ShowDialog()
If Result = DialogResult.OK Then MyDoc.Print()
End Sub
Private Sub UnWrappedPrint(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim MyFont As New Font("Verdana", 16)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
e.Graphics.DrawString(txtData.Text, MyFont, Brushes.Black, x, y)
End Sub
Private Sub WrappedPrint(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim MyFont As New Font("Verdana", 16)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
e.Graphics.DrawString(txtData.Text, MyFont, Brushes.Black, e.MarginBounds, StringFormat.GenericDefault)
End Sub
Private Sub WrappedPrint2(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim MyFont As New Font("Verdana", 16)
Dim LineHeight As Single = MyFont.GetHeight(e.Graphics)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
Dim Line As String
Dim ParsedLines As New ArrayList()
Dim TextToPrint As String = txtData.Text
Dim i As Integer
Do
Line &= TextToPrint.Chars(0)
TextToPrint = TextToPrint.Substring(1)
If Line.EndsWith(" ") Then
If e.Graphics.MeasureString(Line, MyFont).Width > (e.PageBounds.Width - 300) Then
ParsedLines.Add(Line)
Line = ""
End If
End If
Loop While TextToPrint <> ""
ParsedLines.Add(Line)
For Each Line In ParsedLines
e.Graphics.DrawString(Line, MyFont, Brushes.Black, x, y)
y += LineHeight
Next
End Sub
End Class
Related examples in the same category