コード例 #1
0
        public static PrintJob PrintHTML(Activity BaseActivity, string HTMLContent, string DocName = "")
        {
            try
            {
                // Create a WebView object specifically for printing
                WebView webView = new WebView(BaseActivity);
                webView.SetWebViewClient(new WebViewClient());

                // Generate an HTML document on the fly:
                webView.LoadDataWithBaseURL(null, HTMLContent, "text/HTML", "UTF-8", null);
                String htmlDocument = HTMLContent;

                // Get a print adapter instance
                PrintDocumentAdapter printDocumentAdapter = webView.CreatePrintDocumentAdapter(DocName);

                // Get a PrintManager instance
                PrintManager printManager = (PrintManager)BaseActivity.GetSystemService(Context.PrintService);

                // Keep a reference to WebView object until you pass the PrintDocumentAdapter to the PrintManager
                PrintJob printJob = printManager.Print(DocName, printDocumentAdapter, new PrintAttributes.Builder().Build());
                return(printJob);
            }
            catch (Exception Ex)
            {
                WriteErrorLog(Ex);
                return(null);
            }
        }
コード例 #2
0
ファイル: PdfPrint.cs プロジェクト: pandiyan022/HTML2PDF
        public void Print(PrintDocumentAdapter printAdapter)
        {
            printDocumentAdapter = printAdapter;



            printDocumentAdapter.OnLayout(null, printAttributes, null, new MyLayoutResultCallback(printAdapter, JNIEnv.Handle, JniHandleOwnership.DoNotRegister), null);
        }
コード例 #3
0
 public PrintLayoutCallback(
     string pdfFileName,
     PrintDocumentAdapter printDocumentAdapter,
     Action <string> printFinished)
 {
     this.pdfFileName          = pdfFileName;
     this.printDocumentAdapter = printDocumentAdapter;
     this.printFinished        = printFinished;
 }
コード例 #4
0
ファイル: CrossPrint.cs プロジェクト: DuarteGilbert/Punchando
        static void createWebPrintJob(WebView webView)
        {
            PrintManager         printManager = (PrintManager)appcontext.GetSystemService(Context.PrintService);
            String               jobName      = " Document";
            PrintDocumentAdapter printAdapter = webView.CreatePrintDocumentAdapter(jobName);
            PrintJob             printJob     = printManager.Print(jobName, printAdapter, new PrintAttributes.Builder().Build());

            printAdapter.OnStart();
            //printJobs.add(printJob);
        }
コード例 #5
0
        public override void OnPageFinished(Android.Webkit.WebView webView, string url)
        {
            base.OnPageFinished(webView, url);

            var    printManager = (PrintManager)webView.Context.GetSystemService(Context.PrintService);
            string jobName      = webView.Context.PackageName + ".PrintHtml";
            PrintDocumentAdapter printAdapter = webView.CreatePrintDocumentAdapter(jobName);

            Android.Print.PrintJob printJob = printManager.Print(jobName, printAdapter, new PrintAttributes.Builder().Build());
            printManager.PrintJobs.Add(printJob);
        }
コード例 #6
0
ファイル: PDFService_Android.cs プロジェクト: GeorGeWzw/Forms
            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);
                }
            }
コード例 #7
0
        public void Print(WebView viewToPrint)
        {
            var renderer = Platform.CreateRenderer(viewToPrint);
            var webView  = renderer.ViewGroup.GetChildAt(0) as DroidWebView;

            if (webView != null)
            {
                var version = Build.VERSION.SdkInt;

                if (version >= BuildVersionCodes.Kitkat)
                {
                    PrintDocumentAdapter documentAdapter = webView.CreatePrintDocumentAdapter();
                    var printMgr = (PrintManager)Forms.Context.GetSystemService(Context.PrintService);
                    printMgr.Print("Forms-EZ-Print", documentAdapter, null);
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Creates the web print job.
        /// </summary>
        /// <param name="webView">The web view to print.</param>
        private void CreateWebPrintJob(WebView webView)
        {
            // Create the printing resources
            PrintManager printManager = (PrintManager)activity.GetSystemService(Context.PrintService);

            if (printManager != null)
            {
                // Create the print job with the name and adapter
                string jobName = activity.GetString(Resource.String.print_name);
                PrintDocumentAdapter printAdapter = webView.CreatePrintDocumentAdapter(jobName);

                printManager.Print(jobName, printAdapter, null);
            }
            else
            {
                Toast.MakeText(activity, Resource.String.print_failure, ToastLength.Long).Show();
            }
        }
コード例 #9
0
 protected void OnElementChanged(Android.Webkit.WebView webView)
 {
     try
     {
         Device.BeginInvokeOnMainThread(() =>
         {
             var printManager = (PrintManager)Forms.Context.GetSystemService(Context.PrintService);
             PrintDocumentAdapter printAdapter = webView.CreatePrintDocumentAdapter("IDCARD");
             PrintJob printJob = printManager.Print("IDCARD", printAdapter, new PrintAttributes.Builder().Build());
             // Save the job object for later status checking
             mPrintJobs.Add(printJob);
         });
     }
     catch (Exception)
     {
         throw;
     }
 }
コード例 #10
0
        void PrintPage()
        {
            // Ensure google cloud print is installed, or direct users to the google play store
            if (CloudPrintInstalled())
            {
                // Get the print manager.
                PrintManager printManager = (PrintManager)GetSystemService(Context.PrintService);
                // Pass in the WebView's document adapter.
                PrintDocumentAdapter printDocumentAdapter = webView.CreatePrintDocumentAdapter();
                printManager.Print("MyWebPage", printDocumentAdapter, null);
            }
            else
            {
                var    uri         = Android.Net.Uri.Parse("market://details?id=com.google.android.apps.cloudprint");
                Intent storeIntent = new Intent(Intent.ActionView, uri);

                StartActivity(storeIntent);
            }
        }
コード例 #11
0
        /// <summary>
        /// Creates the web print job.
        /// </summary>
        /// <param name="webView">The web view to print.</param>
        private void CreateWebPrintJob(WebView webView)
        {
            // Create the printing resources
            PrintManager printManager = (PrintManager)Xamarin.Forms.Forms.Context.GetSystemService(Context.PrintService);

            if (printManager == null)
            {
                Toast.MakeText(Application.Context, Resource.String.print_failure, ToastLength.Long).Show();
                return;
            }

            try
            {
                // Create the print job with the name and adapter
                PrintDocumentAdapter printAdapter = webView.CreatePrintDocumentAdapter(Application.Context.GetString(Resource.String.print_name));
                printManager.Print(Application.Context.GetString(Resource.String.print_name), printAdapter, null);
            }
            catch (Exception ex)
            {
                Toast.MakeText(Application.Context, ex.Message, ToastLength.Long).Show();
            }
        }
コード例 #12
0
 public MyPrintDocumentAdapter(PrintHtmlOffScreen self)
 {
     this.self        = self;
     mWrappedInstance = self.mWebView.CreatePrintDocumentAdapter();
 }
コード例 #13
0
 public PrintDocumentAdapterWrapper(PrintDocumentAdapter adapter, Action completed) : base()
 {
     this.adapter   = adapter;
     this.completed = completed;
 }
コード例 #14
0
ファイル: PdfPrint.cs プロジェクト: pandiyan022/HTML2PDF
 public MyLayoutResultCallback(PrintDocumentAdapter printDocumentAdapter, IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
 {
     this.printDocumentAdapter = printDocumentAdapter;
 }