How to get predefined paper size by PaperKind

A subset of predefined values can be had by iterating over a PrinterSettings.PaperSizes collection.

Our application has the user select a printer, providing us with a PrinterSettings object. Contained within PrinterSettings is a list of PaperSize's supported by the printer - not everything (note that the XPS Document Driver (win7) supports all sizes).

In our case this subset of supported sizes is all we need. A user specified PaperKind is passed to our printing code, and it goes through our PrinterSettings object until it either finds the user's selection or gives up and uses a default.

In the example below you can see that the PaperSize objects are correctly filled.

PrinterSettings settings = new PrinterSettings();
foreach (PaperSize size in settings.PaperSizes)
    Debug.WriteLine(size);

It's only a subset, but maybe that's also enough for you. the printing APIs in .NET are really unclear and msdn isn't really much help... Hopefully it puts you on the right track!


A LINQ way to achieve your goal is something like this:

PrinterSettings printerSettings = new PrinterSettings();
IQueryable<PaperSize> paperSizes = printerSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
PaperSize a4rotated = paperSizes.Where(paperSize => paperSize.Kind == PaperKind.A4Rotated).FirstOrDefault();

Good luck!

Tags:

C#

.Net

Printing