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!