Customizing Page Settings

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.

11.9.1 The PageSettings Class

Page settings are the properties of a page that are being used when a page is printed, including color, 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.DefaultPageSettings 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 to 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.

11.9.2 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 propertiesLeft, Right, Top, and Bottomwhich 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.

11.9.3 Creating a Custom Paper Size

As mentioned earlier, the PaperSize class specifies the size and type of paper. You can create your own custom paper sizes. For example, Listing 11.37 creates a custom paper size with a height of 200 and a width of 100.

Listing 11.37 Creating a custom paper size

// Create a custom paper size and add it to the list
PaperSize customPaperSize = new PaperSize();
customPaperSize.PaperName = "Custom Size";
customPaperSize.Height = 200;
customPaperSize.Width = 100;

11.9.4 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.

11.9.5 The PaperSourceKind Enumeration

The PaperSourceKind enumeration represents standard paper sources. Table 11.11 describes the members of the PaperSourceKind enumeration.

Table 11.11. PaperSourceKind 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

11.9.6 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.

Figure 11.22. The custom page settings dialog

graphics/11fig22.jpg

The form's load event (see Listing 11.38), loads all the required PageSettings-related settings using the LoadPrinters, LoadPaperSizes, LoadPaperSources, and ReadOtherSettings methods.

Listing 11.38 The form's load event handler

private void Form1_Load(object sender,
 System.EventArgs e)
{
 // Load all available printers
 LoadPrinters();
 // Load paper sizes
 LoadPaperSizes();
 // Load paper sources
 LoadPaperSources();
 // Load other settings
 ReadOtherSettings();
}

The LoadPrinters, LoadPaperSizes, LoadPaperSources, 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 printers to the printersList combo box.

Listing 11.39 Loading printers

private void LoadPrinters()
{
 // Load all available printers
 foreach(String printer in
 PrinterSettings.InstalledPrinters)
 {
 printersList.Items.Add(printer.ToString());
 }
 printersList.Select(0, 1);
}

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 void LoadPaperSizes()
{
 PaperSizeCombo.DisplayMember = "PaperName";
 PrinterSettings settings = new PrinterSettings();
 // Get all paper sizes and add them to the combo box list
 foreach(PaperSize 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());
 }
 // 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 Custom 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);
}

The LoadPaperSources method (see Listing 11.41), reads all available paper sources and adds them to the PaperSourceCombo combo box. We use the PaperSources property of PrinterSettings to read the paper sources.

Listing 11.41 Loading paper sources

private void LoadPaperSources()
{
 PrinterSettings settings = new PrinterSettings();
 PaperSourceCombo.DisplayMember="SourceName";
 // Add all paper sources to the combo box
 foreach(PaperSource source in settings.PaperSources)
 {
 PaperSourceCombo.Items.Add(source.ToString());
 // 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());
 }
}

The last method, ReadOtherSettings, 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 void ReadOtherSettings()
{
 // Set other default properties
 PrinterSettings settings = new PrinterSettings();
 PageSettings pgSettings =
 settings.DefaultPageSettings;
 // Color printing
 if(pgSettings.Color)
 ColorPrintingBox.Checked = true;
 else
 ColorPrintingBox.Checked = false;
 // Page margins
 leftMarginBox.Text =
 pgSettings.Bounds.Left.ToString();
 rightMarginBox.Text =
 pgSettings.Bounds.Right.ToString();
 topMarginBox.Text =
 pgSettings.Bounds.Top.ToString();
 bottomMarginBox.Text =
 pgSettings.Bounds.Bottom.ToString();
 // Landscape or portrait
 if(pgSettings.Landscape)
 landscapeButton.Checked = true;
 else
 portraitButton.Checked = true;
 // Bounds
 boundsTextBox.Text =
 pgSettings.Bounds.ToString();
}

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.

Now if we run the application, its form looks like Figure 11.23. Each of the Windows controls displays its intended property.

Figure 11.23. The PageSetupDialog sample in action

graphics/11fig23.jpg

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 a 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 void SetPropertiesBtn_Click(object sender,
 System.EventArgs e)
{
 // Set other default properties
 PrinterSettings settings = new PrinterSettings();
 PageSettings pgSettings =
 settings.DefaultPageSettings;
 // Color printing?
 if (ColorPrintingBox.Checked )
 pgSettings.Color = true;
 else
 pgSettings.Color = false;

 // Landscape or portrait?
 if(landscapeButton.Checked )
 pgSettings.Landscape = true;
 else
 pgSettings.Landscape = false;
}

private void CancelBtn_Click(object sender,
 System.EventArgs e)
{
 this.Close();
}

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 functionality 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.

 

11.9.7 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.

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics

Transformation

Printing

Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET



GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
EAN: N/A
Year: 2003
Pages: 145

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net