eschao / android-PageFlip

3D Style Page Flip on Android
Apache License 2.0
1.74k stars 262 forks source link

Can we use it on PDF? #64

Open ashvinstech opened 3 years ago

ashvinstech commented 3 years ago

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

malikbilal1997 commented 3 years ago

@ashvinstech Yes

khayalsherif commented 2 years ago

Is there a sample project with pdf?

eschao commented 2 years ago

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

malikbilal1997 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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

shittu33 commented 1 year ago

Oops, I have not cater for that at the moment.

On Mon, 19 Jun 2023, 04:15 CoolChimpanzee, @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/eschao/android-PageFlip/issues/64#issuecomment-1596431592, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGGK7RJZX4HGXH6UVHUKSLDXL673RANCNFSM42N6XZRQ . You are receiving this because you commented.Message ID: @.***>