Tomato
Tomato

Reputation: 122

wpf c# get printer's available papersize names - like PrinterSettings.PaperSize.PaperName in WinForm

I moved from WinForms to WPF. In my old WinForms application to get the names of the PaperSizes works brilliant (PrinterSettings.PaperSize.PaperName). How can i get the list of those papersizes in WPF?

Please note that in WPF PrintCapabilities.PageMediaSizeCapability.PageMediaSizeName is a completely different thing (and give bad results in my case). I mean when the user can create unique paper size(s) in their printer's driver preferences and they can add a name for it (like "John's poster huge"). And i would like to get that name list. What i mentioned, in WinForms it works perfectly. In WPF, the PageMediaSize can't see these names.

If there is any solution for it i would also like to know how to change the the printer's paper size to the choosen one.

Here is the old WinForms version of how i get the papersizes. This one needs to translate to WPF....

public partial class Form1 : Form
{
    public PrintDocument printDoc;
    public string[] PrinterList;
    public string[] PaperSizeList;
    public PaperSize[] pkPaperSizeList; 

    public Form1()
    {
        InitializeComponent();

        printDoc = new PrintDocument();
        PrinterList = new string[PrinterSettings.InstalledPrinters.Count];

        GetPrinterNames();
        GetPaperSizes();
    }
    public void GetPrinterNames()
    {
        for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
        {
            PrinterList[i] = PrinterSettings.InstalledPrinters[i];
            comboBox_printerList.Items.Add(PrinterList[i]);
        }
        comboBox_printerList.SelectedIndex = 0;
        printDoc.PrinterSettings.PrinterName = PrinterList[0];
    }
    public void GetPaperSizes()
    {
        int j = 0;
        comboBox_paperList.Items.Clear();
        PaperSizeList = new string[printDoc.PrinterSettings.PaperSizes.Count];
        pkPaperSizeList = new PaperSize[printDoc.PrinterSettings.PaperSizes.Count];

        foreach (PaperSize paperSize in printDoc.PrinterSettings.PaperSizes)
        {
            PaperSizeList[j] = paperSize.PaperName;
            pkPaperSizeList[j] = paperSize;
            comboBox_paperList.Items.Add(PaperSizeList[j]);
            j++;
        }
        comboBox_paperList.SelectedIndex = 0;
    }

    private void comboBox_printerList_SelectedIndexChanged(object sender, EventArgs e)
    {
        printDoc.PrinterSettings.PrinterName = PrinterList[comboBox_printerList.SelectedIndex];
        GetPaperSizes();
    }

    private void comboBox_paperList_SelectedIndexChanged(object sender, EventArgs e)
    {
        printDoc.DefaultPageSettings.PaperSize = pkPaperSizeList[comboBox_paperList.SelectedIndex];
    }
}

Upvotes: 2

Views: 1663

Answers (1)

Vojta
Vojta

Reputation: 1120

I do not see any problem with your code.

PrinterSettings Class should work the same way like in winforms.

This code works just fine in WPF app.

using System;
using System.Drawing.Printing;
using System.Windows;

namespace _09_Printers
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            GetPrinterNamesAndPaperSize();
        }

        public void GetPrinterNamesAndPaperSize()
        {
            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}");
                }
            }
        }
    }
}

Upvotes: 3

Related Questions