Example #1
0
        public void ToPng(string html, string fileName, Action <string> onComplete)
        {
            if (CanWriteExternalStorage())
            {
                var size         = new Size(8.5, 11);
                var externalPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
                var dir          = new Java.IO.File(externalPath);
                var file         = new Java.IO.File(dir + "/" + fileName + ".png");
                if (!dir.Exists())
                {
                    dir.Mkdir();
                }
                if (file.Exists())
                {
                    file.Delete();
                }

                var webView = new CustomWebView(Android.App.Application.Context);
                webView.Settings.JavaScriptEnabled = true;
                webView.DrawingCacheEnabled        = true;

                webView.Layout(0, 0, (int)((size.Width - 0.5) * 72), (int)((size.Height - 0.5) * 72));

                webView.LoadData(html, "text/html; charset=utf-8", "UTF-8");
                webView.SetWebViewClient(new WebViewCallBack(fileName, onComplete));
            }
            else
            {
                onComplete.Invoke(null);
            }
        }
Example #2
0
        public void OnReceiveValue(Java.Lang.Object value)
        {
            System.Diagnostics.Debug.WriteLine("value=[" + value + "]");
            var height = Convert.ToInt32(value.ToString());

            int specWidth  = MeasureSpecFactory.MakeMeasureSpec(webView.ContentWidth, MeasureSpecMode.Exactly);
            int specHeight = MeasureSpecFactory.MakeMeasureSpec(height + 36, MeasureSpecMode.Exactly);

            webView.Measure(specWidth, specHeight);
            webView.Layout(0, 0, webView.MeasuredWidth, webView.MeasuredHeight);

            var bitmap = Bitmap.CreateBitmap(webView.DrawingCache);

            if (!_dir.Exists())
            {
                _dir.Mkdir();
            }
            var path = _dir.Path + "/" + _fileName + ".png";
            var file = new Java.IO.File(path);

            if (!file.Exists())
            {
                file.CreateNewFile();
            }
            var stream = new FileStream(file.Path, FileMode.Create, System.IO.FileAccess.Write);

            bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream);
            stream.Flush();
            stream.Close();
            _onComplete?.Invoke(path);
        }
        public void OnReceiveValue(Java.Lang.Object value)
        {
            System.Diagnostics.Debug.WriteLine("value=[" + value + "]");
            var height = Convert.ToInt32(value.ToString());

            int specWidth  = MeasureSpecFactory.MakeMeasureSpec(webView.ContentWidth, MeasureSpecMode.Exactly);
            int specHeight = MeasureSpecFactory.MakeMeasureSpec(height + 36, MeasureSpecMode.Exactly);

            webView.Measure(specWidth, specHeight);
            webView.Layout(0, 0, webView.MeasuredWidth, webView.MeasuredHeight);
            System.Diagnostics.Debug.WriteLine("spec [" + specWidth + ", " + specHeight + "]");
            System.Diagnostics.Debug.WriteLine("webView.Measured [" + webView.MeasuredWidth + ", " + webView.MeasuredHeight + "]");
            System.Diagnostics.Debug.WriteLine("webView.Layout [" + webView.Left + ", " + webView.Top + ", " + webView.Width + ", " + webView.Height + "]");

            Complete();
        }