Click here to Skip to main content
Email Password   helpLost your password?

Introduction

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

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.

AdditionalText Class

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

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
     )
);
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionHow do we use your print preview
noka03
20:06 2 Nov '09  
Hi i think this is a great example but how do i use it?

i have downloaded your demo and src but cant seem to print what is in a textbox

do you have a sample of code that you could share for this...

thanks for you help..
-Noka
AnswerRe: How do we use your print preview
Vincenzo Rossi
2:05 3 Nov '09  
Noka,
maybe you still haven't experience in windows forms printing.
Developers must "paint" what is to be printed using the standard PrintDocument class and using the painting capabilities of the gdi, inside a custom handler for PrintPage event.
After that, they can replace the ugly standard interface with the EnhancedPrintPreviewDialog.

References:
You can see microsoft documentation with code examples about PrintDocument.

About textbox and other controls printing, you may use the DrawToBitmap method inside your PrintPage event handler.

There are many useful articles in CodeProject about automatic printing of DataGridView or DataTable an so on. You may use that code if it's right to your needs, avoiding to worry about painting with PrintDocument, and than use the EnhancedPrintPreviewDialog to surround the produced document with a nice UI.

I hope this may help you
Regards
Vin
GeneralBUG: The margin value decreases every time PageSetupDialog is displayed
Gwannoes
0:44 27 May '09  
Nice tool!

But as can be read *here*, there is a bug in the .NET framework with the PageSetupDialog margin settings when using the metric units system.
In .NET 2.0 it can be fixed by setting the PageSetupDialog.EnableMetric property to true in the tsBtnPageSettings event handler in EnhancedPrintPreviewDialog.cs
private void tsBtnPageSettings_Click(object sender, EventArgs e)
{
PageSetupDialog.Document = this.mDocument;
PageSetupDialog.EnableMetric = true;
if (PageSetupDialog.ShowDialog() == DialogResult.OK)
{
printPreviewControl1.InvalidatePreview();
}
}

GeneralHey
ww.mubshir.com
4:18 15 May '09  
Did it use any library in background.

Mubshir Raza Ali
^^^^^^^^^^^^^^^^^^^^
http://www.mubshir.com

GeneralRe: Hey
Vincenzo Rossi
4:10 16 May '09  
No, what is your problem?
GeneralPerfetto
Jamal Alqabandi
13:01 30 Apr '09  
Nice one.

Good luck

Jamal
Kuwait
GeneralPerfect
Fred#
9:01 26 Apr '09  
I've just started learning C#. I could not believe the awful print preview window, I thought I was missing something important. Thank you so much for this code, it's perfect!
GeneralRe: Perfect
Vincenzo Rossi
11:46 26 Apr '09  
Thank you Fred.
Vin
GeneralAwesome
VCSKicks
9:25 23 Apr '09  
Very useful and a clean implementation. 5/5

Visual C# Kicks - Free C#.NET articles, resources, and downloads at
http://www.vcskicks.com

GeneralRe: Awesome
Vincenzo Rossi
23:29 23 Apr '09  
Hi VC,
thank you for your comment
Vin


Last Updated 23 Apr 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010