Imports System.Drawing.Printing

Public Class Form1

    'creo una variabile per gestire i dcumenti di stampa
    Private WithEvents pdocStampa As New PrintDocument()
    'costante per il titolo delle messagebox
    Const titolo As String = "Esempio Stampa"
    Private Sub BtnChiudi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChiudi.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (System.IO.File.Exists(Environment.CurrentDirectory & "\testo.rtf")) Then rtbTesto.LoadFile(Environment.CurrentDirectory & "\testo.rtf")
    End Sub

    Private Sub BtnAnteprima_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAnteprima.Click
        Dim ppdStampa As New PrintPreviewDialog()
        Try
            With ppdStampa

                .Document = pdocStampa
                .StartPosition = FormStartPosition.CenterScreen

                .ShowDialog()
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message, titolo, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub pdocStampa_printpage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocStampa.PrintPage
        Dim ImgStampa As Image = pbxLogo.Image
        'imposto il carattere per la stampa ed anteprima
        Dim fntSTampa As New Font(Me.Font.FontFamily, 10, FontStyle.Regular)
        'imposto altezza
        Dim IntAltezza As Integer = e.MarginBounds.Top + 50
        'imposto l'immagine
        Dim RctImmagine As New Rectangle(e.MarginBounds.Left + 20, e.MarginBounds.Top - 30, 48, 48)

        'ottengo il carattere corrente
        Static IntCarattere As Integer

        Dim intCaratteriInseriti As Integer = 0
        Dim intLineeInserite As Integer = 0
        Dim Fmt As New StringFormat(StringFormatFlags.LineLimit)

        Try
            'disegno l'immagine 
            e.Graphics.DrawImage(ImgStampa, RctImmagine)

            Dim intPrintAreaHeight, intPrintAreaWidth, intmarginLeft, intmarginTop As Int32
            'imposto le misure per la stampa
            With pdocStampa.DefaultPageSettings
                'imposto l'area di stampa
                intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
                intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

                'imposto i margini
                intmarginLeft = .Margins.Left ' X coordinate
                intmarginTop = .Margins.Top + 30 ' Y coordinate
            End With
            'area di stampa
            Dim RctAreaDiStampa As New RectangleF(intmarginLeft, intmarginTop, intPrintAreaWidth, intPrintAreaHeight)
            'verifico che le stringa entri nell'area di stampa
            e.Graphics.MeasureString(Mid(rtbTesto.Text, IntCarattere + 1), fntSTampa, _
             New SizeF(intPrintAreaWidth, intPrintAreaHeight), Fmt, intCaratteriInseriti, intLineeInserite)
            e.Graphics.DrawString(Mid(rtbTesto.Text, IntCarattere + 1), fntSTampa, _
            Brushes.Black, RctAreaDiStampa, Fmt)
            'conto il carattere che sto inserendo
            IntCarattere += intCaratteriInseriti
            'verifico se devo inerire una pagina aggiuntiva
            If IntCarattere < rtbTesto.Text.Length Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
                'riporto a zero il contatore
                IntCarattere = 0
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, titolo, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


    Private Sub BtnOpzioni_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpzioni.Click
        'creo un oggetto di tipo finestra di opzioni di stampa
        Dim psdOpzioni As New PageSetupDialog()
        Try
            With psdOpzioni

                .Document = pdocStampa
                .PageSettings = pdocStampa.DefaultPageSettings
            End With
            If psdOpzioni.ShowDialog = DialogResult.OK Then
                pdocStampa.DefaultPageSettings = psdOpzioni.PageSettings

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, titolo, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

    Private Sub btnFinestra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinestra.Click
        'creo un oggetto di tipo finestra di dialogo di stampa
        Dim pdlgStampa As New PrintDialog()
        Try
            'toglie la finestra pop up
            pdocStampa.PrintController = New System.Drawing.Printing.StandardPrintController()
            pdlgStampa.Document = pdocStampa
            If pdlgStampa.ShowDialog = DialogResult.OK Then
                pdocStampa.Print()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Stampa", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class
