Click here to Skip to main content
15,029,128 members
Articles / Desktop Programming / Windows Forms
Article
Posted 23 Apr 2009

Stats

70.8K views
5K downloads
108 bookmarked

An Enhanced PrintPreviewDialog

Rate me:
Please Sign up or sign in to vote.
4.94/5 (40 votes)
23 Apr 2009CPOL3 min read
An enhanced PrintPreviewDialog with better look, localization, auto page numbers, and additional text.

Image 1

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.

Image 2

Class Architecture

Image 3

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").

Image 4

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

Image 5

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

Image 6

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.

Image 7

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

C#
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.

C#
using VR.PrintPreview;

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

C#
EnhancedPrintPreviewDialog NewPreview = new EnhancedPrintPreviewDialog();
NewPreview.Document = sample.PrintDocument;
NewPreview.ShowDialog();

Use the new properties to set the additional features:

C#
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
Technical Lead
Italy Italy
I'm a graduate in Computer Science.
I work with Metatrader MQL4,MQL5 / C# / Asp.Net / Windows Forms / SQL Server / Access / VBA / HTML / CSS / Javascript / classic C/C++.


I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Un'altra estate from the album L'immobile disegno.



"Short code, good code"

Comments and Discussions

 
Question64 bit system Pin
bataj221-Jun-20 22:25
Memberbataj221-Jun-20 22:25 
QuestionPrints Blank Pages Pin
trantrum13-Apr-16 0:50
professionaltrantrum13-Apr-16 0:50 
It's showing the pages but will not print them. Prints blank pages.

A normal printDialog prints all the pages okay.

A normal printPreviewDialog also will not print the pages.

I discovered when I click tsBtnPageSettings_Click() the page goes blank (no pages showing).

Any ideas please?
GeneralMy vote of 5 Pin
NASSIM LOUCHANI14-Dec-15 0:56
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
Anders202024-Apr-12 7:55
MemberAnders202024-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 
GeneralVB.net Version Pin
kumika25-Aug-10 6:28
Memberkumika25-Aug-10 6:28 
GeneralRe: VB.net 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
professionalVincenzo 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
Mubi | www.mrmubi.com15-May-09 3:18
professionalMubi | www.mrmubi.com15-May-09 3:18 
GeneralRe: Hey Pin
Vincenzo Rossi16-May-09 3:10
professionalVincenzo 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
professionalVincenzo 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
professionalVincenzo 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.