private async void OnPrintClicked(object sender, RoutedEventArgs e) { // Create a new CanvasPrintDocument if (printDocument != null) { printDocument.Dispose(); } printDocument = new CanvasPrintDocument(); printDocument.PrintTaskOptionsChanged += PrintDocument_PrintTaskOptionsChanged; printDocument.Preview += PrintDocument_Preview; printDocument.Print += PrintDocument_Print; // Show the print UI, with the print manager connected to us var printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += PrintingExample_PrintTaskRequested; try { await PrintManager.ShowPrintUIAsync(); } finally { printManager.PrintTaskRequested -= PrintingExample_PrintTaskRequested; } }
private void PrintDocument_Print(CanvasPrintDocument sender, CanvasPrintEventArgs args) { var detailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(args.PrintTaskOptions); var pageRange = detailedOptions.Options["PageRange"].Value.ToString(); int pageCountToPrint; if (pageRange == "PrintFirstPage") { pageCountToPrint = 1; } else { pageCountToPrint = pageCount; } for (uint i = 1; i <= pageCountToPrint; ++i) { using (var ds = args.CreateDrawingSession()) { var imageableRect = args.PrintTaskOptions.GetPageDescription(i).ImageableRect; DrawPage(sender, ds, i, imageableRect); } } }
private async Task EnsureBitmapsLoaded(CanvasPrintDocument sender) { if (bitmaps != null) { return; } bitmaps = new List <BitmapInfo>(); // For this demo we use example gallery's thumbnails (since that's a convenient source of images for the demo) var thumbnailsFolder = await Package.Current.InstalledLocation.GetFolderAsync("Thumbnails"); var files = await thumbnailsFolder.GetFilesAsync(); foreach (var file in files) { if (file.Name.EndsWith("png")) { var bitmap = await CanvasBitmap.LoadAsync(sender, await file.OpenReadAsync()); bitmaps.Add(new BitmapInfo { Name = file.DisplayName, Bitmap = bitmap }); } } largestBitmap = Vector2.Zero; foreach (var bitmap in bitmaps) { largestBitmap.X = Math.Max(largestBitmap.X, (float)bitmap.Bitmap.Size.Width); largestBitmap.Y = Math.Max(largestBitmap.Y, (float)bitmap.Bitmap.Size.Height); } }
private void UserControl_Unloaded(object sender, RoutedEventArgs e) { if (printDocument != null) { printDocument.Dispose(); printDocument = null; } }
private void UserControl_Unloaded(object sender, RoutedEventArgs e) { if (printDocument != null) { printDocument.Dispose(); printDocument = null; } }
private void PrintDocument_Preview(CanvasPrintDocument sender, CanvasPreviewEventArgs args) { var ds = args.DrawingSession; var pageNumber = args.PageNumber; var imageableRect = args.PrintTaskOptions.GetPageDescription(args.PageNumber).ImageableRect; DrawPage(sender, ds, pageNumber, imageableRect); }
public static IPrintDocumentSource Print(Win2DScene scene, float padding) { Guard.NotNull(scene, nameof(scene)); Guard.GreaterThan(padding, 0, nameof(padding)); var printDocument = new CanvasPrintDocument(); var sceneBounds = scene.RenderBounds; Action <CanvasDrawingSession, PrintPageDescription> renderForPrint = (session, page) => { session.Clear(Colors.White); var size = page.PageSize.ToVector2(); var ratio = sceneBounds.Width / sceneBounds.Height; var targetSizeX = Math.Min(size.X - (2 * padding), sceneBounds.Width); var targetSizeY = targetSizeX / ratio; if (targetSizeY > page.PageSize.Height) { targetSizeY = Math.Min(size.Y - (2 * padding), sceneBounds.Height); targetSizeX = targetSizeY * ratio; } var zoom = targetSizeX / sceneBounds.Width; session.Transform = Matrix3x2.CreateTranslation( -sceneBounds.Position.X, -sceneBounds.Position.Y) * Matrix3x2.CreateScale(zoom) * Matrix3x2.CreateTranslation( 0.5f * (size.X - targetSizeX), 0.5f * (size.Y - targetSizeY)); scene.Render(session, false, Rect2.Infinite); }; printDocument.Preview += (sender, args) => { sender.SetPageCount(1); renderForPrint(args.DrawingSession, args.PrintTaskOptions.GetPageDescription(1)); }; printDocument.Print += (sender, args) => { using (var session = args.CreateDrawingSession()) { renderForPrint(session, args.PrintTaskOptions.GetPageDescription(1)); } }; return(printDocument); }
private void PrintDocument_Preview(CanvasPrintDocument sender, CanvasPreviewEventArgs args) { PrintTaskOptions options = args.PrintTaskOptions; PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(options); Rect imageableRect = GetImageableRect(options, args.PageNumber); GraphSize size = GetOptionValue <GraphSize>(printDetailedOptions); LabelLocation labelLocation = GetOptionValue <LabelLocation>(printDetailedOptions); DrawPage(args.DrawingSession, imageableRect, size, labelLocation); }
private async void PrintDocument_PrintTaskOptionsChanged(CanvasPrintDocument sender, CanvasPrintTaskOptionsChangedEventArgs args) { var deferral = args.GetDeferral(); try { await EnsureBitmapsLoaded(sender); var pageDesc = args.PrintTaskOptions.GetPageDescription(1); var newPageSize = pageDesc.PageSize.ToVector2(); if (pageSize == newPageSize && pageCount != -1) { // We've already figured out the pages and the page size hasn't changed, so there's nothing left for us to do here. return; } pageSize = newPageSize; sender.InvalidatePreview(); // Figure out the bitmap index at the top of the current preview page. We'll request that the preview defaults to showing // the page that still has this bitmap on it in the new layout. int indexOnCurrentPage = 0; if (pageCount != -1) { indexOnCurrentPage = (int)(args.CurrentPreviewPageNumber - 1) * bitmapsPerPage; } // Calculate the new layout var printablePageSize = pageSize * 0.9f; cellSize = largestBitmap + imagePadding; var cellsPerPage = printablePageSize / cellSize; columns = Math.Max(1, (int)Math.Floor(cellsPerPage.X)); rows = Math.Max(1, (int)Math.Floor(cellsPerPage.Y)); bitmapsPerPage = columns * rows; // Calculate the page count bitmapCount = bitmaps.Count; pageCount = (int)Math.Ceiling(bitmapCount / (double)bitmapsPerPage); sender.SetPageCount((uint)pageCount); // Set the preview page to the one that has the item that was currently displayed in the last preview args.NewPreviewPageNumber = (uint)(indexOnCurrentPage / bitmapsPerPage) + 1; } finally { deferral.Complete(); } }
private void InitializePrinting() { // create new printer settings printerSettings = new PrinterSettings(); // create new page settings pageSettings = new PageSettings(printerSettings); // create new canvas print document printDocument = new CanvasPrintDocument { PrinterSettings = printerSettings, DefaultPageSettings = pageSettings, }; }
private void PrintDocument_Print(CanvasPrintDocument sender, CanvasPrintEventArgs args) { PrintTaskOptions options = args.PrintTaskOptions; PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(options); Rect imageableRect = GetImageableRect(options, pageNumber: 1); GraphSize size = GetOptionValue <GraphSize>(printDetailedOptions); LabelLocation labelLocation = GetOptionValue <LabelLocation>(printDetailedOptions); using (CanvasDrawingSession drawingSession = args.CreateDrawingSession()) { DrawPage(drawingSession, imageableRect, size, labelLocation); } }
private void InitializePrinting() { printDialog = new PrintDialog() { PageRangeSelection = PageRangeSelection.AllPages, UserPageRangeEnabled = false }; // create new canvas print document printDocument = new CanvasPrintDocument() { AddOverlay = true }; }
private void PrintDocument_PrintTaskOptionsChanged(CanvasPrintDocument sender, CanvasPrintTaskOptionsChangedEventArgs args) { PrintPageDescription pageDesc = args.PrintTaskOptions.GetPageDescription(1); Vector2 newPageSize = pageDesc.PageSize.ToVector2(); if (_pageSize == newPageSize) { // We've already figured out the pages and the page size hasn't changed, // so there's nothing left for us to do here. return; } _pageSize = newPageSize; sender.InvalidatePreview(); sender.SetPageCount(1); args.NewPreviewPageNumber = 1; }
private GraphPrinter(FunctionGraph graph) { _graph = graph; // Resources cannot be loaded from the print thread, so do it here. _title = AppUtilities.GetString("PrintTaskTitle"); _graphSizeOption = AppUtilities.GetString(nameof(GraphSize)); _fullPageItem = AppUtilities.GetString(nameof(GraphSize.FullPage)); _windowSizeItem = AppUtilities.GetString(nameof(GraphSize.Window)); _labelLocationOption = AppUtilities.GetString(nameof(LabelLocation)); _noneItem = AppUtilities.GetString(nameof(LabelLocation.None)); _topLeftItem = AppUtilities.GetString(nameof(LabelLocation.TopLeft)); _topRightItem = AppUtilities.GetString(nameof(LabelLocation.TopRight)); _bottomLeftItem = AppUtilities.GetString(nameof(LabelLocation.BottomLeft)); _bottomRightItem = AppUtilities.GetString(nameof(LabelLocation.BottomRight)); _printDocument = new CanvasPrintDocument(); _printDocument.Preview += PrintDocument_Preview; _printDocument.Print += PrintDocument_Print; _printDocument.PrintTaskOptionsChanged += PrintDocument_PrintTaskOptionsChanged; }
private async void OnPrintClicked(object sender, RoutedEventArgs e) { // Create a new CanvasPrintDocument if (printDocument != null) { printDocument.Dispose(); } printDocument = new CanvasPrintDocument(); printDocument.PrintTaskOptionsChanged += PrintDocument_PrintTaskOptionsChanged; printDocument.Preview += PrintDocument_Preview; printDocument.Print += PrintDocument_Print; // Show the print UI, with the print manager connected to us var printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += PrintingExample_PrintTaskRequested; try { await PrintManager.ShowPrintUIAsync(); } finally { printManager.PrintTaskRequested -= PrintingExample_PrintTaskRequested; } }
private void DrawPage(CanvasPrintDocument sender, CanvasDrawingSession ds, uint pageNumber, Rect imageableRect) { var cellAcross = new Vector2(cellSize.X, 0); var cellDown = new Vector2(0, cellSize.Y); var totalSize = cellAcross * columns + cellDown * rows; Vector2 topLeft = (pageSize - totalSize) / 2; int bitmapIndex = ((int)pageNumber - 1) * bitmapsPerPage; var labelFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 12, VerticalAlignment = CanvasVerticalAlignment.Bottom, HorizontalAlignment = CanvasHorizontalAlignment.Left }; var numberFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 18, VerticalAlignment = CanvasVerticalAlignment.Top, HorizontalAlignment = CanvasHorizontalAlignment.Left }; var pageNumberFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 10, VerticalAlignment = CanvasVerticalAlignment.Bottom, HorizontalAlignment = CanvasHorizontalAlignment.Center }; var titleFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 24, VerticalAlignment = CanvasVerticalAlignment.Top, HorizontalAlignment = CanvasHorizontalAlignment.Left }; if (pageNumber == 1) { ds.DrawText("Win2D Printing Example", imageableRect, Colors.Black, titleFormat); } ds.DrawText(string.Format("Page {0} / {1}", pageNumber, pageCount), imageableRect, Colors.Black, pageNumberFormat); DrawGrid(ds, cellAcross, cellDown, topLeft); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { var cellTopLeft = topLeft + (cellAcross * column) + (cellDown * row); var paddedTopLeft = cellTopLeft + textPadding / 2; var paddedSize = cellSize - textPadding; var bitmapInfo = bitmaps[bitmapIndex % bitmaps.Count]; // Center the bitmap in the cell var bitmapPos = cellTopLeft + (cellSize - bitmapInfo.Bitmap.Size.ToVector2()) / 2; ds.DrawImage(bitmapInfo.Bitmap, bitmapPos); using (var labelLayout = new CanvasTextLayout(sender, bitmapInfo.Name, labelFormat, paddedSize.X, paddedSize.Y)) using (var numberLayout = new CanvasTextLayout(sender, (bitmapIndex + 1).ToString(), numberFormat, paddedSize.X, paddedSize.Y)) { DrawTextOverWhiteRectangle(ds, paddedTopLeft, labelLayout); DrawTextOverWhiteRectangle(ds, paddedTopLeft, numberLayout); } bitmapIndex++; } } }
private void DrawPage(CanvasPrintDocument sender, CanvasDrawingSession ds, uint pageNumber, Rect imageableRect) { var cellAcross = new Vector2(cellSize.X, 0); var cellDown = new Vector2(0, cellSize.Y); var totalSize = cellAcross * columns + cellDown * rows; Vector2 topLeft = (pageSize - totalSize) / 2; int bitmapIndex = ((int)pageNumber - 1) * bitmapsPerPage; var labelFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 12, VerticalAlignment = CanvasVerticalAlignment.Bottom, HorizontalAlignment = CanvasHorizontalAlignment.Left }; var numberFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 18, VerticalAlignment = CanvasVerticalAlignment.Top, HorizontalAlignment = CanvasHorizontalAlignment.Left }; var pageNumberFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 10, VerticalAlignment = CanvasVerticalAlignment.Bottom, HorizontalAlignment = CanvasHorizontalAlignment.Center }; var titleFormat = new CanvasTextFormat() { FontFamily = "Comic Sans MS", FontSize = 24, VerticalAlignment = CanvasVerticalAlignment.Top, HorizontalAlignment = CanvasHorizontalAlignment.Left }; if (pageNumber == 1) ds.DrawText("Win2D Printing Example", imageableRect, Colors.Black, titleFormat); ds.DrawText(string.Format("Page {0} / {1}", pageNumber, pageCount), imageableRect, Colors.Black, pageNumberFormat); DrawGrid(ds, cellAcross, cellDown, topLeft); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { var cellTopLeft = topLeft + (cellAcross * column) + (cellDown * row); var paddedTopLeft = cellTopLeft + textPadding / 2; var paddedSize = cellSize - textPadding; var bitmapInfo = bitmaps[bitmapIndex % bitmaps.Count]; // Center the bitmap in the cell var bitmapPos = cellTopLeft + (cellSize - bitmapInfo.Bitmap.Size.ToVector2()) / 2; ds.DrawImage(bitmapInfo.Bitmap, bitmapPos); using (var labelLayout = new CanvasTextLayout(sender, bitmapInfo.Name, labelFormat, paddedSize.X, paddedSize.Y)) using (var numberLayout = new CanvasTextLayout(sender, (bitmapIndex + 1).ToString(), numberFormat, paddedSize.X, paddedSize.Y)) { DrawTextOverWhiteRectangle(ds, paddedTopLeft, labelLayout); DrawTextOverWhiteRectangle(ds, paddedTopLeft, numberLayout); } bitmapIndex++; } } }
private void PrintDocument_Print(CanvasPrintDocument sender, CanvasPrintEventArgs args) { var detailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(args.PrintTaskOptions); var pageRange = detailedOptions.Options["PageRange"].Value.ToString(); int pageCountToPrint; if (pageRange == "PrintFirstPage") pageCountToPrint = 1; else pageCountToPrint = pageCount; for (uint i = 1; i <= pageCountToPrint; ++i) { using (var ds = args.CreateDrawingSession()) { var imageableRect = args.PrintTaskOptions.GetPageDescription(i).ImageableRect; DrawPage(sender, ds, i, imageableRect); } } }
private void PrintDocument_Preview(CanvasPrintDocument sender, CanvasPreviewEventArgs args) { var ds = args.DrawingSession; var pageNumber = args.PageNumber; var imageableRect = args.PrintTaskOptions.GetPageDescription(args.PageNumber).ImageableRect; DrawPage(sender, ds, pageNumber, imageableRect); }
private async Task EnsureBitmapsLoaded(CanvasPrintDocument sender) { if (bitmaps != null) return; bitmaps = new List<BitmapInfo>(); // For this demo we use example gallery's thumbnails (since that's a convenient source of images for the demo) var thumbnailsFolder = await Package.Current.InstalledLocation.GetFolderAsync("Thumbnails"); var files = await thumbnailsFolder.GetFilesAsync(); foreach (var file in files) { if (file.Name.EndsWith("png")) { var bitmap = await CanvasBitmap.LoadAsync(sender, await file.OpenReadAsync()); bitmaps.Add(new BitmapInfo { Name = file.DisplayName, Bitmap = bitmap }); } } largestBitmap = Vector2.Zero; foreach (var bitmap in bitmaps) { largestBitmap.X = Math.Max(largestBitmap.X, (float)bitmap.Bitmap.Size.Width); largestBitmap.Y = Math.Max(largestBitmap.Y, (float)bitmap.Bitmap.Size.Height); } }
private async void PrintDocument_PrintTaskOptionsChanged(CanvasPrintDocument sender, CanvasPrintTaskOptionsChangedEventArgs args) { var deferral = args.GetDeferral(); try { await EnsureBitmapsLoaded(sender); var pageDesc = args.PrintTaskOptions.GetPageDescription(1); var newPageSize = pageDesc.PageSize.ToVector2(); if (pageSize == newPageSize && pageCount != -1) { // We've already figured out the pages and the page size hasn't changed, so there's nothing left for us to do here. return; } pageSize = newPageSize; sender.InvalidatePreview(); // Figure out the bitmap index at the top of the current preview page. We'll request that the preview defaults to showing // the page that still has this bitmap on it in the new layout. int indexOnCurrentPage = 0; if (pageCount != -1) { indexOnCurrentPage = (int)(args.CurrentPreviewPageNumber - 1) * bitmapsPerPage; } // Calculate the new layout var printablePageSize = pageSize * 0.9f; cellSize = largestBitmap + imagePadding; var cellsPerPage = printablePageSize / cellSize; columns = Math.Max(1, (int)Math.Floor(cellsPerPage.X)); rows = Math.Max(1, (int)Math.Floor(cellsPerPage.Y)); bitmapsPerPage = columns * rows; // Calculate the page count bitmapCount = bitmaps.Count; pageCount = (int)Math.Ceiling(bitmapCount / (double)bitmapsPerPage); sender.SetPageCount((uint)pageCount); // Set the preview page to the one that has the item that was currently displayed in the last preview args.NewPreviewPageNumber = (uint)(indexOnCurrentPage / bitmapsPerPage) + 1; } finally { deferral.Complete(); } }