|
1 | | -# How to print specific pages in uwp datagrid? |
2 | | -This example describes how to print specific pages in uwp datagrid |
| 1 | +# How to print specific pages in UWP DataGrid |
| 2 | + |
| 3 | +This example describes how to print specific pages in [UWP DataGrid](https://www.syncfusion.com/uwp-ui-controls/datagrid) (SfDataGrid). |
| 4 | + |
| 5 | +You can print the specific pages by overriding [OnAddPrintPages](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.Grid.PrintManagerBase.html#Syncfusion_UI_Xaml_Grid_PrintManagerBase_OnAddPrintPages_Windows_UI_Xaml_Printing_AddPagesEventArgs_) method in `PrintManagerBase` class. |
| 6 | + |
| 7 | +``` csharp |
| 8 | +public class CustomPrintManager : GridPrintManager |
| 9 | +{ |
| 10 | + |
| 11 | + public CustomPrintManager(SfDataGrid grid) |
| 12 | + : base(grid) |
| 13 | + { |
| 14 | + |
| 15 | + } |
| 16 | + |
| 17 | + protected override void OnAddPrintPages(AddPagesEventArgs e) |
| 18 | + { |
| 19 | + IList<Windows.Graphics.Printing.PrintPageRange> customPageRanges = e.PrintTaskOptions.CustomPageRanges; |
| 20 | + |
| 21 | + int pageCount = this.PageDictionary.Count; |
| 22 | + |
| 23 | + // An empty CustomPageRanges means "All Pages" |
| 24 | + if (customPageRanges.Count == 0) |
| 25 | + { |
| 26 | + // Loop over all of the preview pages and add each one to be printed |
| 27 | + for (var i = 1; i <= pageCount; i++) |
| 28 | + { |
| 29 | + var printpageControl = CreatePage(i); |
| 30 | + PrintDocument.AddPage(printpageControl); |
| 31 | + } |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + // Print only the pages chosen by the user. |
| 36 | + // |
| 37 | + // The "Current page" option is a special case of "Custom set of pages". |
| 38 | + // In case the user selects the "Current page" option, the PrintDialog |
| 39 | + // will turn that into a CustomPageRanges containing the page that the user was looking at. |
| 40 | + // If the user typed in an indefinite range such as "6-", the LastPageNumber value |
| 41 | + // will be whatever this sample app last passed into the PrintDocument.SetPreviewPageCount API. |
| 42 | + foreach (PrintPageRange pageRange in customPageRanges) |
| 43 | + { |
| 44 | + // The user may type in a page number that is not present in the document. |
| 45 | + // In this case, we just ignore those pages, hence the checks |
| 46 | + // (pageRange.FirstPageNumber <= printPreviewPages.Count) and (i <= printPreviewPages.Count). |
| 47 | + // |
| 48 | + // If the user types the same page multiple times, it will be printed multiple times |
| 49 | + // (e.g 3-4;1;1 will print pages 3 and 4 followed by two copies of page 1) |
| 50 | + if (pageRange.FirstPageNumber <= pageCount) |
| 51 | + { |
| 52 | + for (int i = pageRange.FirstPageNumber; (i <= pageRange.LastPageNumber) && (i <= pageCount); i++) |
| 53 | + { |
| 54 | + // Subtract 1 because page numbers are 1-based, but our list is 0-based. |
| 55 | + var printpageControl = CreatePage(i); |
| 56 | + PrintDocument.AddPage(printpageControl); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + // Indicate that all of the print pages have been provided. |
| 62 | + PrintDocument.AddPagesComplete(); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +``` csharp |
| 68 | +dataGrid.PrintSettings.PrintManagerBase = new CustomPrintManager(this.dataGrid); |
| 69 | +dataGrid.PrintSettings.PrintManagerBase.Print(); |
| 70 | +``` |
0 commit comments