[英]How to get all installed printers list and their supported paper size in UWP app?
我正在开发一个UWP应用,我需要在其中进行打印,并且只能以3种尺寸进行打印。 为了实现这一点,我想我需要获取机器中所有已安装打印机的列表以及它们支持的纸张尺寸。
我进行了很多研究,发现可以使用UWP示例主机的打印示例中提供的PrintHelper类。
ShowPrintUIAsync()是显示已安装打印机下拉列表的方法,但是我无法通过调试找出如何获取所有打印机名称。
protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
{
// Clear the cache of preview pages
printPreviewPages.Clear();
// Clear the print canvas of preview pages
PrintCanvas.Children.Clear();
// This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
RichTextBlockOverflow lastRTBOOnPage;
// Get the PrintTaskOptions
PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
// Get the page description to deterimine how big the page is
PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
// We know there is at least one page to be printed. passing null as the first parameter to
// AddOnePrintPreviewPage tells the function to add the first page.
lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);
// We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
// page has extra content
//while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
//{
// lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
//}
if (PreviewPagesCreated != null)
{
PreviewPagesCreated.Invoke(printPreviewPages, null);
}
PrintDocument printDoc = (PrintDocument)sender;
// Report the number of preview pages created
printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
}
我也调试了这种方法,在页面描述中我得到了纸张的宽度和高度,但是我不能简单地认为它是正确的,并且我需要所有打印机的纸张尺寸而不是特定打印机的尺寸,因此这不是解决方案。
在此先感谢您的帮助!
经过大量的研究和调试,我发现问题本身就存在答案。 在我的问题中提到的方法中有这两行
PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
// Get the page description to deterimine how big the page is
PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
在页面描述中,您得到4件事:1. Dpix 2.Dpiy 3.Imageable Rect 4.页面大小
从这些事情中,我们将能够知道所支持的纸张尺寸打印机,然后根据此进行操作。
对于问题的另一半:从本地计算机获取已安装的打印机列表,答案仅一行
private DeviceInformationCollection deviceCollection;
deviceCollection = await DeviceInformation.FindAllAsync("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");
现在,我可以在CPCL仿真模式下从标签打印机进行打印。
快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.