简体   繁体   English

从UWA打印多页

[英]Print Multiple Pages From a UWA

I have about 8 records that I want to print in one batch, each on a separate page. 我要批量打印约8条记录,每条记录都在单独的页面上。 However, the UWP sample for this uses over 600 lines of code to accomplish it. 但是,为此的UWP示例使用了600多行代码来完成此操作。 It seems to me that it has to be much, much easier than that. 在我看来,它必须比这简单得多。 I thought all we'd have to do is add each page to the PrintDocument and send the print job. 我以为我们要做的就是将每页添加到PrintDocument并发送打印作业。 Apparently not. 显然不是。 I'm using this: 我正在使用这个:

async void Print()
{
    var printDocument = new PrintDocument();
    var printDocumentSource = printDocument.DocumentSource;
    var printMan = PrintManager.GetForCurrentView();
    printMan.PrintTaskRequested += PrintTaskRequested;

    var pages = new List<Page>();

    foreach (var item in items)
    {
        (//Set up variables)

        var printPage = new PageToPrint() { //Set properties };
        printPage.Set_Up();  //Set up fields

        pages.Add(printPage);
    }

    printDocument.SetPreviewPage(1, page);
    printDocument.SetPreviewPageCount(pages.Count, PreviewPageCountType.Final);

    foreach (var page in pages)
    {
        printDocument.AddPage(page);
    }

    printDocument.AddPagesComplete();
    await PrintManager.ShowPrintUIAsync();
}

void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
    PrintTask printTask = null;
    printTask = e.Request.CreatePrintTask("Kimble Print Job", sourceRequested =>
    {
        printTask.Completed += PrintTask_Completed;
        sourceRequested.SetSource(printDocumentSource);
    });
}

private async void PrintTask_Completed(PrintTask sender, PrintTaskCompletedEventArgs args)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        PrintManager printMan = PrintManager.GetForCurrentView();
        printMan.PrintTaskRequested -= PrintTaskRequested;
    });
}

However, it won't generate the print preview. 但是,它不会生成打印预览。 It just sits there spinning and spinning, and if I hit "print" it doesn't succeed (PDF can't open, job never gets to a physical printer.) 它只是坐在那里旋转,如果我按“打印”,它就不会成功(PDF无法打开,作业永远不会到达物理打印机。)

I was hoping printing would be at least reasonably easy with the PrintDocument , and I still think it looks like it should be. 我希望使用PrintDocument至少可以相当轻松地进行打印,但我仍然认为它应该看起来应该如此。 Am I just missing it here, or does it really take 600+ lines of code to dispatch a simple print job? 我只是在这里想念它吗,还是真的需要600多行代码来分配一个简单的打印作业?

However, it won't generate the print preview. 但是,它不会生成打印预览。

This is because the setPreview method printDocument.SetPreviewPage(1, page); 这是因为setPreview方法printDocument.SetPreviewPage(1, page); must be put in printDocument.GetPreviewPage event handle. 必须放在printDocument.GetPreviewPage事件句柄中。 So you should register the event handle firstly. 因此,您应该首先注册事件句柄。 Same with printDocument.AddPages event handle.You messed up the event handle register and callback function all in one.Here I do a little change of your code and I tested it works well. printDocument.AddPages事件句柄相同。 printDocument.AddPages将事件句柄寄存器和回调函数全部弄乱了。在这里,我对代码做了一些改动,并测试了它的工作原理。

protected PrintDocument printDocument;
protected IPrintDocumentSource printDocumentSource;
List<Page> pages = new List<Page>();
Page printPage = new PageToPrint();
public MainPage()
{
    this.InitializeComponent();
    RegisterForPrinting();
}

private async void BtnPrint_Click(object sender, RoutedEventArgs e)
{
    await PrintManager.ShowPrintUIAsync();       
}

public void RegisterForPrinting()
{
    printDocument = new PrintDocument();
    printDocumentSource = printDocument.DocumentSource;
    pages.Add(printPage);
    printDocument.GetPreviewPage += GetPrintPreviewPage;
    printDocument.AddPages += AddPrintPages;
    PrintManager printMan = PrintManager.GetForCurrentView();
    printMan.PrintTaskRequested += PrintTaskRequested;
}
private void AddPrintPages(object sender, AddPagesEventArgs e)
{
    foreach (var page in pages)
    {
        printDocument.AddPage(page);
    }
    printDocument.AddPagesComplete();
}
private void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
{
    printDocument.SetPreviewPage(1, printPage);
    printDocument.SetPreviewPageCount(pages.Count, PreviewPageCountType.Final);
}

void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
    PrintTask printTask = null;
    printTask = e.Request.CreatePrintTask("Kimble Print Job", sourceRequested =>
    {
        printTask.Completed += PrintTask_Completed;
        sourceRequested.SetSource(printDocumentSource);
    });
}

private async void PrintTask_Completed(PrintTask sender, PrintTaskCompletedEventArgs args)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        PrintManager printMan = PrintManager.GetForCurrentView();
        printMan.PrintTaskRequested -= PrintTaskRequested;
    });
} 

Although you may not need all the code of the sample, but I recommend you to follow the official sample structure and build a PrintHelper class. 尽管您可能不需要示例的所有代码,但是我建议您遵循正式的示例结构并构建PrintHelper类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM