V prvom rade musis mat pridaný v projekte PrintDialog a PrintDocument kod vyzerá nasledovne.
Private TlacenyRetazec As String
Private NastavenieStrankyPreTlac As New Printing.PageSettings
Dim PismoTlace As New Font("Fixedsys", 10, FontStyle.Bold)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Urci nastavenie strany
PrintDocument1.DefaultPageSettings = NastavenieStrankyPreTlac
' Urci dokument pre dialog Tlac
TlacenyRetazec = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim Vysledok As DialogResult = PrintDialog1.ShowDialog()
' Ak uzivatel stlaci tlacitko OK, vytlaci dokument
If Vysledok = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim PocetZnakov As Integer
Dim PocetRiadkov As Integer
Dim retazecPreStranku As String
Dim formatRetazca As New StringFormat
' Na zaklade vzhladu stranky definovat obdlznik pre tlac na stranke
Dim navrhObdlzniku As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
' Definovat oblast pre zistenie kolko textu sa vojde na stranku
' Obmedzenie vysky o jeden riadok aby text nebol orezany
Dim velkost As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PismoTlace.GetHeight(e.Graphics))
' Dlhe retazce zalomit medzi slova
formatRetazca.Trimming = StringTrimming.Word
' Vypocitat kolko znakov a riadkov sa vojde na stranku
e.Graphics.MeasureString(TlacenyRetazec, PismoTlace, velkost, formatRetazca, PocetZnakov, PocetRiadkov)
' Vypocita retazec ktory sa vojde na stranku
retazecPreStranku = TlacenyRetazec.Substring(0, PocetZnakov)
' Vytlacit retazec na aktualnej stranke
e.Graphics.DrawString(retazecPreStranku, PismoTlace, Brushes.Black, navrhObdlzniku, formatRetazca)
' Ak je dalsi text indikovat viacej stranok
If PocetZnakov < TlacenyRetazec.Length Then
TlacenyRetazec = TlacenyRetazec.Substring(PocetZnakov)
e.HasMorePages = True
Else
e.HasMorePages = False
TlacenyRetazec = RichTextBox1.Text
End If
End Sub
PS: uprvene :) dodo
|