How do I print WebView content C# UWP Win 10

后端 未结 1 1986
礼貌的吻别
礼貌的吻别 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.

    <Page.BottomAppBar>
        <CommandBar>
            <AppBarButton
                x:Name="appbar_Printer"
                Click="appbar_Printer_Click"
                Label="printer" />
        </CommandBar>
    </Page.BottomAppBar>
    
    <Grid x:Name="PrintArea" Background="White">
    
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="995" />
            <ColumnDefinition Width="300" />
            <ColumnDefinition  Width="50"/>
    
        </Grid.ColumnDefinitions>
    
        <WebView Grid.Column="0" x:Name="MyWebView" Source="http://www.stackoverflow.com" HorizontalAlignment="Right" />
        <Rectangle Grid.Column="1" x:Name="MyWebViewRectangle" Fill="Red" />
        <Button Grid.Column="2" Content="Print"  HorizontalAlignment="Center"/>
    
    </Grid>
    

    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)
提交回复
热议问题