1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
   |  Private Sub MenuImprimerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuImprimer.Click
        _sStringToPrint = TxtBoxTexteFichierGed.Text
        With PrintDialog
            .Document = PrintDocument
            .AllowSomePages = True
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                PrintDocument.Print()
            End If
        End With
    End Sub
 
    Private Sub PrintDocumentPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
        Dim numChars As Integer
        Dim numLines As Integer
        Dim stringForPage As String
        Dim strFormat As New StringFormat()
        Dim PrintFont As Font
        Static numPage As Integer = 0
        Dim numPremièrePage As Integer = 0
        Dim numDernièrePage As Integer = 0
        PrintFont = TxtBoxTexteFichierGed.Font
        Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
        Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
        strFormat.Trimming = StringTrimming.Word
        e.Graphics.MeasureString(_sStringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
        stringForPage = _sStringToPrint.Substring(0, numChars)
        'Si l'option "Toutes les pages" est cochée on fixe NumDernièrePage à 10000
        'sinon on définit le range
 
        If e.PageSettings.PrinterSettings.PrintRange <> PrintRange.AllPages Then
            numPremièrePage = e.PageSettings.PrinterSettings.FromPage
            numDernièrePage = e.PageSettings.PrinterSettings.ToPage
        Else
            numDernièrePage = 10000
        End If
        numPage += 1
        If numPage >= numPremièrePage And numPage <= numDernièrePage Then
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
        End If
        If numChars < _sStringToPrint.Length Then
                _sStringToPrint = _sStringToPrint.Substring(numChars)
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
        If numPage > numDernièrePage Then e.HasMorePages = False
    End Sub | 
Partager