Option Strict On
Option Explicit On
Imports System.IO
Imports System.Drawing.Printing
Imports System
Imports System.Drawing
Public Class Form1
Private TotalToPrint As Integer = 0
Private iPageNumber As Integer = 0
Private blnPrintPreview As Boolean = False
Private pdPrinter As PrintDocument
Private ppvwPrinter As PrintPreviewDialog
Private Sub Button1_CLick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
pdPrinter = New PrintDocument
ppvwPrinter = New PrintPreviewDialog
AddHandler pdPrinter.BeginPrint, AddressOf TestDuplex_BeginPrint
AddHandler pdPrinter.PrintPage, AddressOf TestDuplex_PrintPage
AddHandler pdPrinter.EndPrint, AddressOf TestDuplex_EndPrint
If blnPrintPreview = True Then
ppvwPrinter.Document = pdPrinter
ppvwPrinter.Width = Screen.PrimaryScreen.Bounds.Width
ppvwPrinter.Height = Screen.PrimaryScreen.Bounds.Height
ppvwPrinter.PrintPreviewControl.Zoom = 0.5
ppvwPrinter.ShowDialog()
Else
pdPrinter.PrintController = New StandardPrintController 'silent printing
pdPrinter.Print()
End If
RemoveHandler pdPrinter.BeginPrint, AddressOf TestDuplex_BeginPrint
RemoveHandler pdPrinter.PrintPage, AddressOf TestDuplex_PrintPage
RemoveHandler pdPrinter.EndPrint, AddressOf TestDuplex_EndPrint
pdPrinter.Dispose()
ppvwPrinter.Dispose()
End Sub
Private Sub TestDuplex_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
pdPrinter.DocumentName = "Test Duplex"
pdPrinter.OriginAtMargins = True
pdPrinter.DefaultPageSettings.Landscape = True
pdPrinter.PrinterSettings.Duplex = Printing.Duplex.Vertical
pdPrinter.PrinterSettings.Copies = 2
TotalToPrint = 3
iPageNumber = 0
End Sub
Private Sub TestDuplex_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)
iPageNumber += 1
e.Graphics.PageUnit = GraphicsUnit.Millimeter
e.Graphics.DrawString("PAGE " & iPageNumber.ToString, New Font("Arial", 8, FontStyle.Bold), Brushes.Black, 20, 20)
If iPageNumber < TotalToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub TestDuplex_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
'clean up here if necessary
End Sub
End Class