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 -"
, new Font("Comic sans MS", 10)
, Brushes.Red
, TextPosition.HBottomLeft
, 0
, 0
)
);