C# - 获取打印机当前选定的纸张尺寸

C# - getting current selected paper size of a printer

我正在以编程方式打印文档。为此,我需要获取当前选择的 PaperSize 打印机。

我搜索并找到了这段代码,它获取所有打印机的所有纸张尺寸,

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}");
    }
}

但是,我需要打印机当前选择的纸张尺寸。

您可以在上面的截图中看到当前选择的纸张尺寸是A4。我想知道,是否有任何方法可以使用任何 C# 代码以编程方式获取此纸张大小?

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