How to get paper size from printer variable?

I have 3 printers connected to my computer, and in my win form I select print by name, in code (I do not use PrintDialog). Each printer has a different paper size (small, medium and large). Is there a way to get the paper size for the current printer. I'm trying to

Console.WriteLine(e.PageSettings.Bounds.ToString());
Console.WriteLine(e.PageSettings.PaperSize.ToString());
Console.WriteLine(e.Graphics.VisibleClipBounds.ToString());
Console.WriteLine(e.Graphics.ClipBounds.ToString());

I get the e variable from my delegate options method:

// When I launch my printer (in class constructor):
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintLabel);
//.... other code



//My delegate method:
   private void PrintLabel(object o, PrintPageEventArgs e)
   {
      //This is my code from above:
        Console.WriteLine(e.PageSettings.Bounds.ToString());
      // other code
   }

But it shows me the same size, no matter which printer I use. Thanks in advance.

+4
source share
1 answer

Why not just use custom-designed classes from the namespace System.Drawing.Printing? Or am I missing something on this issue?

using System.Drawing.Printing;

public static PageSettings GetPrinterPageInfo(String printerName) {
  PrinterSettings settings;

  // If printer name is not set, look for default printer
  if (String.IsNullOrEmpty(printerName)) {
    foreach (var printer in PrinterSettings.InstalledPrinters) {
      settings = new PrinterSettings();

      settings.PrinterName = printer.ToString();

      if (settings.IsDefaultPrinter)
        return settings.DefaultPageSettings;
    }

    return null; // <- No default printer  
  }

  // printer by its name 
  settings = new PrinterSettings();

  settings.PrinterName = printerName;

  return settings.DefaultPageSettings;
}

// Default printer default page info
public static PageSettings GetPrinterPageInfo() {
  return GetPrinterPageInfo(null);
}

...

// Default printer default page
PageSettings page = GetPrinterPageInfo(); 
// Or default page of some other printer given by its name
// PageSettings page = GetPrinterPageInfo(MyPrinterName); 

RectangleF area = page.PrintableArea;
Rectangle bounds = page.Bounds;
Margins margins = page.Margins;
+6

All Articles