Click here to Skip to main content
13,504,941 members
Click here to Skip to main content
Add your own
alternative version


107 bookmarked
Posted 23 Apr 2009

An Enhanced PrintPreviewDialog

, 23 Apr 2009
Rate this:
Please Sign up or sign in to vote.
An enhanced PrintPreviewDialog with better look, localization, auto page numbers, and additional text.


The classic PrintPreviewDialog included in Windows Forms presents serious limitations when used in the context of real-life applications:

  • You cannot customize the user interface or add functionalities.
  • The only supported language is English, and there is no way to localize it. So, if your application uses another language, you have an interface inconsistency.
  • The user cannot choose the printer, nor modify the page settings. These tasks should be done elsewhere, before opening the preview dialog!
  • It uses an old-style look for its toolbar and buttons. In other words, it's really ugly.

For these reasons, I felt the need to implement my own preview, trying to overcome the exposed problems.

How it Works

The EnhancedPrintPreviewDialog is derived from Form. It includes a toolbar, and uses the standard PrintPreviewControl. The first task was to recreate the preview dialog functionalities, populating the toolbar, and implementing the appropriate handlers to interact with the preview control.

As for the original preview dialog, the fundamental property is Document, which is of the PrintDocument type. When a PrintDocument is assigned to this property, some handlers are attached to the BeginPrint, PrintPage, and EndPrint events of the document. The BeginPrint handler resets a page counter variable, the PrintPage handler increments the counter, and the EndPrint handler shows the total number of pages in the interface.

After that, I thought to add a feature allowing automatic insertions of additional text, including page numbers. So, the AdditionalText class was introduced along with the AdditionalTextList property of the dialog. The PrintPage handler was modified to print the additional text.

Class Architecture

EnhancedPrintPreviewDialog Class

You will use the main class in place of PrintPreviewDialog.

  • Document: gets or sets the PrintDocument related to this EnhancedPrintPreviewDialog.
  • PageSetupDialog: gets or sets a PageSetupDialog. If not provided, a default PageSetupDialog object is created.
  • PrintDialog: gets or sets a PrintDialog. If not provided, a default PrintDialog object is created.
  • PrintPreviewControl: gets the PrintPreviewControl.
  • ShowPageSettingsButton: gets or sets a boolean value indicating whether to show the page setup button in the toolbar.
  • ShowPrinterSettingsButton: gets or sets a boolean value indicating whether to show the print setup button in the toolbar.
  • ShowPrinterSettingsBeforePrint: gets or sets a boolean value indicating whether to show the print dialog before sending data to the printer.
  • UseAntiAlias: gets or sets a boolean value indicating whether to apply the anti aliasing.
  • AdditionalTextList: gets or sets a list of AdditionalText objects to be printed around the content. If not provided, a default empty List<AdditionalText> is created.

AdditionalText Class

This class contains information about additional text to show around the content.

  • Brush: the Brush to use.
  • Color: the Color to use. If you set this property, the brush will be automatically set to a SolidBrush instance.
  • Font: the Font to use.
  • OffsetX: the horizontal offset of the text. It can be negative.
  • OffsetY: the vertical offset of the text. It can be negative.
  • Position: the position of the text.
  • Text: the text to print. If the string contains the $pagenumber placeholder, this will be replaced with the current page number.


The EnhancedPrintPreviewDialog comes with the default English language resources and the additional Italian language resources. You can easily add your specific language following these steps:

In the file system, create a copy of the file and rename it using your language and country suffix (e.g.: "fr-FR").

In the Solution Explorer of Visual Studio, click the "Show All Files" button.

Then, right click on the copied file, and choose "Include in Project".

Double click the file to open the resource editor, and edit the strings. In the comment column, you will find the English text to translate.

To test languages other than your default language, you can force the culture of the current thread:

