Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

An Enhanced PrintPreviewDialog

Rate me:
Please Sign up or sign in to vote.
4.97/5 (106 votes)
28 May 2014Public Domain8 min read 605.3K   25.9K   218  
A PrintPreviewDialog that is faster and better looking than the standard one
Public Class Form1

    ' fields
    Private _font As Font = New Font("Segoe UI", 14.0!)
    Private _page As Integer = 0
    Private _start As DateTime

    ' show standard print preview
    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        Using dlg As PrintPreviewDialog = New PrintPreviewDialog
            dlg.Document = Me.PrintDocument1
            dlg.ShowDialog(Me)
        End Using
    End Sub

    ' show cool print preview
    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        Using dlg As CoolPrintPreviewDialog = New CoolPrintPreviewDialog
            dlg.Document = Me.printDocument1
            dlg.ShowDialog(Me)
        End Using
    End Sub

    ' render document
    Private Sub PrintDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        Me._start = DateTime.Now
        Me._page = 0
    End Sub

    Private Sub PrintDocument1_EndPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        Console.WriteLine("Document rendered in {0} ms", DateTime.Now.Subtract(Me._start).TotalMilliseconds)
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim rc As Rectangle = e.MarginBounds
        rc.Height = (Me._font.Height + 10)
        Dim i As Integer = 0
        Do While True
            Dim text As String = String.Format("line {0} on page {1}", (i + 1), (Me._page + 1))
            e.Graphics.DrawString([text], Me._font, Brushes.Black, rc)
            rc.Y = (rc.Y + rc.Height)
            If (rc.Bottom > e.MarginBounds.Bottom) Then
                Me._page += 1
                If Me._chkLongDoc.Checked Then
                    e.HasMorePages = Me._page < 3000
                Else
                    e.HasMorePages = Me._page < 30
                End If
                Return
            End If
            i += 1
        Loop
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer
Brazil Brazil
Software Architect/Developer with several years experience creating and delivering software.

Full-stack Web development (including React, Firebase, TypeScript, HTML, CSS), Entity Framework, C#, MS SQL Server.

Passionate about new technologies and always keen to learn new things as well as improve on existing skills.

Comments and Discussions