Files
Wino-Mail/Wino.Core.UWP/Services/PrintService.cs

266 lines
8.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Printing;
using Windows.Data.Pdf;
using Windows.Graphics.Printing;
using Windows.Graphics.Printing.OptionDetails;
using Windows.Storage.Streams;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Printing;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.UWP.Services;
/// <summary>
/// Printer service that uses WinRT APIs to print PDF files.
/// Used modified version of the code here:
/// https://github.com/microsoft/Win2D-Samples/blob/reunion_master/ExampleGallery/PrintingExample.xaml.cs
/// HTML file is saved as PDF to temporary location.
/// Then PDF is loaded as PdfDocument and printed using CanvasBitmap for each page.
/// </summary>
public class PrintService : IPrintService
{
2025-02-16 11:54:23 +01:00
private TaskCompletionSource<PrintingResult> _taskCompletionSource;
private CanvasPrintDocument printDocument;
private PrintTask printTask;
private PdfDocument pdfDocument;
private List<CanvasBitmap> bitmaps = new();
private Vector2 largestBitmap;
private Vector2 pageSize;
private Vector2 imagePadding = new Vector2(64, 64);
private Vector2 cellSize;
private int bitmapCount;
private int columns;
private int rows;
private int bitmapsPerPage;
private int pageCount = -1;
private PrintInformation _currentPrintInformation;
public async Task<PrintingResult> PrintPdfFileAsync(string pdfFilePath, string printTitle)
{
2025-02-16 11:54:23 +01:00
if (_taskCompletionSource != null)
{
2025-02-16 11:54:23 +01:00
_taskCompletionSource.TrySetResult(PrintingResult.Abandoned);
_taskCompletionSource = new TaskCompletionSource<PrintingResult>();
}
2025-02-16 11:54:23 +01:00
// Load the PDF file
var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(pdfFilePath);
pdfDocument = await PdfDocument.LoadFromFileAsync(file);
2025-02-16 11:54:23 +01:00
_taskCompletionSource ??= new TaskCompletionSource<PrintingResult>();
2025-02-16 11:54:23 +01:00
_currentPrintInformation = new PrintInformation(pdfFilePath, printTitle);
2025-02-16 11:54:23 +01:00
printDocument = new CanvasPrintDocument();
printDocument.PrintTaskOptionsChanged += OnDocumentTaskOptionsChanged;
printDocument.Preview += OnDocumentPreview;
printDocument.Print += OnDocumentPrint;
2025-02-16 11:54:23 +01:00
var printManager = PrintManager.GetForCurrentView();
printManager.PrintTaskRequested += PrintingExample_PrintTaskRequested;
2025-02-16 11:54:23 +01:00
try
{
await PrintManager.ShowPrintUIAsync();
2025-02-16 11:54:23 +01:00
var result = await _taskCompletionSource.Task;
2025-02-16 11:54:23 +01:00
return result;
}
finally
{
// Dispose everything.
UnregisterPrintManager(printManager);
ClearBitmaps();
UnregisterTask();
DisposePDFDocument();
2025-02-16 11:54:23 +01:00
_taskCompletionSource = null;
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:54:23 +01:00
private void DisposePDFDocument()
{
if (pdfDocument != null)
{
2025-02-16 11:54:23 +01:00
pdfDocument = null;
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:54:23 +01:00
private void UnregisterTask()
{
if (printTask != null)
{
2025-02-16 11:54:23 +01:00
printTask.Completed -= TaskCompleted;
printTask = null;
}
2025-02-16 11:54:23 +01:00
}
private void UnregisterPrintManager(PrintManager manager)
{
manager.PrintTaskRequested -= PrintingExample_PrintTaskRequested;
}
2025-02-16 11:54:23 +01:00
private void ClearBitmaps()
{
foreach (var bitmap in bitmaps)
{
2025-02-16 11:54:23 +01:00
bitmap.Dispose();
}
2025-02-16 11:54:23 +01:00
bitmaps.Clear();
}
2025-02-16 11:54:23 +01:00
private void PrintingExample_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
if (_currentPrintInformation == null) return;
2025-02-16 11:54:23 +01:00
printTask = args.Request.CreatePrintTask(_currentPrintInformation.PDFTitle, (createPrintTaskArgs) =>
{
2025-02-16 11:54:23 +01:00
createPrintTaskArgs.SetSource(printDocument);
});
2025-02-16 11:54:23 +01:00
printTask.Completed += TaskCompleted;
}
2025-02-16 11:54:23 +01:00
private void TaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
=> _taskCompletionSource?.TrySetResult((PrintingResult)args.Completion);
2025-02-16 11:54:23 +01:00
private async void OnDocumentTaskOptionsChanged(CanvasPrintDocument sender, CanvasPrintTaskOptionsChangedEventArgs args)
{
var deferral = args.GetDeferral();
2025-02-16 11:54:23 +01:00
try
{
2025-02-16 11:54:23 +01:00
await LoadPDFPageBitmapsAsync(sender);
2025-02-16 11:54:23 +01:00
var pageDesc = args.PrintTaskOptions.GetPageDescription(1);
var newPageSize = pageDesc.PageSize.ToVector2();
2025-02-16 11:54:23 +01:00
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;
}
2025-02-16 11:54:23 +01:00
pageSize = newPageSize;
sender.InvalidatePreview();
2025-02-16 11:54:23 +01:00
// 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;
}
2025-02-16 11:54:23 +01:00
// Calculate the new layout
var printablePageSize = pageSize * 0.9f;
2025-02-16 11:54:23 +01:00
cellSize = largestBitmap + imagePadding;
2025-02-16 11:54:23 +01:00
var cellsPerPage = printablePageSize / cellSize;
2025-02-16 11:54:23 +01:00
columns = Math.Max(1, (int)Math.Floor(cellsPerPage.X));
rows = Math.Max(1, (int)Math.Floor(cellsPerPage.Y));
2025-02-16 11:54:23 +01:00
bitmapsPerPage = columns * rows;
2025-02-16 11:54:23 +01:00
// Calculate the page count
bitmapCount = bitmaps.Count;
pageCount = (int)Math.Ceiling(bitmapCount / (double)bitmapsPerPage);
sender.SetPageCount((uint)pageCount);
2025-02-16 11:54:23 +01:00
// 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;
}
2025-02-16 11:54:23 +01:00
finally
{
2025-02-16 11:54:23 +01:00
deferral.Complete();
}
}
2025-02-16 11:54:23 +01:00
private async Task LoadPDFPageBitmapsAsync(CanvasPrintDocument sender)
{
ClearBitmaps();
2025-02-16 11:54:23 +01:00
bitmaps ??= new List<CanvasBitmap>();
2025-02-16 11:54:23 +01:00
for (int i = 0; i < pdfDocument.PageCount; i++)
{
var page = pdfDocument.GetPage((uint)i);
var stream = new InMemoryRandomAccessStream();
await page.RenderToStreamAsync(stream);
var bitmap = await CanvasBitmap.LoadAsync(sender, stream);
bitmaps.Add(bitmap);
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
largestBitmap = Vector2.Zero;
2025-02-16 11:54:23 +01:00
foreach (var bitmap in bitmaps)
{
2025-02-16 11:54:23 +01:00
largestBitmap.X = Math.Max(largestBitmap.X, (float)bitmap.Size.Width);
largestBitmap.Y = Math.Max(largestBitmap.Y, (float)bitmap.Size.Height);
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private void OnDocumentPreview(CanvasPrintDocument sender, CanvasPreviewEventArgs args)
{
var ds = args.DrawingSession;
var pageNumber = args.PageNumber;
2025-02-16 11:54:23 +01:00
DrawPdfPage(sender, ds, pageNumber);
}
2025-02-16 11:54:23 +01:00
private void OnDocumentPrint(CanvasPrintDocument sender, CanvasPrintEventArgs args)
{
var detailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(args.PrintTaskOptions);
int pageCountToPrint = (int)pdfDocument.PageCount;
2025-02-16 11:54:23 +01:00
for (uint i = 1; i <= pageCountToPrint; ++i)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
using var ds = args.CreateDrawingSession();
var imageableRect = args.PrintTaskOptions.GetPageDescription(i).ImageableRect;
DrawPdfPage(sender, ds, i);
}
}
private void DrawPdfPage(CanvasPrintDocument sender, CanvasDrawingSession ds, uint pageNumber)
{
if (bitmaps?.Count == 0) return;
2025-02-16 11:54:23 +01:00
var cellAcross = new Vector2(cellSize.X, 0);
var cellDown = new Vector2(0, cellSize.Y);
2025-02-16 11:54:23 +01:00
var totalSize = cellAcross * columns + cellDown * rows;
Vector2 topLeft = (pageSize - totalSize) / 2;
2025-02-16 11:54:23 +01:00
int bitmapIndex = ((int)pageNumber - 1) * bitmapsPerPage;
2025-02-16 11:54:23 +01:00
for (int row = 0; row < rows; ++row)
{
for (int column = 0; column < columns; ++column)
{
2025-02-16 11:54:23 +01:00
var cellTopLeft = topLeft + cellAcross * column + cellDown * row;
var bitmapInfo = bitmaps[bitmapIndex % bitmaps.Count];
var bitmapPos = cellTopLeft + (cellSize - bitmapInfo.Size.ToVector2()) / 2;
2025-02-16 11:54:23 +01:00
ds.DrawImage(bitmapInfo, bitmapPos);
2025-02-16 11:54:23 +01:00
bitmapIndex++;
}
}
}
}