我正在开发一个UWP应用程序,在其中我需要打印,可以打印在只有3个大小。为了首先实现这一点,我想我需要得到机器上所有已安装打印机的列表,以及它们所支持的纸张大小。
我做了大量的研究,发现我可以使用PrintHelper类,它是在UWP样本母版的打印样本中提供的。
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);
}我也调试了这个方法,在页面描述中,我得到了纸张的宽度和高度,但我不能简单地假设它是正确的,我需要所有打印机的纸张大小,而不是特定的打印机,所以这不可能是解决方案。
提前感谢您的帮助!
发布于 2018-03-16 12:05:34
经过大量的研究和调试,我发现我的问题本身就有答案。在我问题中提到的方法中,有两行
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.Dpix 3. 3.Imageable Rect 4.页面大小
从这些事情,我们将能够知道什么纸张大小打印机支持,然后继续根据这一点。
对于问题的另一半:从本地计算机获取已安装打印机列表,答案仅为一行。
private DeviceInformationCollection deviceCollection;
deviceCollection = await DeviceInformation.FindAllAsync("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");现在我可以在CPCL模拟模式下从我的标签打印机打印。
编码愉快。
https://stackoverflow.com/questions/49150964
复制相似问题