^

Display PDF in ImageView (Zoomable)

Article 51 in Android-Java

2020-09-03 01:36:01 from bugfish
Display PDF in ImageView (Zoomable)
Android-Java / Hints
Last Review on: 2021-02-26 12:36:21 | Hits: 132

Just a quick setup to render a pdf File.

XML Fragment of Image view (somewhere in your layouts xml where you need it)

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/colorSecondary"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/pdf_view"
            tools:srcCompat="@tools:sample/avatars" />

    </RelativeLayout>

Dependencies in build.gradle (Module File) [Inside dependencies { } ]

    /* PDF LIBRARY AND PHOTOVIEWER ZOOM UPDATE APACHE 2 LICENSE */
      implementation 'com.github.barteksc:android-pdf-viewer:2.8.2' /* To Render PDF */
      implementation 'com.commit451:PhotoView:1.2.4' /* To zoom and scroll image view */

The Java Code

   PdfiumCore pdfiumCore = new PdfiumCore(getApplicationContext());
   Uri currenturi  =   Uri.fromFile(FILE OBJECT WITH PDF FILE);
    ParcelFileDescriptor fd = getApplicationContext().getContentResolver().openFileDescriptor(currenturi, "r");
    PdfDocument pdfDocument = pdfiumCore.newDocument(fd);

    /// OPEN FIRST PAGE
    pdfiumCore.openPage(pdfDocument, INT_OF_PAGE_TO_OPEN);

    /// Find Imageview
    ImageView img = findViewById(R.id.imageView2);

    /// GET PAGES OF FILE
     int currentPagesOnPDF    =    pdfiumCore.getPageCount(pdfDocument);

     /// ADD MULTIPLIERS TO INCREASE THE PDF PIXEL QUALITY
      int width = pdfiumCore.getPageWidthPoint(pdfDocument, tmploop); // *2 ? for double pixel size
      int height = pdfiumCore.getPageHeightPoint(pdfDocument, tmploop); // *2 ? for double pixel size

      Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
       pdfiumCore.renderPageBitmap(pdfDocument, bmp, INT_OF_PAGE_TO_OPEN, 0, 0, width, height);

       /// SET RENDERED PAGE TO VIEW
       img.setImageBitmap(bmp);

       //Close Document when render is done
       pdfiumCore.closeDocument(pdfDocument);

 

You can put this code in your mainactivity, you need to apply functions as site-changing and other stuff by yourself...

Tribute to the Programmers:
https://github.com/barteksc/AndroidPdfViewer
https://github.com/chrisbanes/PhotoView

 

I do not guarantee the reliability of the information given here, the code described on this page is executed at your own risk and in the event of damage or other unforeseeable consequences I am in no way responsible or liable.
Comment Section
Currently 0 Upvotes!
captcha image

System - 2021-01-01 15:18:39
We appreciate comments on our site!

This Website is using Session Cookies for Site Functionality and AWStats.
Radio