Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get paper size from printer variable?

I have 3 printers connected to my PC and on my win form I select the print by name, in code, (I don't use PrintDialog). Every printer has different paper size (small, medium and large). Is any way I can get the paper size for current printer. I try

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 variable e from my delegate parameter 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 show me the same size no matter which printer I use. Thanks in advance.

like image 605
Greg Avatar asked Dec 12 '13 11:12

Greg


1 Answers

Why don't just use specialy designed classes from System.Drawing.Printing namespace? Or I've missed something in the question?

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;
like image 165
Dmitry Bychenko Avatar answered Oct 12 '22 23:10

Dmitry Bychenko