1
votes

I created a sheet to print with text and an image ... The only problem is that if I start the print interface the first time, the image does not appear in the print preview ... It can be corrected such that in the preview I can see the image?

MainPage.xaml:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button x:Name="btnPrint" Click="btnPrint_Click" Content="Print" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
</Grid>

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
    PrintManager printmgr = PrintManager.GetForCurrentView();
    PrintDocument printDoc = null;
    PrintTask task = null;

    public MainPage()
    {
        this.InitializeComponent();
        printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
    }
    private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
    {
        var deferral = args.Request.GetDeferral();
        task = args.Request.CreatePrintTask("Print", OnPrintTaskSourceRequrested);
        PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options);
        deferral.Complete();
    }

    private async void OnPrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
    {
        var def = args.GetDeferral();
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
          () =>
          {
              args.SetSource(printDoc?.DocumentSource);
          });
        def.Complete();
    }

    private void PrintDic_AddPages(object sender, AddPagesEventArgs e)
    {
        printDoc.AddPage(new PageToPrint());
        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);
    }
    private void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        Page page = new PageToPrint();
        Grid printableArea = (Grid)page.FindName("PrintArea");
        printDoc.SetPreviewPage(e.PageNumber, printableArea);
    }

    private async void btnPrint_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();
    }
}

PageToPrint.xaml:

<ScrollViewer>
    <Grid x:Name="PrintArea">
        <TextBlock Text="This is the text" FontSize="80"/>
        <Image Source="Image/4.JPG"/>
    </Grid>
</ScrollViewer>

Thanks for help!

1
This article might help you xamlbrewer.wordpress.com/2016/10/25/…Pratyay
I had already seen that but I could not adapt it... Is it possible to have an example?White55

1 Answers

0
votes

It seems the image isn't initialized or loaded completely when the print preview window displays. You can try the following way to see make the image display in the Preview window.

Firstly, add a Canvas control in the MainPage.xaml and make its Opacity="0" so that we can not see it in the MainPage,

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Canvas Name="MyCanvas" Opacity="0"/>
    <Button x:Name="btnPrint" Click="btnPrint_Click" Content="Print" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
</Grid>

Then, add a PreparePrintContent method and call it before the PrintManager.ShowPrintUIAsync in the btnPrint_Click event, the code will be like this,

private async void btnPrint_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;

    PreparePrintContent(new PageToPrint());

    bool showPrint = await PrintManager.ShowPrintUIAsync();
}

private void PreparePrintContent(Page pageToPrint)
{
    var canvas=(Canvas)this.FindName("MyCanvas");
    canvas.Children.Clear();
    canvas.Children.Add(pageToPrint);
}