Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we use it on PDF? #64

Open
ashvinstech opened this issue Apr 6, 2021 · 8 comments
Open

Can we use it on PDF? #64

ashvinstech opened this issue Apr 6, 2021 · 8 comments

Comments

@ashvinstech
Copy link

I want to use it on PDF file can i do that?

@malikbilal1997
Copy link

@ashvinstech Yes

@khayalsherif
Copy link

Is there a sample project with pdf?

@eschao
Copy link
Owner

eschao commented Apr 22, 2022

Sorry, I don't have sample for PDF, if someone has that, please share, thanks a lot!

@malikbilal1997
Copy link

I had implemented it with Android PdfRenderer But it was having glitch issues. So I used https://github.com/harism/android-pagecurl it worked real smooth with pdf.

@malikbilal1997
Copy link

malikbilal1997 commented Apr 22, 2022

You can modify this method to use it with pdf. private void drawPage(final int number) It's in SinglePageRender

private void drawPage(final int number) {

        int width = canvas.getWidth();
        int height = canvas.getHeight();

        Paint p = new Paint();

        p.setFilterBitmap(true);

        Bitmap background = PdfRendererUtility.pdfToBitmap(context, pdfLocation, number - 1);

        Rect rect = new Rect(0, 0, width, height);

        canvas.drawBitmap(background, null, rect, p);

        background.recycle();
}

The Utility Class is used to convert the pdf pages to bitmap.

public class PdfRendererUtility {

    /**
     * Calculate Total PDF File Pages Number
     *
     * @param pdfFile File object to pdf file
     * @return 0 if failed to open the pdf file
     */
    public static int pdfTotalPages(String pdfFile) {

        int pages = 0;

        try {

            PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File(pdfFile), ParcelFileDescriptor.MODE_READ_ONLY));

            pages = renderer.getPageCount();

            renderer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pages;
    }

    /**
     * Generate Bitmap of PDF Specified Page.
     *
     * @param context context to read the file
     * @param pdfFile File object to pdf file
     * @return null if couldn't Read pdf file.
     */
    public static Bitmap pdfToBitmap(Context context, String pdfFile, int pageNumber) {

        Bitmap bitmap = null;

        try {

            PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File(pdfFile), ParcelFileDescriptor.MODE_READ_ONLY));

            Page page = renderer.openPage(pageNumber);

            DisplayMetrics displayMetrics = new DisplayMetrics();

            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            bitmap = Bitmap.createBitmap(displayMetrics.widthPixels, displayMetrics.heightPixels, Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(bitmap);

            canvas.drawColor(Color.WHITE);

            canvas.drawBitmap(bitmap, 0, 0, null);

            page.render(bitmap, null, null, RENDER_MODE_FOR_DISPLAY);

            page.close();

            renderer.close();

        } catch (Exception ex) {

            ex.printStackTrace();
        }
        return bitmap;
    }
}

@shittu33
Copy link

check out my implementation here, https://github.com/shittu33/Dynamic-Page-Flip. it's built on eschao pageFlip. with this you can use an xml layout or use pdfium library.

@CoolChimpanzee
Copy link

I want to use double page rendering to read txt files. Is there a similar example?

@shittu33
Copy link

shittu33 commented Jun 19, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants