How do I print WebView content C# UWP Win 10

后端 未结 1 1988
礼貌的吻别
礼貌的吻别 2020-12-30 16:02

I\'ve searched how to print a simple WebView as :

 

        
1条回答
  •  粉色の甜心
    2020-12-30 16:15

    For your requment, I simplified the PrintHelper of official sample and created a simple sample to print WebView via use WebViewBrush.

    
        
            
        
    
    
    
    
        
            
            
            
    
        
    
        
        
        

    When the WebView load completed, you could add the MyWebViewRectangle that fill with WebViewBrush to printDoc.

    private async void appbar_Printer_Click(object sender, RoutedEventArgs e)
    {
        if (printDoc != null)
        {
            printDoc.GetPreviewPage -= OnGetPreviewPage;
            printDoc.Paginate -= PrintDic_Paginate;
            printDoc.AddPages -= PrintDic_AddPages;
        }
        this.printDoc = new PrintDocument();
        printDoc.GetPreviewPage += OnGetPreviewPage;
        printDoc.Paginate += PrintDic_Paginate;
        printDoc.AddPages += PrintDic_AddPages;
        bool showPrint = await PrintManager.ShowPrintUIAsync();
    }
    private void PrintDic_AddPages(object sender, AddPagesEventArgs e)
    {
        Rectangle page = (Rectangle)this.FindName("MyWebViewRectangle");
        printDoc.AddPage(page);
        printDoc.AddPagesComplete();
    }
    private void PrintDic_Paginate(object sender, PaginateEventArgs e)
    {
        PrintTaskOptions opt = task.Options;
        PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(e.PrintTaskOptions);
    
        printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
    }
    

    I have uploaded the code sample to github that you could refer to.

    0 讨论(0)
提交回复
热议问题