Its your lucky day here is an example.
Just call the preview dialog. The printout drawing is produced on the dialog preview screen. Click ok button on dialog and the same code is used to print the drawing on the printer paper.
There are many ways to do it. This example shows basic printing in columns by using a monotype font. Also printing with word wrap. And finally printing multiple pages.
The example makes the controls just cut and paste into an empty form.
Code:Imports System.Drawing.Printing Public Class PrintTextBasicExample Private WithEvents Button1 As New Button With {.Parent = Me, .Text = "Print...", .Location = New Point(100, 100)} Private WithEvents PrintDocument1 As New PrintDocument Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog Private MasterData, PrintString As String Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load Text = "Basic Text Printing" 'make the column text data MasterData = "Item Description Quantity Unit Price Amount" & vbLf & "_________________________________________________________________" & vbLf & vbLf For i As Integer = 1000 To 1040 MasterData += i.ToString & " 24 in RCP 2200 30 66000" & vbLf & vbLf Next 'make the Word "Wrap text data MasterData += vbLf & vbLf For i As Integer = 1000 To 1040 MasterData += "This is word wrap data for the printing example and contains mostly sentences." If i = 1010 Or i = 1020 Then MasterData += vbLf & vbLf Next End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try PrintString = MasterData PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() Catch ex As Exception MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation) End Try End Sub Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'draw the margins Dim rect As Rectangle = e.MarginBounds rect.Inflate(50, 50) e.Graphics.DrawRectangle(Pens.Black, rect) Dim charactersOnPage, linesPerPage As Integer Using f As Font = New Font("Consolas", 12) ' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(PrintString, f, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Draws the string within the bounds of the page e.Graphics.DrawString(PrintString, f, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic) End Using ' Remove the portion of the string that has been printed. PrintString = PrintString.Substring(charactersOnPage) ' Check to see if more pages are to be printed. e.HasMorePages = PrintString.Length > 0 'reload string for actual printing after preview If Not e.HasMorePages Then PrintString = MasterData End Sub End Class





Reply With Quote
