我从WinForms迁移到了WPF。在我的旧WinForms应用程序中,获取PaperSizes的名称非常出色( PrinterSettings.PaperSize.PaperName )。如何获得WPF中的文件列表?
请注意,在WPF PrintCapabilities.PageMediaSizeCapability.PageMediaSizeName 是一个完全不同的事情(并在我的情况下给出不好的结果)。我的意思是当用户可以在他们的打印机的驱动程序首选项中创建独特的纸张尺寸时,他们可以为其添加名称(例如“ John的海报巨大”)。我想得到那份名单。我提到的,在WinForms中它完美运行。在WPF中, PageMediaSize 无法看到这些名称。
如果有任何解决方案,我也想知道如何将打印机的纸张尺寸更改为选择的纸张尺寸。
以下是我如何获得文件的旧WinForms版本。这个需要翻译成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];
}
}
答案 0 :(得分:1)
我认为您的代码没有任何问题。
PrinterSettings Class应该像winforms一样工作。
此代码在WPF应用程序中运行良好。
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}");
}
}
}
}
}