We have already discussed PageSetupDialog, which allows us to adjust page settings. This is all taken care of by the dialog internally. But what if we need a custom page setup dialog? Sometimes we won't want to use the default dialogs provided by Windows. For example, suppose we want to change the text of the dialog or don't want the user to have page selection or anything else that is not available on the default Windows dialogs.
The System.Drawing.Printing namespace also defines functionality to manage page settings programmatically.
The PageSettings Class
Page settings are the properties of a page that are being used when a page is printer, including colors, page margins, paper size, page bounds, and page resolution.
The PageSettings class represents page settings in the .NET Framework library. This class provides members to specify page settings. It is used by the Printdocument.DefultPageSettings property to specify the page settings of a PrintDocument object. Table 11.10 describes the properties of the PageSettings class.
Besides the properties described in Table 11.10, the PageSettings class provides three methods: Clone, CopyToHdevmode, and SetHdevmode. The Clone method simply creates a copy of the PageSettings object. CopyToHdevmode copies relevant information from the PageSettings object from the specified DEVMODE structure, and SetHdevmode copies relevant information to the PageSettings object from the specified DEVMODE structure. The DEVMODE structure is used by WIN32 programmers.
Page Margins
The Margins class represents a page margin in the .NET Framework library. It allows you to get the current page margin settings and set new margin settings. This class has four properties - Left, Right, Top, and Bottom which represent the left, right, top, and bottom margins, respectively, in hundredths of an inch. This class is used by the Margins property of the PageSettings class. We will use this class and its members in our examples.
TABLE 11.10: PageSettings properties
Property
|
Description
|
Bounds
|
Returns the size of the page.
|
Color
|
Indicates whether the page should be printed in color. Both get and set. The default is determined by the printer.
|
Landscape
|
Indicates whether the page is printed in landscape or portrait orientation. Both get and set. The default is determined by the printer.
|
Margins
|
Identifies the page margins. Both get and set.
|
PaperSize
|
Identifies the paper size. Both get and set.
|
PaperSource
|
Identifies the paper source (a printer tray). Both get and set.
|
PrinterResolution
|
Identifies the printer resolution for the page. Both get and set.
|
PrinterSettings
|
Identifies the printer settings associated with the page. Both get and set.
|
Creating a Custom Paper Size
As mentioned earlier, the PaperSize class specifies the size and type of paper. You can create you own custom paper sizes. For example, Listing 11.37 creates a custom paper size with a height of 200 and width of 100.
LISTING 11.37: Creating a custom paper size
'Create a custom paper size and add it to the list
Dim customPaperSize As New PaperSize()
customPaperSize.PaperName = "Custom Size"
customPaperSize.Width = 100
The PaperKind Enumeration
The PaperKind enumeration, as we saw earlier, is used by the Kind property to specify standard paper sizes. This enumeration has over 100 members. Among them are A2, A3, A3Extra, A3ExtraTransverse, A3Rotated, A3Transverse, A4, A5, A6, Custom, DCEnvelope, Executive, InviteEnvelope, ItalyEnvelope, JapanesePostCard, Ledger, Legal, LegalExtra, Letter, LetterExtra, LetterSmall, Standard10x11 (10x14, 10x17, 12x11, 15x11, 9x11), Statement, and Tabloid.
The PaperSourceKind Enumeration
The PaperSourceKind enumeration represents standard paper sources. Table 11.11 describes the members of the PaperSourceKind enumeration.
TABLE 11.11: PaperSoruceKind members
Member
|
Description
|
AutomaticFeed
|
Automatically fed paper
|
Cassette
|
A paper cassette
|
Custom
|
A printer-specific paper source
|
Envelope
|
An Envelope
|
FormSource
|
The printer's default input bin
|
LargeCapacity
|
The printer's large-capacity bin
|
LargeFormat
|
Large-format paper
|
Lower
|
The lower bin of a printer
|
Manual
|
Manually fed paper
|
ManualFeed
|
Manually fed envelope
|
Middle
|
The middle bin of a printer
|
SmallFormat
|
Small-format paper
|
TractorFeed
|
A tractor feed
|
Upper
|
The upper bin of a printer
|
Page Settings in Action
Now let's create an application that will allow us to get and set page settings. In this application we will create a custom dialog.
We start by creating a new Windows application in VS .NET. We add some controls to the form, with the result shown in Figure 11.22. The Available Printers combo box displays all available printers. The Size and Source combo boxes display paper sizes and sources, respectively. The Paper Orientation section indicates whether paper is oriented in landscape mode or portrait mode. The Paper Margins text boxes obviously represent left, right, top and bottom margins. The Bounds property is represented by the Bounds (Rectangle) text box. The Color Printing check box indicates whether the printer supports color printing. The Set Properties button allows us to enter new values in the controls.
The form's load event (see Listing 11.38) loads all the required PageSettings-related settings using the LoadPrinters, LoadPaperSizes, LoadPaperSources, and ReadOtherSettings methods.
FIGURE 11.22: The custom page settings dialog
LISTING 11.38: The form's load event handler
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'Load all available printers
LoadPrinters()
'Load paper sizes
LoadPaperSizes()
'Load paper sources
LoadPaperSources()
'Load other settings
ReadOtherSettings()
End Sub
The LoadPrinters, LoadPaperSizes, LoadPaperSoruces, and ReadOtherSettings methods are used to load printers, paper sizes, paper sources, and other properties, respectively. The LoadPrinters method is given in Listing 11.39. We simply read the InstalledPrinters property of PrinterSettings and add printer to the printersList combo box.
LISTING 11.39: Loading printers
Private Sub LoadPrinters()
'Load all avaiable printers
For Each printer As [String] In PrinterSettings.InstalledPrinters
printerList.Items.Add(printer.ToString())
Next
printersList.[Select](0, 1)
End Sub
The LoadPaperSizes method (see Listing 11.40), loads all available paper sizes to the combo box. We read the PaperSizes property of PrinterSettings and add the paper type to the combo box. Then we create a custom paper size and add this to the combo box as well. This example will give you an idea of how to create your own custom paper sizes.
LISTING 11.40: Loading paper sizes
Private Sub LoadPaperSizes()
PaperSizeCombo.DisplayMember = "PaperName"
Dim settings As PrinterSettings = New PrinterSettings()
'Get all paper sizes and add them to the combo box list
Dim size As PaperSize
For Each size In settings.PaperSizes
PaperSizeCombo.Items.Add(size.Kind.ToString())
'You can even read the paper name and all PaperSize properties by uncommenting these
two lines
'PaperSizeCombo.Items.Add
'(size.PaperName.ToString());
'PaperSizeCombo.Items.Add (size.ToString());
Next
'Create a custom paper size and add it to the list
PaperSize customPaperSize =
New PaperSize("Custom Size", 50, 100)
'You can also change properties
customPaperSize.PaperName = ""New Custo, Size"
customPaperSize.Height = 200
customPaperSize.width = 100
'Don't assign the Kind property. It's read-only.
'customPaperSize.Kind = PaperKind.A4;
'Add custom size
PaperSizeCombo.Items.Add(customPaperSize)
End Sub
The LoadPaperSources methods (see Listing 11.41), reads all available paper sources and adds them to the PaperSourceCombo combobox.
We use the PaperSoruces property of PrinterSettings to read the paper sources.
LISTING 11.41: Loading paper sources
Private Sub LoadPaperSources()
Dim settings As New PrinterSettings()
PaperSourceCombo.DisplayMember = "SourceName"
'Add all paper sources to the combo box
For Each source As PaperSource In settings.PaperSources
'You can even add Kind and SourceName
'by uncommenting the following two lines:
'PaperSourceCombo.Items.Add
'(Source.Kind.ToString());
'PaperSourceCombo.Items.Add
'(source.SourceName.ToString());
PaperSourcesCombo.Items.Add(source.ToString())
Next
End Sub
The last method, ReadOtherSettnigs, reads other properties of a printer, such as whether it supports color,
margins, and bounds. Listing 11.42 shows the ReadOtherSettings method.
LISTING 11.42: Loading other properties of a printer
Private Sub ReadOtherSettngs()
'Set other default properties
Dim settings As New PrinterSettings()
Dim pgSettings As PageSettings = settings.DefaultPageSettings
'Color printing
If pgSettnigs.Color Then
ColorPrintingBox.Checked = True
Else
ColorPrintingBox.Checked = False
End If
'Page margins
leftMarginBox.Text = pgSettings.Bound.Left.ToString()
rightMarginBox.Text = pgSettings.Bound.Right.ToString()
topMarginBox.Text = pgSettings.Bound.Top.ToString()
bottomMarginBox.Text = pgSettings.Bound.Bottom.ToString()
'Landscape or portrait
If pgSettings.Landscape Then
landscapeButton.Checked = True
Else
portraitButton.Checked = ture
End If
'Bounds
boundsTextBox.Text = pgSettings.Bounds.ToString()
End Sub
Note : Remember that you need to add a reference to the System.Drawing.Printing namespace to your application whenever you
use classes from this namespace.
FIGURE 11.23: The PageSetupDialog sample in action
Now if we run the application, its form looks like Figure 11.23. Each of the Windows controls displays its intended
property.Finally, we want to save settings through the Set Properties button click and write code for a Cancel button.
On the Set Properties button click, we set the properties using PrinterSettings. Make sure printer is available in the
Available Printers combo box. The Cancel button simply closes the dialog.The code for the Set Properties and Cancel button click event handlers is given in Listing 11.43, in which we
set the page settings,
color, and landscape properties of a page.
LISTING 11.43: Saving paper settings
Private Sub SetPropertiesBtn_click(ByVal sender As Object, ByVal e As System.EventArge)
'Set other default properties
Dim setting As New PrinterSettings()
Dim pgSettings As PageSettings = settings.DefaultPageSettings
'Color printing?
If ColorPrintingBox.Checked Then
pgSettings.Color = True
Else
pgSettings.Color = False
End If
'Landscape or portrait?
If landscapeButton.Checked Then
pgSettings.Landscape = True
Else
pgSettings.Landscape = False
End If
End Sub
Private Sub CancelBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
The preceding discussion should enable you to customize page settings in the way that you want, instead of using the standard page settings dialog provided in the PageSettingsDialog class.
Note: Even though the printing facility defined in the System.Drawing.Printing namespace allows developers to customize the standard Windows dialogs, I recommend that you use the standard Windows dialogs unless you can't live without customizing them.
The PrintRange Enumeration
The PrintRange enumeration is used to specify the part of a document to print. This enumeration is used by the PrinterSettings and PrintDialog classes. Table 11.12 describes the members of the PrintRange enumeration.
You can use the PrintRange property of the PrinterSettings object to set the print range. Here's an example of code that does this:
PrinterSettings.PrintRange = PrintRange.SomePages;
TABLE 11.12: PrintRange members
Member
|
Description
|
AllPages
|
All pages are printed.
|
Selection
|
The selected pages are printed.
|
SomePages
|
The pages between FromPage and ToPage are printed.
|
Conclusion
Hope the article would have helped you in understanding Customizing Page Settings in GDI+. Read other articles on GDI+ on the website.