Display PDF in ImageView (Zoomable)

ID - _BUGFISH - E#51
^Top
<< Back
Mobile-Menu










Display PDF in ImageView (Zoomable)
Category: Android-Java
Sub-Category:
Creator: Jan-Maurice Dahlmanns
Created: 2020-09-03 01:36:01
Modified: 2024-11-17 05:19:52
Views: 645

Caution: 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.

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

 

Currently 0 Upvotes!

captcha image
System - 2024-11-05 18:28:38
Commenting System Initialized! Have a very nice day!

Switches: 0 | Arrivals: 5 | Visits: 5
This Website is using Session Cookies for Site Functionality.