Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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.

Localization

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 EnhancedPrintPreviewDialog.it-IT.resx 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() {
    InitializeComponent();
    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;
NewPreview.ShowDialog();

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
     )
);

License

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

Share

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.
 


"Short code, good code"



Comments and Discussions

 
QuestionAdd an auto close after print feature? PinmemberAllen Johnson III4-Sep-14 9:29 
QuestionExcellent (five hundreds star!!!) article Pinmemberferhat_11-Jan-14 11:55 
QuestionExcellent PinmemberAndrewGMatondo24-Apr-12 7:55 
QuestionBy-passing print preview PinmemberMember 428156627-Mar-12 7:44 
GeneralMy vote of 5 PinmemberProEnggSoft27-Feb-12 14:41 
QuestionThanks for nice utility PinmemberSunil Apte17-Oct-11 0:20 
GeneralThanks Pinmembereibbed4-Oct-10 11:22 
GeneralVB.net Version Pinmemberkumika25-Aug-10 6:28 
GeneralRe: VB.net Version PinmemberAllen Johnson III18-Aug-14 9:11 
QuestionHow do we use your print preview Pinmembernoka032-Nov-09 19:06 
AnswerRe: How do we use your print preview PinmemberVincenzo Rossi3-Nov-09 1:05 
GeneralBUG: The margin value decreases every time PageSetupDialog is displayed PinmemberGwannoes26-May-09 23:44 
GeneralHey Pinmemberww.mubshir.com15-May-09 3:18 
GeneralRe: Hey PinmemberVincenzo Rossi16-May-09 3:10 
GeneralPerfetto PinmemberJamal Alqabandi30-Apr-09 12:01 
GeneralPerfect PinmemberFred#26-Apr-09 8:01 
GeneralRe: Perfect PinmemberVincenzo Rossi26-Apr-09 10:46 
GeneralAwesome PinmemberVCSKicks23-Apr-09 8:25 
GeneralRe: Awesome PinmemberVincenzo Rossi23-Apr-09 22:29 

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

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

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