/// <summary> /// Note: Not stable. /// </summary> /// <param name="webview"></param> private async void CreatePDF1(Android.Webkit.WebView webview) { //var pageCount = await GetPDFPageCount(webview); PdfDocument document = new PdfDocument(); PdfDocument.Page page = document.StartPage(new PdfDocument.PageInfo.Builder(2120, 3000, 1).Create()); await Task.Delay(34); webview.Draw(page.Canvas); document.FinishPage(page); Stream filestream = null; FileOutputStream fos = null; try { filestream = new MemoryStream(); fos = new Java.IO.FileOutputStream(fileNameWithPath, false); document.WriteTo(filestream); fos.Write(((MemoryStream)filestream).ToArray(), 0, (int)filestream.Length); } catch (Java.Lang.Exception e) { System.Console.WriteLine(e.Message); } finally { filestream?.Close(); fos?.Close(); } document.Close(); OnPageLoadFinished?.Invoke(this, new EventArgs()); }
public override void OnPageFinished(Android.Webkit.WebView myWebview, string url) { PdfDocument document = new PdfDocument(); PdfDocument.Page page = document.StartPage(new PdfDocument.PageInfo.Builder(2120, 3000, 1).Create()); myWebview.Draw(page.Canvas); document.FinishPage(page); Stream filestream = new MemoryStream(); FileOutputStream fos = new Java.IO.FileOutputStream(fileNameWithPath, false);; try { document.WriteTo(filestream); fos.Write(((MemoryStream)filestream).ToArray(), 0, (int)filestream.Length); fos.Close(); } catch { } }
public override void OnPageFinished(Android.Webkit.WebView myWebview, string url) { PdfDocument document = new PdfDocument(); PdfDocument.Page page = document.StartPage(new PdfDocument.PageInfo.Builder(width, height, 1).Create()); myWebview.Draw(page.Canvas); document.FinishPage(page); Stream filestream = new MemoryStream(); try { FileOutputStream fos = new FileOutputStream(fileNameWithPath, false); document.WriteTo(filestream); fos.Write(((MemoryStream)filestream).ToArray(), 0, (int)filestream.Length); fos.Close(); } catch (Exception ex) { Crashes.TrackError(ex); } }
async Task OnPageFinished(Android.Webkit.WebView view, string fileName, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource) { /* * var widthString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetWidth"); * var width = (int)System.Math.Ceiling(double.Parse(widthString.ToString())); * * var heightString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetHeight"); * var height = (int)System.Math.Ceiling(double.Parse(heightString.ToString())); * var contentHeight = view.ContentHeight; */ //var imageView = new WebViewImage(view, fileName, taskCompletionSource); int specWidth = MeasureSpecFactory.MakeMeasureSpec((int)(pageSize.Width), MeasureSpecMode.Exactly); int specHeight = MeasureSpecFactory.MakeMeasureSpec(0, MeasureSpecMode.Unspecified); view.Measure(specWidth, specHeight); var height = view.ContentHeight; view.Layout(0, 0, view.MeasuredWidth, height); //imageView.Layout(0, 0, width, height); if (height < 1) { var heightString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetHeight"); height = (int)System.Math.Ceiling(double.Parse(heightString.ToString())); } var width = view.MeasuredWidth; if (width < 1) { var widthString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetWidth"); width = (int)System.Math.Ceiling(double.Parse(widthString.ToString())); } if (height < 1 || width < 1) { taskCompletionSource.SetResult(new ToFileResult(true, "WebView width or height is zero.")); return; } await Task.Delay(50); //using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments)) //using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)) using (var _dir = Forms9Patch.Droid.Settings.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads)) { if (!_dir.Exists()) { _dir.Mkdir(); } //var path = _dir.Path + "/" + fileName + ".png"; //var path = System.IO.Path.Combine(_dir.AbsolutePath, Android.OS.Environment.DirectoryDownloads, fileName + ".png"); //var file = new Java.IO.File(path); var file = new Java.IO.File(_dir, fileName + ".png"); int iter = 0; while (file.Exists()) { file.Dispose(); iter++; //path = System.IO.Path.Combine(_dir.AbsolutePath, Android.OS.Environment.DirectoryDownloads, fileName + "_" + iter.ToString("D4") + ".png"); //file = new Java.IO.File(path); file = new Java.IO.File(_dir, fileName + "_" + iter.ToString("D4") + ".png"); } //file.CreateNewFile(); file = Java.IO.File.CreateTempFile(fileName + "_" + iter.ToString("D4"), "png", _dir); var path = file.AbsolutePath; using (var stream = new FileStream(file.Path, FileMode.Create, System.IO.FileAccess.Write)) { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Honeycomb) { await Task.Delay(1000); //using (var bitmap = Bitmap.CreateBitmap(System.Math.Max(view.MeasuredWidth, view.ContentWidth()), view.MeasuredHeight, Bitmap.Config.Argb8888)) using (var bitmap = Bitmap.CreateBitmap(view.MeasuredWidth, height, Bitmap.Config.Argb8888)) { using (var canvas = new Canvas(bitmap)) { if (view.Background != null) { view.Background.Draw(canvas); } else { canvas.DrawColor(Android.Graphics.Color.White); } view.SetClipChildren(false); view.SetClipToPadding(false); view.SetClipToOutline(false); await Task.Delay(50); view.Draw(canvas); await Task.Delay(50); bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream); } } } else { await Task.Delay(1000); #pragma warning disable CS0618 // Type or member is obsolete using (var bitmap = Bitmap.CreateBitmap(view.DrawingCache)) #pragma warning restore CS0618 // Type or member is obsolete { bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream); } } stream.Flush(); stream.Close(); /* * if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Q) * { * // You can add more columns.. Complete list of columns can be found at * // https://developer.android.com/reference/android/provider/MediaStore.Downloads * var contentValues = new ContentValues(); * contentValues.Put(Android.Provider.MediaStore.DownloadColumns.Title, System.IO.Path.GetFileName(path)); * contentValues.Put(Android.Provider.MediaStore.DownloadColumns.DisplayName, System.IO.Path.GetFileName(path)); * contentValues.Put(Android.Provider.MediaStore.DownloadColumns.MimeType, "image/png"); * contentValues.Put(Android.Provider.MediaStore.DownloadColumns.Size, File.ReadAllBytes(path).Length); * * // If you downloaded to a specific folder inside "Downloads" folder * //contentValues.Put(Android.Provider.MediaStore.DownloadColumns.RelativePath, Android.OS.Environment.DirectoryDownloads + File.separator + "Temp"); * * // Insert into the database * ContentResolver database = Forms9Patch.Droid.Settings.Context.ContentResolver; // getContentResolver(); * database.Insert(Android.Provider.MediaStore.Downloads.ExternalContentUri, contentValues); * } * else * { * // notify download manager! * var downloadManager = Android.App.DownloadManager.FromContext(Android.App.Application.Context); #pragma warning disable CS0618 // Type or member is obsolete * downloadManager.AddCompletedDownload( * System.IO.Path.GetFileName(path), * System.IO.Path.GetFileName(path), * true, "image/png", path, * File.ReadAllBytes(path).Length, true); #pragma warning restore CS0618 // Type or member is obsolete * } */ taskCompletionSource.SetResult(new ToFileResult(false, path)); view.Dispose(); } file.Dispose(); } }
async Task OnPageFinished(Android.Webkit.WebView view, string fileName, TaskCompletionSource <ToFileResult> taskCompletionSource) { var widthString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetWidth"); var width = double.Parse(widthString.ToString()); var heightString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetHeight"); var height = double.Parse(heightString.ToString()); int specWidth = MeasureSpecFactory.MakeMeasureSpec((int)(width * Display.Scale), MeasureSpecMode.Exactly); int specHeight = MeasureSpecFactory.MakeMeasureSpec((int)(height * Display.Scale), MeasureSpecMode.Exactly); view.Measure(specWidth, specHeight); view.Layout(0, 0, view.MeasuredWidth, view.MeasuredHeight); await Task.Delay(50); using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments)) { if (!_dir.Exists()) { _dir.Mkdir(); } var path = _dir.Path + "/" + fileName + ".png"; using (var file = new Java.IO.File(path)) { if (!file.Exists()) { file.CreateNewFile(); } using (var stream = new FileStream(file.Path, FileMode.Create, System.IO.FileAccess.Write)) { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Honeycomb) { await Task.Delay(1000); using (var bitmap = Bitmap.CreateBitmap(System.Math.Max(view.MeasuredWidth, view.ContentWidth()), view.MeasuredHeight, Bitmap.Config.Argb8888)) { using (var canvas = new Canvas(bitmap)) { if (view.Background != null) { view.Background.Draw(canvas); } else { canvas.DrawColor(Android.Graphics.Color.White); } view.SetClipChildren(false); view.SetClipToPadding(false); view.SetClipToOutline(false); await Task.Delay(50); view.Draw(canvas); await Task.Delay(50); bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream); } } } else { await Task.Delay(1000); #pragma warning disable CS0618 // Type or member is obsolete using (var bitmap = Bitmap.CreateBitmap(view.DrawingCache)) #pragma warning restore CS0618 // Type or member is obsolete { bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream); } } stream.Flush(); stream.Close(); taskCompletionSource.SetResult(new ToFileResult(false, path)); view.Dispose(); } } } }
async Task OnPageFinished(Android.Webkit.WebView view, string fileName, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource) { /* * var widthString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetWidth"); * var width = (int)System.Math.Ceiling(double.Parse(widthString.ToString())); * * var heightString = await view.EvaluateJavaScriptAsync("document.documentElement.offsetHeight"); * var height = (int)System.Math.Ceiling(double.Parse(heightString.ToString())); * var contentHeight = view.ContentHeight; */ //var imageView = new WebViewImage(view, fileName, taskCompletionSource); int specWidth = MeasureSpecFactory.MakeMeasureSpec((int)(pageSize.Width), MeasureSpecMode.Exactly); int specHeight = MeasureSpecFactory.MakeMeasureSpec(0, MeasureSpecMode.Unspecified); view.Measure(specWidth, specHeight); var height = view.ContentHeight; view.Layout(0, 0, view.MeasuredWidth, height); //imageView.Layout(0, 0, width, height); if (height < 1 || view.MeasuredWidth < 1) { taskCompletionSource.SetResult(new ToFileResult(true, "WebView width or height is zero.")); return; } await Task.Delay(50); //using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments)) using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)) { if (!_dir.Exists()) { _dir.Mkdir(); } //var path = _dir.Path + "/" + fileName + ".png"; var path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads, fileName + ".png"); var file = new Java.IO.File(path); int iter = 0; while (file.Exists()) { file.Dispose(); iter++; path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads, fileName + "_" + iter.ToString("D3") + ".png"); file = new Java.IO.File(path); } file.CreateNewFile(); using (var stream = new FileStream(file.Path, FileMode.Create, System.IO.FileAccess.Write)) { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Honeycomb) { await Task.Delay(1000); //using (var bitmap = Bitmap.CreateBitmap(System.Math.Max(view.MeasuredWidth, view.ContentWidth()), view.MeasuredHeight, Bitmap.Config.Argb8888)) using (var bitmap = Bitmap.CreateBitmap(view.MeasuredWidth, height, Bitmap.Config.Argb8888)) { using (var canvas = new Canvas(bitmap)) { if (view.Background != null) { view.Background.Draw(canvas); } else { canvas.DrawColor(Android.Graphics.Color.White); } view.SetClipChildren(false); view.SetClipToPadding(false); view.SetClipToOutline(false); await Task.Delay(50); view.Draw(canvas); await Task.Delay(50); bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream); } } } else { await Task.Delay(1000); #pragma warning disable CS0618 // Type or member is obsolete using (var bitmap = Bitmap.CreateBitmap(view.DrawingCache)) #pragma warning restore CS0618 // Type or member is obsolete { bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream); } } stream.Flush(); stream.Close(); // notify download manager! var downloadManager = Android.App.DownloadManager.FromContext(Android.App.Application.Context); downloadManager.AddCompletedDownload( System.IO.Path.GetFileName(path), System.IO.Path.GetFileName(path), true, "image/png", path, File.ReadAllBytes(path).Length, true); taskCompletionSource.SetResult(new ToFileResult(false, path)); view.Dispose(); } file.Dispose(); } }