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);
                }
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
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.º 4
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);
                }