Ejemplo n.º 1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource:
            SetContentView(Resource.Layout.pdf_paging_activity);

            // Get our RecyclerView layout:
            mRecyclerView = FindViewById <RecyclerView>(Resource.Id.recyclerView);

            btnPrev        = FindViewById <Button>(Resource.Id.button_prev);
            btnPrev.Text   = "Prev";
            btnPrev.Click += BtnPrev_Click;

            btnNext        = FindViewById <Button>(Resource.Id.button_next);
            btnNext.Text   = "Next";
            btnNext.Click += BtnNext_Click;

            textPageCurrent = FindViewById <TextView>(Resource.Id.text_page_current);

            // Use the built-in linear layout manager:
            mLayoutManager = new LinearLayoutManager(this);

            // Plug the layout manager into the RecyclerView:
            mRecyclerView.SetLayoutManager(mLayoutManager);

            // Get screen width for bitmap sizing
            screenWidth = Resources.DisplayMetrics.WidthPixels;

            // Set up pdf bitmap adapter
            pages    = new List <Bitmap>();
            mAdapter = new PdfBitmapAdapter(pages);
            mRecyclerView.SetAdapter(mAdapter);

            // Register the item click handler (below) with the adapter:
            mAdapter.ItemClick += OnItemClick;

            RenderPages();
        }
Ejemplo n.º 2
0
        void RenderPages()
        {
            Stream inputStream = Assets.Open(fileName);

            using (var outputStream = this.OpenFileOutput("_sample.pdf", Android.Content.FileCreationMode.Private))
            {
                inputStream.CopyTo(outputStream);
            }
            var          fileStreamPath = this.GetFileStreamPath("_sample.pdf");
            MemoryStream m_memoryStream = new MemoryStream();

            File.OpenRead(fileStreamPath.AbsolutePath).CopyTo(m_memoryStream);

            var renderer    = new PdfRenderer(ParcelFileDescriptor.Open(fileStreamPath, ParcelFileMode.ReadOnly));
            var screenWidth = Resources.DisplayMetrics.WidthPixels;

            // render all pages
            pageCount = renderer.PageCount;
            for (int i = 0; i < pageCount; i++)
            {
                page = renderer.OpenPage(i);

                // create bitmap at appropriate size
                var ratio     = (float)page.Height / page.Width;
                var newHeight = screenWidth * ratio;
                bitmap = Bitmap.CreateBitmap(screenWidth, (int)newHeight, Bitmap.Config.Argb8888);

                // render PDF page to bitmap
                page.Render(bitmap, null, null, PdfRenderMode.ForDisplay);

                // add bitmap to list
                pages.Add(bitmap);

                // close the page
                page.Close();

                // if free memory is less than the size of two page bitmaps and we still have pages left to load
                // we'll stop loading and then display a message about downloading the full doc
                // * this catch doesn't seem to work on Android O but it worked on a Nexus 5 running Android M, apparently Android O changes some memory handling
                Debug.WriteLine("\nMemory usage " + i + "\n" + "Bitmap Byte Count: " + bitmap.ByteCount + "\nMemory Available: " + MemoryAvailable());
                if (bitmap.ByteCount * 2 > MemoryAvailable() && i < pageCount - 1)
                {
                    lowMemory = true;
                    break;
                }
            }

            // close the renderer
            renderer.Close();

            // Create an adapter for the RecyclerView, and pass it the
            // data set (the bitmap list) to manage:
            mAdapter = new PdfBitmapAdapter(pages);

            // Register the item click handler (below) with the adapter:
            mAdapter.ItemClick += OnItemClick;

            // Plug the adapter into the RecyclerView:
            mRecyclerView.SetAdapter(mAdapter);

            if (lowMemory)
            {
                try
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.SetTitle("Out Of Memory");
                    alert.SetMessage("You are only viewing " + pages.Count + " out of " + pageCount + " pages.");
                    alert.SetPositiveButton("OK", (senderAlert, args) => { });
                    Dialog dialog = alert.Create();
                    dialog.Show();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception: " + e.Message);
                }
            }
        }