با استفاده از کد زیر که در کلاس فرم 1 تعریف شده است شما می توانید یک فایل متنی را با هر چند صفحه پرینت بگیرید
که البته قبل از پرینت گرفتن تنظیمات و مشاهده قبلی نیز می توانید داشته باشید
خوشحال میشم بازیم رو امتحان کنیدImports System.Drawing.Printing
Imports System.IO
Public Class Form1
Private WithEvents bt As New Button
Private PrintPageSettings As New PageSettings
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FilePath As String
'Display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.*)|*.*"
OpenFileDialog1.ShowDialog()
'If Cancel button not selected, load FilePath variable
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'Read text file and load into RichTextBox1
RichTextBox1.LoadFile(FilePath, RichTextBoxStreamType.PlainText)
'Initialize string to print
StringToPrint = RichTextBox1.Text
'Enable Print button
Button2.Enabled = True
Catch ex As Exception
'display error messages if they appear
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
'Specify current page settings
'Specify document for print dialog box and show
StringToPrint = RichTextBox1.Text
Dim result As DialogResult = PrintDialog1.ShowDialog()
'If click OK, print document to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
PrintDocument1.DefaultPageSettings = PrintPageSettings
PrintDialog1.Document = PrintDocument1
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat
'Based on page setup, define drawable rectangle on page
Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
'Define area to determine how much text can fit on a page
'Make height one line shorter to ensure text doesn't clip
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
'When drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'Compute how many chars and lines can fit based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
'Compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, numChars)
'Print string on current page
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
'If there is more text, indicate there are more pages
If numChars < StringToPrint.Length Then
'Subtract text from string that has been printed
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'All text has been printed, so restore string
StringToPrint = RichTextBox1.Text
End If
End Sub
Private Sub btnSetup_Click(sender As Object, e As EventArgs) Handles btnSetup.Click
Try
'Load page settings and display page setup dialog box
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnPreview_Click(sender As Object, e As EventArgs) Handles btnPreview.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print preview dialog box and show
StringToPrint = RichTextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub
End Class