PaperSize in C#


This article has been excerpted from book "Graphics Programming with GDI+".

Most printers can use papers of more than one size (height and width). The PaperSize class is used to read and set the paper size used by a printer.

The PaperSize class represents the size of paper used in printing. This class is used by PrinterSettings through its PaperSizes property to get and set the paper sizes for the printer.

TABLE 11.2: Other PrinterSettings properties


Property

Description

DefaultPageSettings

Returns the default page settings.

FromPage

Returns the page number of the first page to print. Both get and set.

IsDefaultPrinter

Returns true if the current printer is the default printer.

LandScapeAngle

Returns the angle, in degrees, by which the portrait orientation is rotated to, produces the landscape orientation. Valid rotation values are 90 and 27 degrees. If landscape is not supported, the only valid rotation value is 0 degrees.

MaximumPage

Returns the maximum value of FromPage or ToPage that can be selected in a print dialog. Both get and set.

MinimumPage

Returns the minimum value of FromPage to ToPage that can be selected in a print dialog. Both get and set.

PrintRange

Returns the page numbers that the user has specified to be printed. Both get and set.

PrintToFile

Returns a value indicating whether the printing output is sent to a file instead of a port. Both get and set.

ToPage

Returns the page number of the last page to print. Both get and set.

The PaperSize class has four properties: Height, Kind, PaperName, and Width.Height, Width and PaperName have both get and set access. The Height and Width properties are used to get and set the paper's height and width, respectively, in hundredths of an inch. The PaperName property is used to get and set the name of the type of paper, but it can be used only when the Kind property is set to Custom. The Kind property returns the type of paper.

We can construct custom paper size using the PaperSize clas. Listing 11.12 reads the PaperSize properties.

LISTING 11.12: Reading PaperSize properties


            PrinterSettings ps = new PrinterSettings();
            Console.WriteLine("Paper Sizes");

            foreach (PaperSize psize in ps.PaperSizes)
            {
                string str1 = psize.Kind.ToString();
                string str2 = psize.PaperName.ToString();
                string str3 = psize.Height.ToString();
                string str4 = psize.Width.ToString();
            }


The PaperSource Class

The PaperSource class specifies the paper tray from which the printer retrieves the paper for the current printing task. This class is used by PritnerSettings through its PaperSoruces property to get and set the paper source trays that a re available on the printer. The PaperSize class has two properties: Kind and SourceName. The Kind property returns an enumerated value for the paper source, and SoruceName returns the name of the paper source as a string.

Listing 11.13 reads all the paper sources and displays them in a message box.

LISTING 11.13: Reading paper sources


            PrinterSettings ps = new PrinterSettings();

            foreach (PaperSource p in ps.PaperSources)
            {
                MessageBox.Show(p.SourceName);
            }


The PrinterResolutionKind Enumeration

The PrinterResolutionKind enumeration specifies a printer resolution, as described in Table 11.3. This enumeration is used by the PrinterResolution, PrinterSettings, and PageSettings classes.

PrinterSettings Collection Classes

Besides the PrinterSetting class, the System.Drawing.Printing namespace provides three PrinterSettings collection classes. These collection classes provide members to count total items in a collection, and to add item to and remove items from a collection. The classes are:

  1. PrinterSettings.PaperSizeCollection. A printer may support different kinds of papers, including papers of different sizes. This class returns a collection including all paper sizes supported by the printer. PaperSizeCollection contains PaperSizes objects.
  2. PrinterSettings.PaperSourceCollection. A printer may support different paper sources (trays). This class represents a collection of paper sources (trays) provided by a printer. PaperSourceCollection is available via the PaperSoruces property and contains PaperSource objects.
  3. PrinterSettings.PrinterResolution.Collection. A printer may support different resolutions. This class represents a collection of resolutions supported by a printer. PrinterResolutionsCollection is accessible via the PrinterResoultions property and contains PrinterResoultion objects.

TABLE 11.3: PrinterResolutionKind members

Member

Description

Custom

Custom resolution

Draft

Draft-quality resolution

High

High resolution

Low

Low resolution

Medium

Medium resolution

All these collection classes provide Count and Item properties. The Count property returns the total number of items in a collection, and the Item property returns the item at the specified index. We will use these classes in our samples.

Conclusion

Hope the article would have helped you in understanding the PaperSize Class in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.