async Task OnPageFinished(Android.Webkit.WebView webView, string fileName, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource) { if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { await Task.Delay(5); var builder = new PrintAttributes.Builder(); //builder.SetMediaSize(PrintAttributes.MediaSize.NaLetter); builder.SetMediaSize(new PrintAttributes.MediaSize(pageSize.Name, pageSize.Name, (int)(pageSize.Width * 1000 / 72), (int)(pageSize.Height * 1000 / 72))); builder.SetResolution(new PrintAttributes.Resolution("pdf", "pdf", 72, 72)); if (margin is null) { builder.SetMinMargins(PrintAttributes.Margins.NoMargins); } else { builder.SetMinMargins(new PrintAttributes.Margins((int)(margin.Left * 1000 / 72), (int)(margin.Top * 1000 / 72), (int)(margin.Right * 1000 / 72), (int)(margin.Bottom * 1000 / 72))); } var attributes = builder.Build(); var adapter = webView.CreatePrintDocumentAdapter(Guid.NewGuid().ToString()); var layoutResultCallback = new PdfLayoutResultCallback(); layoutResultCallback.Adapter = adapter; layoutResultCallback.TaskCompletionSource = taskCompletionSource; layoutResultCallback.FileName = fileName; adapter.OnLayout(null, attributes, null, layoutResultCallback, null); } }
async Task OnPageFinished(Android.Webkit.WebView webView, string fileName, TaskCompletionSource <ToFileResult> taskCompletionSource) { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { var builder = new PrintAttributes.Builder(); builder.SetMediaSize(PrintAttributes.MediaSize.NaLetter); builder.SetResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600)); builder.SetMinMargins(PrintAttributes.Margins.NoMargins); var attributes = builder.Build(); var adapter = webView.CreatePrintDocumentAdapter(Guid.NewGuid().ToString()); var layoutResultCallback = new PdfLayoutResultCallback(); layoutResultCallback.Adapter = adapter; layoutResultCallback.TaskCompletionSource = taskCompletionSource; layoutResultCallback.FileName = fileName; adapter.OnLayout(null, attributes, null, layoutResultCallback, null); } }
internal void OnPageLoaded(Android.Webkit.WebView webView) { try { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { var builder = new PrintAttributes.Builder(); builder.SetMediaSize(PrintAttributes.MediaSize.IsoA4); builder.SetResolution(new PrintAttributes.Resolution("pdf", "pdf", (int)pDFToHtml.PageDPI, (int)pDFToHtml.PageDPI)); builder.SetMinMargins(PrintAttributes.Margins.NoMargins); var attributes = builder.Build(); var adapter = webView.CreatePrintDocumentAdapter(pDFToHtml.FileName); var layoutResultCallback = new PdfLayoutResultCallback(); layoutResultCallback.Adapter = adapter; layoutResultCallback.PDFToHtml = pDFToHtml; adapter.OnLayout(null, attributes, null, layoutResultCallback, null); } } catch { pDFToHtml.Status = PDFEnum.Failed; } }