Search code examples
c#.netprintingprinters

C# - getting current selected paper size of a printer


I am working with printing the documents programmatically. For this purpose, I need to get the currently selected PaperSize of a Printer.

I searched and found this code which gets all paper sizes of all printers,

var printDoc = new PrintDocument();

foreach (string printer in PrinterSettings.InstalledPrinters)
{
    Console.WriteLine(printer);
    Console.WriteLine("**************************");

    printDoc.PrinterSettings.PrinterName = printer;

    foreach (PaperSize paperSize in printDoc.PrinterSettings.PaperSizes)
    {
        Console.WriteLine($"PaperName:{paperSize.PaperName}, PaperSize: {paperSize.Height},{paperSize.Width}");
    }
}

But, I need currently selected paper size of a printer.

enter image description here

You can see in above screenshot the currently selected Paper Size is A4. I wants to know, is there any way to get this paper size using any C# code programmatically?


Solution

  • There is PrinterSettings.DefaultPageSettings which gives you the default page settings for a given printer. Given a certain PageSettings object, you can retrieve the specific papersize.