public void Print(PrintDocumentAdapter printAdapter) { printDocumentAdapter = printAdapter; printDocumentAdapter.OnLayout(null, printAttributes, null, new MyLayoutResultCallback(printAdapter, JNIEnv.Handle, JniHandleOwnership.DoNotRegister), null); }
private async void CreatePDF2(Android.Webkit.WebView webview) { try { // 计算webview打印需要的页数 int numberOfPages = await GetPDFPageCount(webview); File pdfFile = new File(fileNameWithPath); if (pdfFile.Exists()) { pdfFile.Delete(); } pdfFile.CreateNewFile(); descriptor = ParcelFileDescriptor.Open(pdfFile, ParcelFileMode.ReadWrite); // 设置打印参数 var dm = webview.Context.Resources.DisplayMetrics; var d = dm.Density; var dpi = dm.DensityDpi; var height = dm.HeightPixels; var width = dm.WidthPixels; var xdpi = dm.Xdpi; var ydpi = dm.Ydpi; PrintAttributes attributes = new PrintAttributes.Builder() .SetMediaSize(MediaSize) .SetResolution(new PrintAttributes.Resolution("id", Context.PrintService, Convert.ToInt16(xdpi), Convert.ToInt16(ydpi))) .SetColorMode(PrintColorMode.Color) .SetMinMargins(PrintAttributes.Margins.NoMargins) .Build(); ranges = new PageRange[] { new PageRange(0, numberOfPages - 1) }; // 创建pdf文件缓存目录 // 获取需要打印的webview适配器 printAdapter = webview.CreatePrintDocumentAdapter("CreatePDF"); // 开始打印 printAdapter.OnStart(); printAdapter.OnLayout(attributes, attributes, new CancellationSignal(), GetLayoutResultCallback(this), new Bundle()); } catch (Java.IO.FileNotFoundException e) { System.Console.WriteLine(e.Message); } catch (Java.IO.IOException e) { System.Console.WriteLine(e.Message); } catch (Java.Lang.Exception e) { System.Console.WriteLine(e.Message); } }
public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { mWrappedInstance.OnLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras); }