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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'------------------------- Choix de l'imprimante ----------------------
'Dim dlg As New PrintDialog
'dlg.Document = PrintDocument1
'Dim result As DialogResult = dlg.ShowDialog() = PrintPreviewDialog1.ShowDialog()
'If (result = System.Windows.Forms.DialogResult.OK) Then
' PrintDocument1.Print()
'End If
'---------------------------------------------------------------------
'-------------- visualisation du document Avant Impression ------------
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.1
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
'--------------------------------------------------------------------------
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.SetDigitSubstitution(New Globalization.CultureInfo("Ar").LCID, StringDigitSubstitute.National)
fmt.FormatFlags = StringFormatFlags.DirectionRightToLeft
fmt.Trimming = StringTrimming.EllipsisCharacter
fmt.LineAlignment = StringAlignment.Near
Dim y As Single = e.MarginBounds.Top
Do While mRow < .RowCount - 1
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) ' Cadriage du tableau
If (newpage) Then
e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width 'espacement entre les colones de 1.1
h = Math.Max(h, rc.Height) 'espacement entre les lignes de 1.1
'x += 1.1 * rc.Width 'espacement entre les colones de 1.1
'h = Math.Max(h, rc.Height) * 1.1 'espacement entre les lignes de 1.1
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub |
Partager