using System.Threading;
using System.Globalization;
public Form1() {
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("it-IT");

Using the Code

In your projects, add a reference to the PrintPreview.dll. In your code, import the namespace VR.PrintPreview.

using VR.PrintPreview;

Use the EnhancedPrintPreviewDialog in the same way you use the classic PrintPreviewDialog.

EnhancedPrintPreviewDialog NewPreview = new EnhancedPrintPreviewDialog();
NewPreview.Document = sample.PrintDocument;

Use the new properties to set the additional features:

NewPreview.AdditionalTextList.Add(new AdditionalText(
      "Page - $pagenumber -"         // text
     , new Font("Comic sans MS", 10) // font
     , Brushes.Red                   // brush
     , TextPosition.HBottomLeft      // position
     , 0                             // OffsetX
     , 0                             // OffsetY


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vincenzo Rossi
Software Developer (Senior)
Italy Italy
I'm a graduate in Computer Science.
I work with C++, Visual Basic 6, C#, asp, asp.Net, Windows Forms, SQL Server, Access, Flash.

I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Vivo così from the album L'immobile disegno (The immobile drawing).

"Short code, good code"

You may also be interested in...

Comments and Discussions

QuestionPrints Blank Pages Pin
trantrum13-Apr-16 0:50
professionaltrantrum13-Apr-16 0:50 
GeneralMy vote of 5 Pin
professionalNASSIM LOUCHANI14-Dec-15 0:56 
QuestionAdd an auto close after print feature? Pin
Allen Johnson III4-Sep-14 9:29
memberAllen Johnson III4-Sep-14 9:29 
QuestionExcellent (five hundreds star!!!) article Pin
ferhat_11-Jan-14 11:55
memberferhat_11-Jan-14 11:55 
QuestionExcellent Pin
AndrewGMatondo24-Apr-12 7:55
memberAndrewGMatondo24-Apr-12 7:55 
QuestionBy-passing print preview Pin
Member 428156627-Mar-12 7:44
memberMember 428156627-Mar-12 7:44 
GeneralMy vote of 5 Pin
ProEnggSoft27-Feb-12 14:41
memberProEnggSoft27-Feb-12 14:41 
QuestionThanks for nice utility Pin
Sunil Apte17-Oct-11 0:20
memberSunil Apte17-Oct-11 0:20 
GeneralThanks Pin
eibbed4-Oct-10 11:22
membereibbed4-Oct-10 11:22 Version Pin
kumika25-Aug-10 6:28
memberkumika25-Aug-10 6:28 
GeneralRe: Version Pin
Allen Johnson III18-Aug-14 9:11
memberAllen Johnson III18-Aug-14 9:11 
QuestionHow do we use your print preview Pin
noka032-Nov-09 19:06
membernoka032-Nov-09 19:06 
AnswerRe: How do we use your print preview Pin
Vincenzo Rossi3-Nov-09 1:05
memberVincenzo Rossi3-Nov-09 1:05 
GeneralBUG: The margin value decreases every time PageSetupDialog is displayed Pin
Gwannoes26-May-09 23:44
memberGwannoes26-May-09 23:44 
GeneralHey Pin
ww.mubshir.com15-May-09 3:18
memberww.mubshir.com15-May-09 3:18 
GeneralRe: Hey Pin
Vincenzo Rossi16-May-09 3:10
memberVincenzo Rossi16-May-09 3:10 
GeneralPerfetto Pin
Jamal Alqabandi30-Apr-09 12:01
memberJamal Alqabandi30-Apr-09 12:01 
GeneralPerfect Pin
Fred#26-Apr-09 8:01
memberFred#26-Apr-09 8:01 
GeneralRe: Perfect Pin
Vincenzo Rossi26-Apr-09 10:46
memberVincenzo Rossi26-Apr-09 10:46 
GeneralAwesome Pin
VCSKicks23-Apr-09 8:25
memberVCSKicks23-Apr-09 8:25 
GeneralRe: Awesome Pin
Vincenzo Rossi23-Apr-09 22:29
memberVincenzo Rossi23-Apr-09 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 23 Apr 2009
Article Copyright 2009 by Vincenzo Rossi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid