Ejemplo n.º 1
0
 public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
 {
     try
     {
         using (InputStream input = new FileInputStream(path))
         {
             using (OutputStream output = new FileOutputStream(destination.FileDescriptor))
             {
                 var buf = new byte[1024];
                 int bytesRead;
                 while ((bytesRead = input.Read(buf)) > 0)
                 {
                     output.Write(buf, 0, bytesRead);
                 }
             }
         }
         callback.OnWriteFinished(new[] { PageRange.AllPages });
     }
     catch (FileNotFoundException fileNotFoundException)
     {
         System.Diagnostics.Debug.WriteLine(fileNotFoundException);
     }
     catch (Exception exception)
     {
         System.Diagnostics.Debug.WriteLine(exception);
     }
 }
Ejemplo n.º 2
0
        public override void OnWrite(PageRange[] pages, Android.OS.ParcelFileDescriptor destination, Android.OS.CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream  input  = null;
            OutputStream output = null;

            try {
                input  = new FileInputStream(_filePath);
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[1024];
                int    bytesRead;

                while ((bytesRead = input.Read(buf)) > 0)
                {
                    output.Write(buf, 0, bytesRead);
                }

                callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
            } catch (FileNotFoundException ee) {
                //Catch exception
            } catch (Exception e) {
                //Catch exception
            } finally {
                try {
                    input.Close();
                    output.Close();
                } catch (IOException e) {
                    e.PrintStackTrace();
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="pages"></param>
        /// <param name="destination"></param>
        /// <param name="cancellationSignal"></param>
        /// <param name="callback"></param>
        public override async void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            _input  = null;
            _output = null;

            try
            {
                _input  = new FileInputStream(FileToPrint);                 // if we use using on this, we get an ObjectDisposedException // TODO - investigate further
                _output = new FileOutputStream(destination.FileDescriptor); // if we use using on this, we get an ObjectDisposedException // TODO - investigate further

                byte[] buf = new byte[1024];
                int    bytesRead;

                while ((bytesRead = _input.Read(buf)) > 0)
                {
                    _output.Write(buf, 0, bytesRead);
                }

                callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
            }
            catch (Java.IO.FileNotFoundException ee)
            {
                await PrintStatusReporting.ReportExceptionSilentlyAsync(ee);
            }
            catch (Java.Lang.Exception jlex)
            {
                await PrintStatusReporting.ReportExceptionSilentlyAsync(jlex);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (System.Exception e)
            {
                await PrintStatusReporting.ReportExceptionSilentlyAsync(e);
            }
#pragma warning restore CA1031 // Do not catch general exception types
            finally
            {
                try
                {
                    if (!(_input is null))
                    {
                        _input.Close();
                        _input = null;
                    }

                    if (!(_output is null))
                    {
                        _output.Close();
                        _output = null;
                    }
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (System.Exception e)
                {
                    await PrintStatusReporting.ReportExceptionSilentlyAsync(e);
                }
#pragma warning restore CA1031 // Do not catch general exception types
            }
        }
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream input = null;

            OutputStream output = null;

            try
            {
                File file = new File(path);

                input  = new FileInputStream(file);
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[8 * 1024];

                int ligne;

                while ((ligne = input.Read(buf)) >= 0 && !cancellationSignal.IsCanceled)
                {
                    output.Write(buf, 0, ligne);
                }

                if (cancellationSignal.IsCanceled)
                {
                    callback.OnWriteCancelled();
                }
                else
                {
                    callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
                }
            }
            catch (Exception ex)
            {
                callback.OnWriteFailed(ex.Message);
            }
            finally
            {
                try
                {
                    input.Close();

                    output.Close();
                }
                catch (IOException ex)
                {
                    Log.Error("e", "", ex.Message);
                }
            }
        }
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream  input  = null;
            OutputStream output = null;

            try
            {
                //Create FileInputStream object from the given file
                input = new FileInputStream(FileToPrint);
                //Create FileOutputStream object from the destination FileDescriptor instance
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[1024];
                int    bytesRead;

                while ((bytesRead = input.Read(buf)) > 0)
                {
                    //Write the contents of the given file to the print destination
                    output.Write(buf, 0, bytesRead);
                }

                callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
            }
            catch (FileNotFoundException ex)
            {
                //Catch exception
                System.Diagnostics.Debug.WriteLine(ex);
            }
            catch (Exception e)
            {
                //Catch exception
                System.Diagnostics.Debug.WriteLine(e);
            }
            finally
            {
                try
                {
                    input.Close();
                    output.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                    System.Diagnostics.Debug.WriteLine(e);
                }
            }
        }
Ejemplo n.º 6
0
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream  input  = null;
            OutputStream output = null;

            try
            {
                File file = new File(_filePath);
                input  = new FileInputStream(file);
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[16384];
                int    size;

                while ((size = input.Read(buf)) >= 0 && !cancellationSignal.IsCanceled)
                {
                    output.Write(buf, 0, size);
                }

                if (cancellationSignal.IsCanceled)
                {
                    callback.OnWriteCancelled();
                }
                else
                {
                    callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
                }
            }
            catch (Exception e)
            {
                callback.OnWriteFailed(e.Message);
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            finally
            {
                try
                {
                    input?.Close();
                    output?.Close();
                }
                catch (IOException e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }
        }
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination,
                                     CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            PrintedPdfDocument.Page page = document.StartPage(0);

            page.Canvas.Scale(scale, scale);

            page.Canvas.DrawText(text, page.Canvas.ClipBounds.Right / 2, page.Canvas.ClipBounds.Bottom / 2, new Android.Graphics.Paint());

            document.FinishPage(page);

            WritePrintedPdfDoc(destination);

            document.Close();

            document.Dispose();

            callback.OnWriteFinished(pages);
        }
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination,
                                      CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            PrintedPdfDocument.Page page = document.StartPage(0);

            page.Canvas.Scale(scale, scale);

            page.Canvas.DrawText(text, page.Canvas.ClipBounds.Right / 2, page.Canvas.ClipBounds.Bottom / 2, new Android.Graphics.Paint());
            
            document.FinishPage(page);

            WritePrintedPdfDoc(destination);

            document.Close();

            document.Dispose();

            callback.OnWriteFinished(pages);
        }
Ejemplo n.º 9
0
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination,
                                     CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            PrintedPdfDocument.Page page = document.StartPage(0);

            page.Canvas.Scale(scale, scale);

            view.Draw(page.Canvas);

            document.FinishPage(page);

            WritePrintedPdfDoc(destination);

            document.Close();

            document.Dispose();

            callback.OnWriteFinished(pages);
        }
Ejemplo n.º 10
0
            public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
            {
                int totalPages = 1;

                for (int i = 0; i < totalPages; i++)
                {
                    if (pageInRange(pages, i))
                    {
                        PdfDocument.Page[] writtenPagesArray;
                        PdfDocument.Page   page = mPdfDocument.StartPage(i);
                        if (cancellationSignal.IsCanceled)
                        {
                            callback.OnWriteCancelled();
                            mPdfDocument.Close();
                            mPdfDocument = null;
                            return;
                        }
                        drawPage(page);
                        mPdfDocument.FinishPage(page);
                    }
                }
                try
                {
                    FileOutputStream fileOutputStream = new FileOutputStream(destination.FileDescriptor);
                    MemoryStream     stream           = new MemoryStream();
                    mPdfDocument.WriteTo(stream);
                    byte[] bytes = stream.ToArray();
                    stream.Close();
                    fileOutputStream.Write(bytes);
                }
                catch (Java.IO.IOException e)
                {
                    callback.OnWriteFailed(e.ToString());
                    return;
                }
                finally
                {
                    mPdfDocument.Close();
                    mPdfDocument = null;
                }
                callback.OnWriteFinished(pages);
            }
Ejemplo n.º 11
0
 public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
 {
     try
     {
         using (InputStream input = new FileInputStream(filePath))
         {
             using (OutputStream output = new FileOutputStream(destination.FileDescriptor))
             {
                 var buf = new byte[1024];
                 int bytesRead;
                 while ((bytesRead = input.Read(buf)) > 0)
                 {
                     output.Write(buf, 0, bytesRead);
                 }
             }
         }
         callback.OnWriteFinished(new[] { PageRange.AllPages });
     }
     catch (Exception exception)
     {
         throw new AggregateException(exception);
     }
 }
