Ejemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="printJobConfiguration">Configuration information for the print job</param>
 /// <param name="inputStream">Stream containing PDF data</param>
 public PdfPrintJob(
     PrintJobConfiguration printJobConfiguration,
     Stream inputStream)
     : base(printJobConfiguration)
 {
     _inputStream = inputStream ?? throw new PrintJobException("EXCEPTION:PRN007 Null stream", new ArgumentNullException(nameof(inputStream)));
 }
Ejemplo n.º 2
0
        public override async Task <bool> PrintFromURLAsync(string url, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            try
            {
                if (PrintFromURLAsyncSupported)
                {
                    WebViewPrintClientSubclass webViewClientSubclass = new WebViewPrintClientSubclass(url, printJobConfiguration); // TODO - check that this is disposed
                    webViewClientSubclass.Run();
                    result = true;
                }
            }
            catch (Java.Lang.Exception jlex)
            {
                await PrintStatusReporting.ReportExceptionSilentlyAsync(jlex);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (System.Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Print the contents of a WebView
        /// </summary>
        /// <param name="parentPage">The page that the WebView is shown on.</param>
        /// <param name="webView">The Xamarin.Forms WebView.</param>
        /// <param name="webViewAdditionFunctions">Additional functions</param>
        /// <param name="printJobConfiguration">Configuration information for the print job.</param>
        /// <returns>true if attempting print, false otherwise.</returns>
        public override async Task <bool> PrintWebViewAsync(
            Page parentPage,
            WebView webView,
            IWebViewAdditionalFunctions webViewAdditionFunctions,
            PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            try
            {
                // TODO - get rid of use of UIWebView as deprecated by Apple
                // See https://forums.xamarin.com/discussion/168620/itms-90338-non-public-api-usage-apple-rejected-my-app-xamarin#latest

                if (PrintWebViewAsyncSupported &&
                    (webViewAdditionFunctions?.NativeControl is UIWebView nativeWebView))
                {
                    using (nativeWebView)
                    {
                        result = await PrintUIViewAsync(nativeWebView, printJobConfiguration);
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="printJobConfiguration">Configuration information for the print job</param>
 /// <param name="imageData">Byte array containing image data</param>
 public ImagePrintJob(
     PrintJobConfiguration printJobConfiguration,
     byte[] imageData)
     : base(printJobConfiguration)
 {
     _imageData = imageData;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="printJobConfiguration">Configuration information for the print job</param>
 /// <param name="imageStream">Stream providing image data</param>
 public ImagePrintJob(
     PrintJobConfiguration printJobConfiguration,
     Stream imageStream)
     : base(printJobConfiguration)
 {
     _imageStream = imageStream;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="printJobConfiguration">Configuration information for the print job</param>
 /// <param name="uri">The URI addressing whatever is to be printed</param>
 public UriPrintJob(
     PrintJobConfiguration printJobConfiguration,
     Uri uri)
     : base(printJobConfiguration)
 {
     _uri = uri ?? throw new PrintJobException("EXCEPTION:PRN001 Invalid URL", new ArgumentNullException(nameof(uri)));
 }
Ejemplo n.º 7
0
        public override async Task <bool> PrintFromURLAsync(string url, PrintJobConfiguration printJobConfiguration) // was string jobName, bool enablePrintPreview = true)
        {
            bool result = false;

            try
            {
                if (PrintFromURLAsyncSupported)
                {
                    if (!(url is null))
                    {
                        UriPrintJob printJob = new UriPrintJob(printJobConfiguration, new Uri(url));
                        await printJob.PrintAsync();

                        result = true;
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);

                result = false;
            }
#pragma warning restore CA1031 // Do not catch general exception types

            // TODO - does this want to be here??? printJob?.UnwireAllEvents();

            return(result);
        }
Ejemplo n.º 8
0
        public override async Task <bool> PrintImageFromByteArrayAsync(
            byte[] content,
            PrintJobConfiguration config)
        {
            bool result = false;

            try
            {
                if (PrintImageFromByteArrayAsyncSupported)
                {
                    ImagePrintJob printJob = new ImagePrintJob(config, content);
                    await printJob.PrintAsync();

                    result = true;
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);

                result = false;
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 9
0
        public override async Task <bool> PrintWebViewAsync(
            Page parentPage,
            WebView webView,
            IWebViewAdditionalFunctions webViewAdditionFunctions,
            PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            try
            {
                if (PrintWebViewAsyncSupported &&
                    (webViewAdditionFunctions?.NativeControl is Android.Webkit.WebView nativeWebView))
                {
                    PrintServiceAndroidHelper.PrintFromWebView(nativeWebView, printJobConfiguration);
                    result = true;
                }
            }
            catch (Java.Lang.Exception jlex)
            {
                await PrintStatusReporting.ReportExceptionSilentlyAsync(jlex);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (System.Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 10
0
        public override async Task <bool> PrintPdfFromStreamAsync(
            Stream inputStream,
            PrintJobConfiguration config)
        {
            bool result = false;

            try
            {
                if (PrintPdfFromStreamAsyncSupported)
                {
                    PdfPrintJob printJob = new PdfPrintJob(config, inputStream);
                    await printJob.PrintAsync();

                    result = true;
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);

                result = false;
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
 public override void OnPageFinished(Android.Webkit.WebView webView, string url)
 {
     // TODO - investigate whether even using OnPageFinished is enough to always avoid getting a blank page output
     PrintServiceAndroidHelper.PrintFromWebView(webView, _printJobConfiguration);
     _printJobConfiguration = null;
     _webView.Dispose();
     _webView = null;
     Dispose(); // is this legitimate?
 }
Ejemplo n.º 12
0
        private static async Task <UIPrintInteractionController> PopulateCommonPrinterDetailsAsync(
            PrintJobConfiguration printJobConfiguration)
        {
            UIPrintInteractionController printer = UIPrintInteractionController.SharedPrintController;

            if (printer is null)
            {
                await PrintStatusReporting.ReportErrorAsync(
                    CrossPrinting.PrintingMessages.UnableToPrintAtThisTime);
            }
            else
            {
                printer.PrintInfo = UIPrintInfo.PrintInfo;

                if (!(printer.PrintInfo is null))
                {
                    if (!(printJobConfiguration is null))
                    {
                        switch (printJobConfiguration.DuplexPreference)
                        {
                        case PrintJobDuplexConfiguration.LongEdge:
                            printer.PrintInfo.Duplex = UIPrintInfoDuplex.LongEdge;
                            break;

                        case PrintJobDuplexConfiguration.ShortEdge:
                            printer.PrintInfo.Duplex = UIPrintInfoDuplex.ShortEdge;
                            break;

                        case PrintJobDuplexConfiguration.None:
                        default:
                            printer.PrintInfo.Duplex = UIPrintInfoDuplex.None;
                            break;
                        }

                        printer.PrintInfo.JobName = printJobConfiguration.JobName;

                        printer.PrintInfo.Orientation
                            = printJobConfiguration.PreferPortraitOrientation
                            ? UIPrintInfoOrientation.Portrait
                            : UIPrintInfoOrientation.Landscape;

                        printer.PrintInfo.OutputType
                            = printJobConfiguration.PreferColor
                            ? UIPrintInfoOutputType.General
                            : UIPrintInfoOutputType.Grayscale;
                    }
                }
                printer.ShowsPageRange = true;
            }

            return(printer);
        }
Ejemplo n.º 13
0
        private async Task <bool> PrintImageFromData(NSData data, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            using (data)
            {
                UIImage uiImage = UIImage.LoadFromData(data);
                using (uiImage)
                {
                    result = await PrintObjectAsync(uiImage, printJobConfiguration);
                }
            }

            return(result);
        }
Ejemplo n.º 14
0
        private static async Task <bool> PrintObjectAsync(NSObject nsObject, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            UIPrintInteractionController printer = await PopulateCommonPrinterDetailsAsync(printJobConfiguration);

            if (!(printer is null))
            {
                printer.PrintingItem = nsObject;
                printer.Present(true, PrintCompletion);
                printer.Dispose();
                result = true;
            }

            return(result);
        }
Ejemplo n.º 15
0
        private async Task <bool> PrintFileFromStreamAsync(Stream inputStream, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            try
            {
                string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string library   = Path.Combine(documents, "..", "Library");
                string filePath  = Path.Combine(library, printJobConfiguration.JobName);

                try
                {
                    File.Delete(filePath);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch
                {
                    // no-op
                }
#pragma warning restore CA1031 // Do not catch general exception types

                using (MemoryStream tempStream = new MemoryStream())
                {
                    inputStream.Position = 0;
                    inputStream.CopyTo(tempStream);
                    File.WriteAllBytes(filePath, tempStream.ToArray());
                }

                NSUrl url = NSUrl.FromFilename(filePath);
                using (url)
                {
                    result = await PrintObjectAsync(url, printJobConfiguration);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 16
0
        public override async Task <bool> PrintImageFromStreamAsync(Stream stream, PrintJobConfiguration printJobConfiguration)
        {
            try
            {
                if (PrintImageFromStreamAsyncSupported)
                {
                    return(await PrintImageFromData(
                               NSData.FromStream(stream),
                               printJobConfiguration));
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(false);
        }
Ejemplo n.º 17
0
        public override async Task <bool> PrintFromURLAsync(string url, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            try
            {
                if (PrintFromURLAsyncSupported)
                {
                    using (NSUrl nsUrl = NSUrl.FromString(url))
                    {
                        result = await PrintObjectAsync(nsUrl, printJobConfiguration);
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 18
0
        private async Task <bool> PrintUIViewAsync(UIView uiView, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            UIPrintInteractionController printer = await PopulateCommonPrinterDetailsAsync(printJobConfiguration);

            if (!(printer is null))
            {
                printer.PrintPageRenderer = new UIPrintPageRenderer()
                {
                    HeaderHeight = 40,
                    FooterHeight = 40
                };
                printer.PrintPageRenderer.AddPrintFormatter(uiView.ViewPrintFormatter, 0);

                UIPrintInteractionResult interactionResult;

                if (Device.Idiom == TargetIdiom.Tablet) // TODO - move to ApplicationType
                {
                    interactionResult =
                        await printer.PresentFromRectInViewAsync(new CGRect(200, 200, 0, 0), uiView, true);
                }
                else
                {
                    interactionResult = await printer.PresentAsync(true);
                }

                PrintCompletion(
                    interactionResult.PrintInteractionController,
                    interactionResult.Completed,
                    null);
                result = interactionResult.Completed;
                printer.Dispose();
            }

            return(result);
        }
Ejemplo n.º 19
0
        public override async Task <bool> PrintWebViewAsync(
            Page page,
            WebView webView,
            IWebViewAdditionalFunctions webViewAdditionFunctions,
            PrintJobConfiguration config)
        {
            bool result = false;

            try
            {
                if (PrintWebViewAsyncSupported)
                {
                    if (!(webViewAdditionFunctions is null))
                    {
                        await webViewAdditionFunctions.WaitForViewToBeInNativeLayout(page, 10, 100);

                        WebViewPrintJob printJob = new WebViewPrintJob(config, webViewAdditionFunctions);
                        //WebViewPrintJob printJob = new WebViewPrintJob(config, html);
                        await printJob.PrintAsync();

                        result = true;
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);

                result = false;
            }
#pragma warning restore CA1031 // Do not catch general exception types

            // TODO - does this want to be here??? printJob?.UnwireAllEvents();

            return(result);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="printJobConfiguration">Configuration information for the print job</param>
        /// <param name="webViewAdditionalFunctions">Interface that provides access to functionality required by this class. Typically, this interface will be implemented by a subclass of Xamarin.Forms.WebView</param>
        public WebViewPrintJob(
            PrintJobConfiguration printJobConfiguration,
            IWebViewAdditionalFunctions webViewAdditionalFunctions)
            : base(printJobConfiguration)
        {
            if (webViewAdditionalFunctions is null)
            {
                throw new PrintJobException(
                          "EXCEPTION:PRN003 Invalid interface to native WebView",
                          new ArgumentNullException(nameof(webViewAdditionalFunctions)));
            }
            else if (webViewAdditionalFunctions.NativeControl is null)
            {
                throw new PrintJobException(
                          "EXCEPTION:PRN004 Invalid (null) native WebView");
            }
            else if (!(webViewAdditionalFunctions.NativeControl is Windows.UI.Xaml.Controls.WebView))
            {
                throw new PrintJobException(
                          "EXCEPTION:PRN006 Invalid native WebView");
            }

            _nativeWebView = (Windows.UI.Xaml.Controls.WebView)webViewAdditionalFunctions.NativeControl;
        }
Ejemplo n.º 21
0
 public abstract Task <bool> PrintImageFromByteArrayAsync(byte[] content, PrintJobConfiguration config);
Ejemplo n.º 22
0
 public abstract Task <bool> PrintPdfFromStreamAsync(Stream stream, PrintJobConfiguration config);
Ejemplo n.º 23
0
 public abstract Task <bool> PrintWebViewAsync(Page parentPage, WebView webView, IWebViewAdditionalFunctions webViewAdditionFunctions, PrintJobConfiguration config);
#pragma warning restore IDE0052 // Remove unread private members

        public WebViewPrintClientSubclass(string url, PrintJobConfiguration printJobConfiguration)
        {
            _printJobConfiguration = printJobConfiguration;
            _url = url;
        }
Ejemplo n.º 25
0
        public override async Task <bool> PrintImageFromStreamAsync(Stream inputStream, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            try
            {
                if (PrintImageFromStreamAsyncSupported)
                {
                    PrintServiceAndroidHelper.PrintImageFromStream(inputStream, printJobConfiguration);
                    result = true;
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (System.Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 26
0
        public override async Task <bool> PrintImageFromByteArrayAsync(byte[] content, PrintJobConfiguration printJobConfiguration)
        {
            // based on https://github.com/bushbert/XamarinPCLPrinting/blob/master/PCLPrintExample/PCLPrintExample/PCLPrintExample.Android/Print.cs

            bool result = false;

            try
            {
                if (PrintImageFromByteArrayAsyncSupported)
                {
                    //using (
                    Stream inputStream = new MemoryStream(content); //)
                    {
                        PrintServiceAndroidHelper.PrintImageFromStream(inputStream, printJobConfiguration);
                        result = true;
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (System.Exception ex)
            {
                await PrintStatusReporting.ReportExceptionAsync(ex);
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(result);
        }
Ejemplo n.º 27
0
 public abstract Task <bool> PrintFromURLAsync(string url, PrintJobConfiguration config);
Ejemplo n.º 28
0
 public override async Task <bool> PrintPdfFromStreamAsync(Stream inputStream, PrintJobConfiguration printJobConfiguration)
 => PrintPdfFromStreamAsyncSupported &&
 await PrintFileFromStreamAsync(inputStream, printJobConfiguration);
Ejemplo n.º 29
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="printJobConfiguration">Configuration information for the print job</param>
 protected BasePrintJob(PrintJobConfiguration printJobConfiguration)
 {
     _printJobConfiguration = printJobConfiguration;
 }