I need to print a List through my UWP app. Since List can have N number of items so I need to show the pages in my print preview according to that and then print them all. I have tried few scenarios and looked at the sample of UWP Print app provided by Microsoft but in that they are using RichTextBlock for showing all the pages when the content is overflow. But I have a list so I would not know when my content is getting overflow. It just shows only first page in the print preview UI.
I have calculated the total pages also according to the size of A4 paper. Below is my C# code:
if (PrintManager.IsSupported())
{
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
_totalPages = (int)Math.Ceiling(wholeItemsListForPDF.Count / (double)15);
tempList = new ManifestPDFDataModel();
tempList = _manifestPDFDataModel;
printDoc.Paginate += PaginateManifestLabel;
for (int i = 1; i <= _totalPages; i++)
{
printDoc.GetPreviewPage += GetPreviewPageManifestLabel;
printDoc.AddPages += AddPagesManifestLabel;
if (i != _totalPages)
{
_manifestPDFDataModel.ItemsPDFList.RemoveRange(0, 15);
tempList.ItemsPDFList = _manifestPDFDataModel.ItemsPDFList;
}
}
printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequestedManifestLabel;
//printHelper = new PrintHelper(this);
//printHelper.RegisterForPrinting();
//// Initialize print content for this scenario
//printHelper.PreparePrintContent(new GenericManifestPDF(_manifestPDFDataModel));
try
{
// Show print UI
await PrintManager.ShowPrintUIAsync();
}
catch (Exception ex)
{
// Printing cannot proceed at this time
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, printing can' t proceed at this time.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
}
}
else
{
// Printing is not supported on this device
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing not supported",
Content = "\nSorry, printing is not supported on this device.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
}
All the Print related events:
public void PrintTaskRequestedManifestLabel(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequestedManifestLabel);
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompletedManifestLabel;
}
/// <summary>
/// PrintTaskSourceRequestedManifestLabel
/// </summary>
/// <param name="args">PrintTaskSourceRequestedArgs</param>
public void PrintTaskSourceRequestedManifestLabel(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
#region Print preview
/// <summary>
/// Pagination Manifest Label
/// </summary>
/// <param name="sender">pass sender object</param>
/// <param name="e">PaginateEventArgs</param>
public void PaginateManifestLabel(object sender, PaginateEventArgs e)
{
// As I only want to print one Rectangle, so I set the count to 1
printDoc.SetPreviewPageCount(_totalPages, PreviewPageCountType.Intermediate);
}
/// <summary>
/// Print Preview Page ManifestLabel SetPreviewPage
/// </summary>
/// <param name="sender">pass sender object</param>
/// <param name="e">GetPreviewPageEventArgs</param>
public void GetPreviewPageManifestLabel(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
printDoc.SetPreviewPage(e.PageNumber, new GenericManifestPDF(tempList));
}
#endregion
#region Add pages to send to the printer
/// <summary>
/// Add Pages ManifestLabel
/// </summary>
/// <param name="sender">pass sender object</param>
/// <param name="e">AddPagesEventArgs</param>
public void AddPagesManifestLabel(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(new GenericManifestPDF(tempList));
// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}
#endregion
#region Print task completed
/// <summary>
/// Print Task Completed ManifestLabel
/// </summary>
/// <param name="sender">PrintTask</param>
/// <param name="args">PrintTaskCompletedEventArgs</param>
public async void PrintTaskCompletedManifestLabel(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
});
}
else
{
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
//printDoc.Paginate -= Paginate;
//printDoc.GetPreviewPage -= GetPreviewPage;
//printDoc.AddPages -= AddPages;
}
}
#endregion
XAML Page code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30*"/>
<RowDefinition Height="70*"/>
</Grid.RowDefinitions>
<Grid BorderBrush="Black" BorderThickness="2" Margin="30">
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Manifest PDF Report" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="Function" Foreground="Black" Grid.Row="1" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="Counts" Grid.Row="2" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock x:Name="PrintedValue" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="RouteValue" Foreground="Black" Grid.Row="1" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="BatchIDValue" Grid.Row="2" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image x:Name="BarcodeImage" Margin="20,0,0,0" HorizontalAlignment="Center"/>
<TextBlock x:Name="BatchBarcodeText" Grid.Row="1" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
</Grid>
</Grid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Delivered By :" Foreground="Black" Margin="20,0,0,0" VerticalAlignment="Center"/>
<TextBlock Text="Batch Delivery Time :" Foreground="Black" Grid.Column="1" Margin="20,0,0,0" VerticalAlignment="Center"/>
</Grid>
</Grid>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="90*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock FontWeight="Bold" Text="BILL#" Foreground="Black" TextDecorations="Underline" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="1" Foreground="Black" TextDecorations="Underline" Text="Carrier" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="2" Foreground="Black" TextDecorations="Underline" Text="Package" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="3" Foreground="Black" TextDecorations="Underline" Text="Location" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="4" Text="ItemType" Foreground="Black" TextDecorations="Underline" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="5" Foreground="Black" TextDecorations="Underline" Text="Deliver To" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="6" Foreground="Black" TextDecorations="Underline" Text="Sender" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="7" Foreground="Black" TextDecorations="Underline" Text="Date" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="8" Foreground="Black" TextDecorations="Underline" Text="PO" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="9" Foreground="Black" TextDecorations="Underline" Text="Control" Margin="20,20,0,0"/>
</Grid>
<ListView Grid.Row="1" x:Name="PDFItemsList" IsItemClickEnabled="False">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<ContentControl Style="{StaticResource EmptyContentControlStyle}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Bill}" Grid.Column="0" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding CarrierName}" HorizontalAlignment="Right" Grid.Column="1" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/>
<TextBlock Text="{Binding PackageID}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="2" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Location}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="3" Margin="20,20,0,0"/>
<TextBlock Text="{Binding ItemType}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="4" Margin="20,20,0,0"/>
<TextBlock Text="{Binding DeliverTo}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="5" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Sender}" HorizontalAlignment="Right" Grid.Column="6" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding CreationDate}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="7" Margin="20,20,0,0"/>
<TextBlock Text="{Binding PONumber}" Foreground="Black" HorizontalAlignment="Right" Grid.Column="8" Margin="20,20,0,0"/>
<TextBlock Text="{Binding ControlNumber}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="9" Margin="20,20,0,0"/>
</Grid>
</ContentControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Grid>
Thanks in advance.