Ejemplo n.º 12
0
                protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms)
                {
                    // Go over all the pages and write only the requested ones.
                    // Create an adapter with the stats and an inflater
                    // to load resources for the printer density.
                    var adapter = new MotoGpStatAdapter(items,
                                                        (LayoutInflater)self.mPrintContext.GetSystemService(
                                                            Context.LayoutInflaterService));

                    int  currentPage       = -1;
                    int  pageContentHeight = 0;
                    int  viewType          = -1;
                    View view = null;

                    Android.Graphics.Pdf.PdfDocument.Page page = null;
                    var dummyParent = new LinearLayout(self.mPrintContext);

                    dummyParent.Orientation = Android.Widget.Orientation.Vertical;

                    // The content is laid out and rendered in screen pixels with
                    // the width and height of the paper size times the print
                    // density but the PDF canvas size is in points which are 1/72",
                    // so we will scale down the content.
                    float scale = System.Math.Min(
                        (float)mPdfDocument.PageContentRect.Width() / self.mRenderPageWidth,
                        (float)mPdfDocument.PageContentRect.Height() / self.mRenderPageHeight);

                    int itemCount = adapter.Count;

                    for (int i = 0; i < itemCount; i++)
                    {
                        // Be nice and respond to cancellation.
                        if (IsCancelled)
                        {
                            return(null);
                        }

                        // Get the next view.
                        int nextViewType = adapter.GetItemViewType(i);
                        if (viewType == nextViewType)
                        {
                            view = adapter.GetView(i, view, dummyParent);
                        }
                        else
                        {
                            view = adapter.GetView(i, null, dummyParent);
                        }
                        viewType = nextViewType;

                        // Measure the next view
                        self.MeasureView(view);

                        // Add the height but if the view crosses the page
                        // boundary we will put it to the next one.
                        pageContentHeight += view.MeasuredHeight;
                        if (currentPage < 0 || pageContentHeight > self.mRenderPageHeight)
                        {
                            pageContentHeight = view.MeasuredHeight;
                            currentPage++;
                            // Done with the current page - finish it.
                            if (page != null)
                            {
                                mPdfDocument.FinishPage(page);
                            }
                            // If the page is requested, render it.
                            if (self.ContainsPage(pages, currentPage))
                            {
                                page = mPdfDocument.StartPage(currentPage);
                                page.Canvas.Scale(scale, scale);
                                // Keep track which pages are written.
                                mWrittenPages.Append(mWrittenPages.Size(), currentPage);
                            }
                            else
                            {
                                page = null;
                            }
                        }

                        // If the current view is on a requested page, render it.
                        if (page != null)
                        {
                            // Layout an render the content.
                            view.Layout(0, 0, view.MeasuredWidth, view.MeasuredHeight);
                            view.Draw(page.Canvas);
                            // Move the canvas for the next view.
                            page.Canvas.Translate(0, view.Height);
                        }
                    }

                    // Done with the last page.
                    if (page != null)
                    {
                        mPdfDocument.FinishPage(page);
                    }

                    // Write the data and return success or failure.
                    try {
                        var fos = new Java.IO.FileOutputStream(destination.FileDescriptor);
                        mPdfDocument.WriteTo(new OutputStreamInvoker(fos));
                        // Compute which page ranges were written based on
                        // the bookkeeping we maintained.
                        var pageRanges = self.ComputeWrittenPageRanges(mWrittenPages);
                        callback.OnWriteFinished(pageRanges);
                    } catch (IOException) {
                        callback.OnWriteFailed((string)null);
                    } finally {
                        mPdfDocument.Close();
                    }

                    return(null);
                }