Click here to Skip to main content
15,886,676 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 597.5K   25.9K   218  
A PrintPreviewDialog that is faster and better looking than the standard one
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CoolPrintPreview
{
    public partial class Form1 : Form
    {
        DateTime _start;
        int _page = 0;
        Font _font = new Font("Segoe UI", 14);

        public Form1()
        {
            InitializeComponent();
        }

        void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            _start = DateTime.Now;
            _page = 0;
        }
        void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // fill page with text
            Rectangle rc = e.MarginBounds;
            rc.Height = _font.Height + 10;
            for (int i = 0; ; i++)
            {
                var text = string.Format("line {0} on page {1}", i + 1, _page + 1);
                e.Graphics.DrawString(text, _font, Brushes.Black, rc);
                rc.Y += rc.Height;
                if (rc.Bottom > e.MarginBounds.Bottom)
                {
                    break;
                }
            }
                
            // move on to next page
            _page++;
            e.HasMorePages = _chkLongDoc.Checked
                ? _page < 3000
                : _page < 30;
        }
        void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            Console.WriteLine("Document rendered in {0} ms", 
                DateTime.Now.Subtract(_start).TotalMilliseconds);
        }

        // show standard print preview
        void button1_Click(object sender, EventArgs e)
        {
            using (var dlg = new PrintPreviewDialog())
            {
                dlg.Document = this.printDocument1;
                dlg.ShowDialog(this);
            }
        }

        // show cool print preview
        void button2_Click(object sender, EventArgs e)
        {
            using (var dlg = new CoolPrintPreviewDialog())
            {
                dlg.Document = this.printDocument1;
                dlg.ShowDialog(this);
            }
        }
    }
}

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